Discussion:
Wie PING (DOS) per VBA ausführen
(zu alt für eine Antwort)
Carsten Boll
2007-01-25 12:12:55 UTC
Permalink
Hi NG,

XL 2000

Wie kann ich den DOS-Befehl PING von VBA aus ausführen und das Ergebnis
auswerten? Ich hatte überlegt, mit 'PING 123.456.789.123 > LOG.TXT' eine
Textdatei zu erzeugen und diese dann einzulesen.

Oder gibt es eine elegantere Lösung, ob ein bestimmter Rechner im Netzwerk
ansprechbar ist?

Danke,

Carsten
Peter Lederer
2007-01-25 12:47:41 UTC
Permalink
Post by Carsten Boll
Oder gibt es eine elegantere Lösung, ob ein bestimmter Rechner im Netzwerk
ansprechbar ist?
Schau dir das mal an
<http://www.herber.de/forum/archiv/428to432/t430941.htm>, habe ich selbst
aber nicht getestet.
--
Grüße
Peter
WIN XP Prof. SP2, Excel 2002
unknown
2007-01-25 13:17:36 UTC
Permalink
Hallo Carsten,
folgende Beispielanweisung:

Ergebnis = Shell("cmd.exe /C ping 192.168.0.30 >C:\ping.txt")

MfG Frank
_________________________________________________
Frank Arendt-Theilen (ehem. MVP für Excel), Hameln
Microsoft Excel - Die ExpertenTipps: tinyurl.com/cmned
Website: xl-faq.de
Ralph 'rkhb' Bauer
2007-01-25 18:07:11 UTC
Permalink
Post by Carsten Boll
Hi NG,
XL 2000
Wie kann ich den DOS-Befehl PING von VBA aus ausführen und das
Ergebnis auswerten? Ich hatte überlegt, mit 'PING 123.456.789.123 >
LOG.TXT' eine Textdatei zu erzeugen und diese dann einzulesen.
Oder gibt es eine elegantere Lösung, ob ein bestimmter Rechner im
Netzwerk ansprechbar ist?
Eine elegantere Lösung gibt es bei:

http://www.activevb.de/tipps/vb6tipps/tipp0272.html

Ich habe mir erlaubt. den Quelltext für VBA anzupassen und etwas abzuändern.
Kopiere den nachfolgenden Code in ein Modul und starte Ping().

Option Explicit

Private Declare Function CreatePipe Lib "kernel32" (phReadPipe _
As Long, phWritePipe As Long, lpPipeAttributes As Any, _
ByVal nSize As Long) As Long

Private Declare Function ReadFile Lib "kernel32" (ByVal hFile _
As Long, ByVal lpBuffer As String, ByVal _
nNumberOfBytesToRead As Long, lpNumberOfBytesRead As _
Long, ByVal lpOverlapped As Any) As Long

Private Declare Function CreateProcessA Lib "kernel32" (ByVal _
lpApplicationName As Long, ByVal lpCommandLine As _
String, lpProcessAttributes As Any, lpThreadAttributes _
As Any, ByVal bInheritHandles As Long, ByVal _
dwCreationFlags As Long, ByVal lpEnvironment As Long, _
ByVal lpCurrentDirectory As Long, lpStartupInfo As Any, _
lpProcessInformation As Any) As Long

Private Declare Function CloseHandle Lib "kernel32" (ByVal _
hObject As Long) As Long

Private Declare Function PeekNamedPipe Lib "kernel32" (ByVal _
hNamedPipe As Long, lpBuffer As Any, ByVal nBufferSize _
As Long, lpBytesRead As Long, lpTotalBytesAvail As Long, _
lpBytesLeftThisMessage As Long) As Long

Private Declare Function WaitForSingleObject Lib "kernel32" ( _
ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long

Private Type SECURITY_ATTRIBUTES
nLength As Long
lpSecurityDescriptor As Long
bInheritHandle As Long
End Type

Private Type STARTUPINFO
cb As Long
lpReserved As Long
lpDesktop As Long
lpTitle As Long
dwX As Long
dwY As Long
dwXSize As Long
dwYSize As Long
dwXCountChars As Long
dwYCountChars As Long
dwFillAttribute As Long
dwFlags As Long
wShowWindow As Integer
cbReserved2 As Integer
lpReserved2 As Long
hStdInput As Long
hStdOutput As Long
hStdError As Long
End Type

Private Type PROCESS_INFORMATION
hProcess As Long
hThread As Long
dwProcessID As Long
dwThreadID As Long
End Type

Private Const STARTF_USESTDHANDLES = &H100&
Private Const NORMAL_PRIORITY_CLASS = &H20&
Private Const STARTF_USESHOWWINDOW As Long = &H1&

Private Function ExecCmd(cmdline$) As String
Dim proc As PROCESS_INFORMATION
Dim start As STARTUPINFO
Dim sa As SECURITY_ATTRIBUTES
Dim hReadPipe As Long, hWritePipe As Long
Dim L As Long, Result As Long, bSuccess As Long
Dim Buffer As String

Dim retText As String

sa.nLength = Len(sa)
sa.bInheritHandle = 1&
sa.lpSecurityDescriptor = 0&
Result = CreatePipe(hReadPipe, hWritePipe, sa, 0)

If Result = 0 Then
MsgBox "CreatePipe failed Error!"
Exit Function
End If

With start
.cb = Len(start)
.dwFlags = STARTF_USESTDHANDLES Or STARTF_USESHOWWINDOW
.hStdOutput = hWritePipe
.wShowWindow = vbHide ' *** von rkhb: Konsolenfenster nicht zeigen
End With

Result = CreateProcessA(0&, cmdline$, sa, sa, 1&, _
NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc)

If Result <> 0 Then
'*** Anfang der Verbesserung Achim Neubauer***
Dim lPeekData As Long
Do
Call PeekNamedPipe(hReadPipe, ByVal 0&, 0&, ByVal 0&, _
lPeekData, ByVal 0&)
If lPeekData > 0 Then
Buffer = Space$(lPeekData)
bSuccess = ReadFile(hReadPipe, Buffer, Len(Buffer), L, 0&)
If bSuccess = 1 Then
retText = retText & Left(Buffer, L)
Else
MsgBox "ReadFile failed!"
End If
Else
bSuccess = WaitForSingleObject(proc.hProcess, 0&)

If bSuccess = 0 Then
Exit Do
End If
End If

DoEvents
Loop
'*** Ende der Verbesserung Achim Neubauer ***
Else
MsgBox "Error while starting process!"
End If

Call CloseHandle(proc.hProcess)
Call CloseHandle(proc.hThread)
Call CloseHandle(hReadPipe)
Call CloseHandle(hWritePipe)

ExecCmd = retText
End Function

Sub Ping()
Dim s As String
s = ExecCmd("ping.exe -n 1 -w 200 google.de")
If InStr(s, "Antwort von") Then
MsgBox ("Der Zielrechner hat geantwortet")
Else
MsgBox ("Der Zielrechner hat nicht geantwortet")
End If
End Sub

viele grüße
ralph
Thomas Risi
2007-01-25 20:53:43 UTC
Permalink
Post by Carsten Boll
Hi NG,
XL 2000
Wie kann ich den DOS-Befehl PING von VBA aus ausführen und das Ergebnis
auswerten? Ich hatte überlegt, mit 'PING 123.456.789.123 > LOG.TXT' eine
Textdatei zu erzeugen und diese dann einzulesen.
Oder gibt es eine elegantere Lösung, ob ein bestimmter Rechner im Netzwerk
ansprechbar ist?
Danke,
Carsten
Hi Carsten,

schau mal auf meiner Webseite, bei den Beispielen unter 'Ping' nach.
Damit kann ohne das Ping-Programm pingen.

Und wirklich elegant, wäre es mit der RTD-Funktion. Geht aber erst mit
ExcelXP.
--
Gruß
Thomas

http://rtsoftwaredevelopment.de
Frank Vellner
2007-01-26 07:38:04 UTC
Permalink
Moin Carsten,

es gibt eine kleine Freeware, die genau das tut:
http://www.lab1.de/Central/Software/Internet/Webmaster/Ping-o-Meter/
Du kannst die Ping-Intervalle frei einstellen und das Ergebnis wird als
Excel-lesbare Datei abgespeichert. Die Sache läuft im Hintergrund und
zeigt dir bei Bedarf den Ping-Verlauf gratis. Hat aber alles nix mit
Excel zu tun.

ciao
Frank

Loading...