×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

ping computer before vbscript execution

ping computer before vbscript execution

ping computer before vbscript execution

(OP)
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.

RE: ping computer before vbscript execution

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

RE: ping computer before vbscript execution

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

RE: ping computer before vbscript execution

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

RE: ping computer before vbscript execution

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

RE: ping computer before vbscript execution

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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close