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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

help with ping script

Status
Not open for further replies.

n3tw0rkadm1n1strat0r

IS-IT--Management
Aug 29, 2006
119
US
What am I doing wrong here? The script doesn't error out...

Code:
Dim strComputer, objShell
strComputer = computername
set objShell = CreateObject("WScript.Shell")
objShell.Run("%comspec% /K ping "" & strComputer & "" -t"), 0, True
If objShell = "Request timed out." then
msgbox "& strComputer & is now shutdown!"
Else
End if
 
First off this line:
"%comspec% /K ping "" & strComputer & "" -t"

should be more like this:
"%comspec% /K ping " & strComputer & " -t"

Secondly, you can't read the output using the Run method, you need to use the Exec method.



--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
example:

Code:
If Reachable("10.50.138.48") Then
 WScript.Echo "Computer is Reachable!"
Else
 WScript.Echo "Computer is Unreachable!"
End If

Function Reachable(strComputer)
'     On Error Resume Next

 Dim objShell, objExec, strCmd, strTemp
 
 strCmd = "ping -n 1 " & strComputer
 
 Set objShell = CreateObject("WScript.Shell")
 Set objExec = objShell.Exec(strCmd)
 strTemp = UCase(objExec.StdOut.ReadAll)
 
 If InStr(strTemp, "REPLY FROM") Then
    Reachable = True 
 Else
    Reachable = False
 End If
End Function

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Here is a bit of code I got from somewhere and amended it a bit. It uses vbscript to do the ping instead of relying on PING.EXE:

Code:
Option Explicit

Dim strHost

strHost = InputBox("Enter Computer Name","Computer name")

if Ping(strHost) = True then
    Wscript.Echo "Host " & strHost & " contacted"
Else
    Wscript.Echo "Host " & strHost & " could not be contacted"
end if

Function Ping(strHost)

    dim objPing, objRetStatus

    set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery _
      ("select * from Win32_PingStatus where address = '" & strHost & "'")

    for each objRetStatus in objPing
        if IsNull(objRetStatus.StatusCode) or objRetStatus.StatusCode<>0 then
    Ping = False
            'WScript.Echo "Status code is " & objRetStatus.StatusCode
        else
            Ping = True
            'Wscript.Echo "Bytes = " & vbTab & objRetStatus.BufferSize
            'Wscript.Echo "Time (ms) = " & vbTab & objRetStatus.ResponseTime
            'Wscript.Echo "TTL (s) = " & vbTab & objRetStatus.ResponseTimeToLive
        end if
    next
End Function

--------------------------------------
"Insert funny comment in here!"
--------------------------------------
 
Thanks for all the replies...I guess I was doing this completly wrong. I like using the vbscript ping method insdead of ping.exe...but how do I do a constant ping with it untill the ping is false or not reachable?
 
I was going to post an example of the Win32_PingStatus method, but seeing how not everyone uses WinXP...posted the old method of doing it.

If you want to put it into a loop then something like this....

Do While Ping("computername")
WScript.Sleep 1000
Loop

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Hmmm not quit sure I know what your talking about...How would that stop the continuous ping once it has found a "Request timed out"?
 
It's only going to be checking 1 computer...and it will have to be a continuous ping untill it is unreachable...I don't see how sleep will do that.
 
Have you tried it? Being in a loop, you want something to slow it down or it will lock your PC. The ping function returns true or false. So as long as it is reachable, the loop will continue and then exit when it is no longer reachable allowing the rest of your script to continue.

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Ahh ok I understand now....would it be like this?

Code:
Do While Ping(strHost) = False
     WScript.Sleep 1000
Loop 

if Ping(strHost) = False then
    Wscript.Echo "Host " & strHost & " is now shutdown!"
Else
end if
 
No, should be more like this.

Code:
Do While Ping(strHost)
	WScript.Sleep 1000
Loop

WScript.Echo "pc is now unreachable"

Function Ping(strHost)
    Dim objPing : Set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery _
      ("select * from Win32_PingStatus where address = '" & strHost & "'")
	
	Dim objRetStatus
	For Each objRetStatus In objPing
		If IsNull(objRetStatus.StatusCode) Or objRetStatus.StatusCode<>0 Then
			Ping = False
		Else
			Ping = True
		End If
	Next
End Function

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top