Jump to content

vbs ping


Recommended Posts

Hi

can anyone help me

I want some simple .vbs script (or something else) witch continuously pings some host and if that host went down (not responding during 10 seconds or 5 timeout) it runs some .bat file (witch takes another action).

thank you in advance

Link to comment
Share on other sites


This code is for Windows Server 2003 and Windows XP and up.

Save As WmiPing.vbs


Dim Wmi :Set Wmi = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Dim Obj, Png
'-> Loop To Get User Input
Do While Png = ""
'-> Inputbox To Get User Input
Png = InputBox( _
"Type in the IP Address or the Computer Name" & vbCrLf & _
"that you want to run the Ping Command on." & vbCrLf & _
"To do nothing type in either Exit Or Quit","Get IP Or Computer Name","",,475)
'-> Exit The Script And Do Nothing
If InStr(1,Png,"exit",1) Or InStr(1,Png,"quit",1) Then
WScript.Quit
End If
Loop
'-> Ping The Computer
For Each Obj In Wmi.ExecQuery _
("Select * From Win32_PingStatus where Address = '" & Png & "'")
If IsNull(Obj.StatusCode) Or Obj.StatusCode <> 0 Then
'-> Code Here If Computer Does Not Replies
MsgBox "Did Not Respond : " & Png, 4128, "No Reply"
Else
'-> Code Here If Computer Replies
MsgBox "Confirm Respond : " & Png, 4128, "Yes Reply"
End If
Next

I have attach the above code as a text file just remove the.txt to make acrive.

Link to comment
Share on other sites

thank you very much Gunsmokingman for your reply but I want code witch continuously pings host, and when it receives "Request timed out" or "Destination host unreachable" takes some action, runs .bat file or some other script witch, for example, plays alarm.

Link to comment
Share on other sites

I want some simple .vbs script (or something else) witch continuously pings some host and if that host went down (not responding during 10 seconds or 5 timeout) it runs some .bat file (witch takes another action).

It would be fairly trivial to write a quick vscript that does precisely this and more. It would take like 5 minutes. Or we could even write even a C# app using the Ping class from the System.Net.NetworkInformation namespace in not much longer (but that can very easily become a significant undertaking as you add features). But why bother when there's already dozens of such things all over the internet available for free such as FREEping and peermonitor (Google will find plenty more). There likely several open source apps to do that too. It's trivial to write, but why reinvent the wheel when there's plenty of existing apps for this?

Link to comment
Share on other sites

I want some simple .vbs script (or something else) witch continuously pings some host and if that host went down (not responding during 10 seconds or 5 timeout) it runs some .bat file (witch takes another action).

It would be fairly trivial to write a quick vscript that does precisely this and more. It would take like 5 minutes. Or we could even write even a C# app using the Ping class from the System.Net.NetworkInformation namespace in not much longer (but that can very easily become a significant undertaking as you add features). But why bother when there's already dozens of such things all over the internet available for free such as FREEping and peermonitor (Google will find plenty more). There likely several open source apps to do that too. It's trivial to write, but why reinvent the wheel when there's plenty of existing apps for this?

Thank you for your attention CoffeeFiend, This programs is very nice but I had explained very badly, OK I have .bat file witch kills my VPN client and runs again, and I want to execute this .bat file when my internal host, witch I connect to threw VPN is unreachable, I want code witch pings and runs .bat file after it receives 10 "timeouts" or "unreachable" or maybe after 10 - 15 seconds when it realizes that host is unreachable.

Link to comment
Share on other sites

I have made some minor changes to the script, it now has a Loop that will exit funtion after 10 unsuccesfull ping tries or 10 succesfull ping.

Save As WmiPing2.vbs


Dim Act :Set Act = CreateObject("wscript.Shell")
Dim Wmi :Set Wmi = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Dim C1, C2, Obj, Png
'-> Loop To Get User Input
Do While Png = ""
'-> Inputbox To Get User Input
Png = InputBox( _
"Type in the IP Address or the Computer Name" & vbCrLf & _
"that you want to run the Ping Command on." & vbCrLf & _
"To do nothing type in either Exit Or Quit","Get IP Or Computer Name","",,475)
'-> Exit The Script And Do Nothing
If InStr(1,Png,"exit",1) Or InStr(1,Png,"quit",1) Then
WScript.Quit
End If
Loop
'-> Start The Ping Loop
PingLoop()
Function PingLoop()
'-> Ping The Computer
For Each Obj In Wmi.ExecQuery _
("Select * From Win32_PingStatus where Address = '" & Png & "'")
If IsNull(Obj.StatusCode) Or Obj.StatusCode <> 0 Then
'-> Code Here If Computer Does Not Replies
C1 = C1 + 1
Act.Popup "Total Ping : " & C1 & vbcrlf & _
"No Respond : " & Png, 3, "No Reply", 4128
'-> Quit After 10 Tries
If C1 = 10 Then WScript.Quit
Else
'-> Code Here If Computer Replies
C2 = C2 + 1
Act.Popup "Total Pings : " & C2 & vbcrlf & _
"Yes Respond : " & Png, 3, "Yes Reply", 4128
'-> Quit After 10 Tries
If C2 = 10 Then WScript.Quit
End If
Next
PingLoop()
End Function

Link to comment
Share on other sites

I have made some minor changes to the script, it now has a Loop that will exit funtion after 10 unsuccesfull ping tries or 10 succesfull ping.

Save As WmiPing2.vbs


Dim Act :Set Act = CreateObject("wscript.Shell")
Dim Wmi :Set Wmi = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Dim C1, C2, Obj, Png
'-> Loop To Get User Input
Do While Png = ""
'-> Inputbox To Get User Input
Png = InputBox( _
"Type in the IP Address or the Computer Name" & vbCrLf & _
"that you want to run the Ping Command on." & vbCrLf & _
"To do nothing type in either Exit Or Quit","Get IP Or Computer Name","",,475)
'-> Exit The Script And Do Nothing
If InStr(1,Png,"exit",1) Or InStr(1,Png,"quit",1) Then
WScript.Quit
End If
Loop
'-> Start The Ping Loop
PingLoop()
Function PingLoop()
'-> Ping The Computer
For Each Obj In Wmi.ExecQuery _
("Select * From Win32_PingStatus where Address = '" & Png & "'")
If IsNull(Obj.StatusCode) Or Obj.StatusCode <> 0 Then
'-> Code Here If Computer Does Not Replies
C1 = C1 + 1
Act.Popup "Total Ping : " & C1 & vbcrlf & _
"No Respond : " & Png, 3, "No Reply", 4128
'-> Quit After 10 Tries
If C1 = 10 Then WScript.Quit
Else
'-> Code Here If Computer Replies
C2 = C2 + 1
Act.Popup "Total Pings : " & C2 & vbcrlf & _
"Yes Respond : " & Png, 3, "Yes Reply", 4128
'-> Quit After 10 Tries
If C2 = 10 Then WScript.Quit
End If
Next
PingLoop()
End Function

Thank you, very nice code, it wants little improvement, could you do such thing? after it receives 10 unsuccessful PING to take another action , I want it to run a .bat file and could successful PINGs continue forever?

Link to comment
Share on other sites

Could you post the contents of the bat file, I might be able to code it so it does not need the bat file.

To make the script Ping without quiting, remove these lines from the script.

'-> Quit After 10 Tries 
If C2 = 10 Then WScript.Quit

this is content of .bat file,

first:


echo wscript.sleep 5000 > tmp.vbs
start /wait tmp.vbs
start cidial
start /wait tmp.vbs
"C:\Program Files\Cisco Systems\VPN Client\vpngui.exe"

and second .bat file is


echo wscript.sleep 10000 > tmp1.vbs
start /wait tmp1.vbs
taskkill /F /IM vpngui.exe /T
start /wait tmp1.vbs
"C:\Program Files\Cisco Systems\VPN Client\vpngui.exe"

I might be need both in different situation

Edited by gunsmokingman
Added Code Tags
Link to comment
Share on other sites

Here try this script, I havnt coded in the exe stuff yet.

Save As WmiPing3.vbs


Dim Act :Set Act = CreateObject("wscript.Shell")
Dim Wmi :Set Wmi = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Dim C1, C2, Obj, Png
'-> Loop To Get User Input
Do While Png = ""
'-> Inputbox To Get User Input
Png = InputBox( _
"Type in the IP Address or the Computer Name" & vbCrLf & _
"that you want to run the Ping Command on." & vbCrLf & _
"To do nothing type in either Exit Or Quit","Get IP Or Computer Name","",,475)
'-> Exit The Script And Do Nothing
If InStr(1,Png,"exit",1) Or InStr(1,Png,"quit",1) Then
WScript.Quit
End If
Loop
'-> Start The Ping Loop
PingLoop()
Function PingLoop()
'-> Ping The Computer
For Each Obj In Wmi.ExecQuery _
("Select * From Win32_PingStatus where Address = '" & Png & "'")
If IsNull(Obj.StatusCode) Or Obj.StatusCode <> 0 Then
'-> Code Here If Computer Does Not Replies
C1 = C1 + 1
call ShowMessage(C1, "No Respond : ", "No Reply")
'-> Quit After 10 Tries
If C1 = 10 Then
WScript.Quit
End If
Else
'-> Code Here If Computer Replies
C2 = C2 + 1
call ShowMessage(C2, "Yes Respond : ", "Yes Reply")
End If
Next
PingLoop()
End Function
'-> Function For Yes Responce Or No Responce From Ping
Function ShowMessage(N, Tx1, Tx2)
'-> If No Button Is Press The Script
'-> Stops For 15 Seconds, Then Continues
If Act.Popup( _
"Total Pings : " & N & vbcrlf & _
Tx1 & Png & vbcrlf & _
"To Exit Or Quit Press Yes", 5, Tx2, 4132) = 6 Then
WScript.Quit
Else
WScript.Sleep 10000
End If
End Function

Link to comment
Share on other sites

Here try this script, I havnt coded in the exe stuff yet.

Save As WmiPing3.vbs


Dim Act :Set Act = CreateObject("wscript.Shell")
Dim Wmi :Set Wmi = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Dim C1, C2, Obj, Png
'-> Loop To Get User Input
Do While Png = ""
'-> Inputbox To Get User Input
Png = InputBox( _
"Type in the IP Address or the Computer Name" & vbCrLf & _
"that you want to run the Ping Command on." & vbCrLf & _
"To do nothing type in either Exit Or Quit","Get IP Or Computer Name","",,475)
'-> Exit The Script And Do Nothing
If InStr(1,Png,"exit",1) Or InStr(1,Png,"quit",1) Then
WScript.Quit
End If
Loop
'-> Start The Ping Loop
PingLoop()
Function PingLoop()
'-> Ping The Computer
For Each Obj In Wmi.ExecQuery _
("Select * From Win32_PingStatus where Address = '" & Png & "'")
If IsNull(Obj.StatusCode) Or Obj.StatusCode <> 0 Then
'-> Code Here If Computer Does Not Replies
C1 = C1 + 1
call ShowMessage(C1, "No Respond : ", "No Reply")
'-> Quit After 10 Tries
If C1 = 10 Then
WScript.Quit
End If
Else
'-> Code Here If Computer Replies
C2 = C2 + 1
call ShowMessage(C2, "Yes Respond : ", "Yes Reply")
End If
Next
PingLoop()
End Function
'-> Function For Yes Responce Or No Responce From Ping
Function ShowMessage(N, Tx1, Tx2)
'-> If No Button Is Press The Script
'-> Stops For 15 Seconds, Then Continues
If Act.Popup( _
"Total Pings : " & N & vbcrlf & _
Tx1 & Png & vbcrlf & _
"To Exit Or Quit Press Yes", 5, Tx2, 4132) = 6 Then
WScript.Quit
Else
WScript.Sleep 10000
End If
End Function

thank you i will test it

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...