Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ping computer before vbscript execution

Status
Not open for further replies.

brian32

Vendor
Mar 20, 2005
35
0
0
US
Hi.

I need my script to make sure the remote computer is on the network before the rest of the script executes. If it can't ping the computer, then I need a popup box saying that the remote computer is not available. How would I do this?

Thanks.
 
brian32,
is the box you are looking for static or dhcp?
if static then why not just put a simple ping command that writes what is returned to another file into a .bat job.
read in the result. if it's what you need then do your work if not then pop up the messag box.
regards,
longhair
 
brian32,
try this:

On Error Resume Next

Dim strTarget, strPingResults
strTarget = "198.162.1.2" 'IP address or hostname

Set WshShell = WScript.CreateObject("WScript.Shell")
Set WshExec = WshShell.Exec("ping -n 3 -w 2000 " & strTarget) 'send 3 echo requests, waiting 2secs each
strPingResults = LCase(WshExec.StdOut.ReadAll)
If InStr(strPingResults, "reply from") Then
WScript.Echo strTarget & " responded to ping."
Else
WScript.Echo strTarget & " did not respond to ping."
End If

regards,
bertl
 
Try this one, as a function then call it. Reason is different versions of Ping (Window2000 vs. XP) work differently and some versions make ping open up cmd prompt to do it. This one doesn't show the user anything and work in all version from Windows 2003 down to Windows 98


If IsAlive("WORKSTATION1" then
'do something here
end if


Function IsAlive(strHost)
'---------- Test to see if host or url alive through ping -----------------
' Returns True if Host responds to ping
'
' Though there are other ways to ping a computer, Win2K,
' XP and different versions of PING return different error
' codes. So the only reliable way to see if the ping
' was sucessful is to read the output of the ping
' command and look for "TTL="
'
' strHost is a hostname or IP
Const OpenAsASCII = 0
Const FailIfNotExist = 0
Const ForReading = 1
Dim objShell, objFSO, sTempFile, fFile
Set objShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
sTempFile = objFSO.GetSpecialFolder(2).ShortPath & "\" & objFSO.GetTempName
objShell.Run "%comspec% /c ping.exe -n 2 -w 500 " & strHost & ">" & sTempFile, 0 , True
Set fFile = objFSO.OpenTextFile(sTempFile, ForReading, FailIfNotExist, OpenAsASCII)
Select Case InStr(fFile.ReadAll, "TTL=")
Case 0
IsAlive = False
Case Else
IsAlive = True
End Select
fFile.Close
objFSO.DeleteFile(sTempFile)
Set objFSO = Nothing
Set objShell = Nothing
End Function

Phil Gordemer
ARH Associates
 
Here is another simple way to do it. Not sure it accounts for the things that gordemer's solution addresses, but it has worked for me.

Code:
Set WshShell = CreateObject("WScript.Shell")
PINGFlag = Not CBool(WshShell.run("ping -n 1 " & vSystemIdentifier,0,True))
      If PINGFlag = True Then
	     'Successful ping
      Else
	     'Unsuccessful ping
      End If

strebor
 
wmi also has a Win32_Ping class which might be of use, if not then checking of return code from ping.exe is the most obvious
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top