La Web del Programador: Comunidad de Programadores
 
    Pregunta:  65685 - IPCONFIG AUTOMATICO
Autor:  Ulises R.M
Saludos, Me dedico a reparar equipo de computo y en ocaciones me topo con fallas en la red y debo estar presione ipconfig varias veces entre otros comandos mi, duda es como puedo usar basic para ejecutar comandos de ms-DOS, y leer esos datos como texto y reusarlo para otra tarea
ejemplo:

Ipconfig
gateway 192.168.1.254
ip 192.168.1.2
netmask 254.254.254.0
leer gateway
ping 192.168.1.254

mas o menos asi :D gracias!!

  Respuesta:  Gonzalo Quintana
Espero que esto te sirva, está hecho desde VBA (excel) en un form.

Option Explicit


'Un textbox multilinea : txt_resultado
'Un TextBox : txt_Comando
'Un commandbutton
'La referencia a Windows Script Host Object Model
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Private Sub CommandButton1_Click()

txt_resultado.Text = ""
txt_resultado.Text = ejecutar_Dos("ipconfig /all")

End Sub

Private Sub UserForm_Activate()

CommandButton1.Caption = " Ejecutar DOS "
txt_Comando.Text = "dir c:windowssystem32*.*"

End Sub

Function ejecutar_Dos(Comando As String) As String

Dim oShell As WshShell
Dim oExec As WshExec
Dim ret As String

Set oShell = New WshShell
DoEvents

' ejecutar el comando
Set oExec = oShell.Exec("%comspec% /c " & Comando)
ret = oExec.StdOut.ReadAll()

' retornar la salida y devolverla a la función
ejecutar_Dos = Replace(ret, Chr(10), vbNewLine)

DoEvents
'Me.SetFocus

End Function

'Ejemplos
'txt_resultado.Text = ejecutar_Dos("ping www.google.com.ar")
'txt_resultado.Text = ejecutar_Dos(Trim(txt_Comando))
'txt_resultado.Text = ejecutar_Dos("ipconfig help")

Saludos,