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!

VBScript Ping

Status
Not open for further replies.

josephk73

Technical User
Aug 1, 2002
115
GB
Guys,

I am trying to write a program that will send a ping packet to a server and then read the reply. Depending on whether the reply contains the word "reply" ( as in reply from xxxxxxx)or "request" (as in request timed out) I will then take the appropriate action.

So far I have managed to get some of it to work, whereby I use a select or an IF statement which looks for the word 'reply' and then the script takes the action I want. BUT if I use a second clause to the IF or SELECT statement to check the second condition (ie 'request' in the ping reply) nothing happens.

My script is as follows (I've only included the relevant part)


Sub Pinger

Pinger = False
Set objShell = WScript.CreateObject("WScript.Shell")
Set objExecObject=objShell.Exec("cmd /k ping -n 1 -w 1000" & " " & strComputer)

Do While Not objExecObject.StdOut.AtEndOfStream
strText = objExecObject.StdOut.ReadLine()
Select Case strText
Case 1 Instr(readOut) = "Reply"
Wscript.Echo "Reply received."
Call IEVER
Case 2 Instr(readOut) = "Request"
Call Offline
Wscript.Echo "machine off"
End Select


Loop
End Sub


So to re-cap, the script runs as intended if the machine is online, but if the machine is offline, the script dosent branch off to the second case statement and call the subroutine Offline. Instead it seeems to ignore the second case statement alltogether and just runs the first case statement, thus calling the incorrect sub-routine.

Just to add a little bit more confusion, if I try using an IF...THEN...ELSE statement like in the following the script, it just hangs when it reaches a machine that is offline.


function Pinger
'Pinger = False
Set objShell = WScript.CreateObject("WScript.Shell")
Set objExecObject=objShell.Exec("cmd /k ping -n 1 -w 1000" & " " & strComputer)

Do While Not objExecObject.StdOut.AtEndOfStream
strText = objExecObject.StdOut.ReadLine()
If Instr(strText, "Reply") > 0 Then
Wscript.Echo "Reply received."
Call PingResult
Else
Wscript.Echo "Machine Offline"
Exit Do
End If
Loop
End Function

However if I omit the Else statement in the same script above, things work fine. But of course I am then faced with the problem of not being able to take any action if the machine is offline.

Any help you guys can give would be really great.
 
Hello josephk73,

> Case 1 Instr(readOut) = "Reply"
> Case 2 Instr(readOut) = "Request"
These uses of instr() are of wrong syntax.

regards - tsuji
 
HelpFile said:
InStr Function

Returns the position of the first occurrence of one string within another.

InStr([start, ]string1, string2[, compare])
Arguments
start
Optional. Numeric expression that sets the starting position for each search. If omitted, search begins at the first character position. If start contains Null, an error occurs. The start argument is required if compare is specified.
string1
Required. String expression being searched.
string2
Required. String expression searched for.
compare
Optional. Numeric value indicating the kind of comparison to use when evaluating substrings. See Settings section for values. If omitted, a binary comparison is performed.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
You can use WMI WIN32_PingStatus In you subtroutine. Based On the reply you can complete the scripts Next steps

Sub Pinger

Pinger = False
Set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}")._
ExecQuery("select * from Win32_PingStatus where address = '"_
& strComputer & "'")

For Each objStatus in objPing

If IsNull(objStatus.StatusCode) Or objStatus.StatusCode<> 0 Then
Call Offline
WScript.Echo "machine off"
Else
Call IEVER
WScript.Echo "Reply Received"
End If
Next


End Sub




Good Luck!!!

Horace
 
- A Supplementary Note -

Just think it is worthwhile to make a supplementary note on win32_pingstatus as pointed out in horecer's posting. It is supported by winxp/server2003 up.

- tsuji
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top