November 17, 201015 yr Hi can anyone help meI 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
November 17, 201015 yr This code is for Windows Server 2003 and Windows XP and up.Save As WmiPing.vbsDim 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 NextI have attach the above code as a text file just remove the.txt to make acrive.
November 18, 201015 yr Author 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.
November 18, 201015 yr 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?
November 18, 201015 yr Author 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.
November 18, 201015 yr 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.vbsDim 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
November 19, 201015 yr Author 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.vbsDim 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 FunctionThank 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?
November 19, 201015 yr 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
November 20, 201015 yr Author 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.vbsstart /wait tmp.vbsstart cidial start /wait tmp.vbs"C:\Program Files\Cisco Systems\VPN Client\vpngui.exe" and second .bat file isecho wscript.sleep 10000 > tmp1.vbsstart /wait tmp1.vbstaskkill /F /IM vpngui.exe /Tstart /wait tmp1.vbs"C:\Program Files\Cisco Systems\VPN Client\vpngui.exe" I might be need both in different situation Edited November 20, 201015 yr by gunsmokingman Added Code Tags
November 20, 201015 yr Here try this script, I havnt coded in the exe stuff yet.Save As WmiPing3.vbsDim 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
November 22, 201015 yr Author Here try this script, I havnt coded in the exe stuff yet.Save As WmiPing3.vbsDim 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 Functionthank you i will test it
Please sign in to comment
You will be able to leave a comment after signing in
Sign In Now