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

Don't know how to search for an occurance 2

Status
Not open for further replies.

miteshlad2003

Technical User
Oct 25, 2003
42
GB
Hi,

I've trying to read the output from a ping. The out is like this in a textfile:

Packets: Sent = 5, Received = 0, Lost = 5 (100% loss),

or

Packets: Sent = 5, Received = 0, Lost = 30 (100% loss),

I want to see what the value is of lost each time. If its over 25 then I 'll issue a wscript to the screen to say the server is down...otherwise a message to say the server is OK.

Thanks



 
This:

Dim oShell, StrLine
Dim objFile, objFSO
dim Count

Count = 0

Set oShell = WScript.CreateObject ("WScript.shell")
oShell.Run "cmd /c ping -n 5 10.50.50.40 | find " & Chr(34) & "Lost" & Chr(34) & " >

C:\Mitesh\scripts\backups\socbackup1.txt"
Wscript.sleep 29500
Set oShell = Nothing

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\Mitesh\scripts\backups\socbackup1.txt", 1)
Do Until objFile.AtEndOfStream
strLine = objFile.ReadLine
Count = strLine
Loop
objFile.Close

Wscript.Echo " The Value of Count is now = " & Count

If Count > 3 then
Wscript.Echo "Server is Down"
else
Wscript.Echo "Server is OK"
end if


Thanks

 
Something like this ?
Set oShell = WScript.CreateObject ("WScript.shell")
Set oExec = oShell.Exec("ping -n 5 10.50.50.40")
Do While oExec.Status = 0
WScript.Sleep 100
Loop
pLost = 100
Do While Not oExec.StdOut.AtEndOfStream
strLine = oExec.StdOut.ReadLine
iPos = InStr(strLine, "% los")
If iPos > 1 Then
a = Split(Left(strLine, iPos - 1), "(")
pLost = a(UBound(a))
Exit Do
End If
Loop
Wscript.Echo " The percentage of loss is now = " & pLost
If pLost > 25 then
Wscript.Echo "Server is Down"
Else
Wscript.Echo "Server is OK"
End If

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
PERFECT!!!...Thanks ever so much

Have a good easter!

Miteshlad2003
 
Just one quick question though, I need to enter a list of 6 servers in here. instead of repeating the code six times, how could i do this using a simple array instead with the servers in there?

Thanks
 
I would put the whole thing into a sub then in the main body loop through the array of servers passing each one into the sub.
 
TomThumbKP,

You think i can do that??? The code I wrote above took me 2 days!!!!! :)

I need help and thats why i'm in here....

Thanks
 
Code:
Option Explicit
Dim arrServers, i
arrServers = Array("Server1", "Server2", "Server3", "Server4", "Server5", "Server6")

For i=0 To UBound(arrServers)
	CheckTheServer(arrServers(i))
Next



Sub CheckTheServer(strServer)
	WScript.Echo strServer
End Sub

Replace the servernames above with the ip addresses of the servers you want to check. Put your code that checks the servers into the CheckTheServer subroutine. in your checking code, replace:
Set oExec = oShell.Exec("ping -n 5 10.50.50.40")
with
Set oExec = oShell.Exec("ping -n 5 " & strServer)

Let me know if there are problems.
 
Thanks for that!.

but i am still getting errors:

this is what I have:

Dim arrServers, i
arrServers = Array("socbackup1", "10.50.50.50", "socbackup2")

For i=0 To UBound(arrServers)
CheckTheServer(arrServers(i))
Next



Sub CheckTheServer(strServer)
WScript.Echo strServer

Set oShell = WScript.CreateObject ("WScript.shell")
Set oExec = oShell.Exec("ping -n 5 " & strServer)
Do While oExec.Status = 0
WScript.Sleep 100
Loop
pLost = 100

Do While Not oExec.StdOut.AtEndOfStream
strLine = oExec.StdOut.ReadLine
iPos = InStr(strLine, "% los")
If iPos > 1 Then
a = Split(Left(strLine, iPos - 1), "(")
pLost = a(UBound(a))
Exit Do
End If
Loop

Wscript.Echo " The Percentage of loss is now = " & pLost

If pLost > 25 then
Wscript.Echo "Server is Down"
Else
Wscript.Echo "Server is OK"
End If

END SUB

call CheckTheServer(strServer)

 
What error are you getting? Also, why did you put call CheckTheServer(strServer) at the end?
 
but i am still getting errors
Any chance you could post the whole error message ?

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
Its ok, I had a typo its working like a dream, thanks TomThumbKP for your help!!!
 
No problem. It seems like this is something that you want to keep running all the time. The way it is written right now, it will only check all of the servers one time then quit. To keep it running forever, one solution would be:

Do While (True)
For i=0 To UBound(arrServers)
CheckTheServer(arrServers(i))
Next
Loop
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top