|
|
What are the errorlevels for the ping command?
I am trying to create a script that will ping a remote router, and in the event it is non-responsive, will cause the machine the script is running on to reboot without warnings or prompts.
Any help much appreciated.
|
|
"K" <no[ at ]spam.net> wrote in message news:ON0OqAJSJHA.4212[ at ]TK2MSFTNGP03.phx.gbl...
[Quoted Text] > What are the errorlevels for the ping command? > > I am trying to create a script that will ping a remote router, and in the > event it is non-responsive, will cause the machine the script is running > on to reboot without warnings or prompts. > > Any help much appreciated.
Ping does not have any specific error levels. You need to check its output in order to work out if the remote computer is responding, e.g. like so:
ping SomePC | find /i "bytes=" > nul && echo SomePC is responding. or maybe ping SomePC | find /i "bytes=" > nul || echo SomePC is not responding.
|
|
|
[Quoted Text] >> What are the errorlevels for the ping command? >> >> I am trying to create a script that will ping a remote router, and in the >> event it is non-responsive, will cause the machine the script is running >> on to reboot without warnings or prompts. >> >> Any help much appreciated. > > Ping does not have any specific error levels. You need to check its output > in order to work out if the remote computer is responding, e.g. like so: > > ping SomePC | find /i "bytes=" > nul && echo SomePC is responding. > or maybe > ping SomePC | find /i "bytes=" > nul || echo SomePC is not responding.
After some googling I have found mention of an errorlevel 1 for a fail and 0 for a result but they seem hit and miss. Certainly not something I am that confident on trusting a reboot to.
|
|
On Nov 17, 5:13 am, "K" <n...[ at ]spam.net> wrote:
[Quoted Text] > >> What are the errorlevels for the ping command? > > >> I am trying to create a script that will ping a remote router, and in the > >> event it is non-responsive, will cause the machine the script is running > >> on to reboot without warnings or prompts. > > >> Any help much appreciated. > > > Ping does not have any specific error levels. You need to check its output > > in order to work out if the remote computer is responding, e.g. like so: > > > ping SomePC | find /i "bytes=" > nul && echo SomePC is responding. > > or maybe > > ping SomePC | find /i "bytes=" > nul || echo SomePC is not responding.. > > After some googling I have found mention of an errorlevel 1 for a fail and 0 > for a result but they seem hit and miss. Certainly not something I am that > confident on trusting a reboot to.
Pegasus shows how to test the PING result in a batch procedure. Below is a function, IsConnectible that returns True or False based on the PING response. It uses a test similar to Pegasus', but wraps script code around it.
Function IsConnectible(sHost, iPings, iTO) ' Returns True or False based on the output from ping.exe ' ' Authors: Alex Angelopoulos/Torgeir Bakken ' Modified by: Tom Lavedas ' Works an "all" WSH versions ' sHost is a hostname or IP ' iPings is number of ping attempts ' iTO is timeout in milliseconds ' if values are set to "", then defaults below used
Dim nRes If iPings = "" Then iPings = 1 ' default number of pings If iTO = "" Then iTO = 250 ' default timeout per ping with CreateObject("WScript.Shell") nRes = .Run("%comspec% /c ping.exe -n " & iPings & " -w " & iTO _ & " " & sHost & " | find ""TTL="" > nul 2>&1", 0, True) end with IsConnectible = (nRes = 0) End Function
The other half of the problem - the reboot - can be accomplished thus ...
Const Shutdown = 1, Forced = 4, Reboot = 2 strComputer = "." ' Local Computer Set oWMISrvc = GetObject(_ "winmgmts:{impersonationLevel=impersonate,(Shutdown)}\\" _ & strComputer & "\root\cimv2")
Set cO = oWMISrvc.ExecQuery(_ "Select * from Win32_OperatingSystem")
For Each oI in cO oI.Win32Shutdown Forced + Reboot Next
So wrap these two pieces something like this ...
if IsConnectible(sYourRouterIP, nPings, nWaitms) then ' do nothing else ' Reboot code from above end if
Tom Lavedas ==========
|
|
The problem I see with the script, isn't with doing the logic. BUT, if you are not able to get a response from pinging, chances are you will not be able to send a remote reboot either.
"K" wrote:
[Quoted Text] > What are the errorlevels for the ping command? > > I am trying to create a script that will ping a remote router, and in the > event it is non-responsive, will cause the machine the script is running on > to reboot without warnings or prompts. > > Any help much appreciated. > > >
|
|
It;s a local reboot - various reasons why but if I cannot contact a remore router I want to bounce the local machine.
[Quoted Text] > The problem I see with the script, isn't with doing the logic. BUT, if > you > are not able to get a response from pinging, chances are you will not be > able > to send a remote reboot either. > > "K" wrote: > >> What are the errorlevels for the ping command? >> >> I am trying to create a script that will ping a remote router, and in the >> event it is non-responsive, will cause the machine the script is running >> on >> to reboot without warnings or prompts. >> >> Any help much appreciated. >> >> >>
|
|
|