Werbung: SecurityConsole.de verwaltet Ihre Computer mit Security Essentails aus der Cloud!
30 Tage kostenfrei testen und 20% Rabatt für Ihre Bestellung mit Promocode: WBF2685582
(Promocode gültig bis 31.12.2011)

Group:  English: Windows Server » microsoft.public.windows.server.scripting
Thread: Simple Ping Script

HTVi
TV Discussion Newsgroups

Simple Ping Script
"K" <no[ at ]spam.net> 11/17/2008 8:40:02 AM
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.


Re: Simple Ping Script
"Pegasus \(MVP\)" <I.can[ at ]fly.com.oz> 11/17/2008 8:51:13 AM

"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.


Re: Simple Ping Script
"K" <no[ at ]spam.net> 11/17/2008 10:13:24 AM
[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.


Re: Simple Ping Script
Tom Lavedas <tglbatch[ at ]cox.net> 11/17/2008 1:58:17 PM
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
==========
RE: Simple Ping Script
J Ford 11/17/2008 2:12:01 PM
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.
>
>
>
Re: Simple Ping Script
"K" <no[ at ]spam.net> 11/18/2008 7:24:53 AM
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.
>>
>>
>>


Home | Search | Terms | Imprint Contact
Newsgroups Reader - provided by WiredBox.Net
Suche nach Orten, Städten, Postleitzahlen, Vorwahlen, Kfz-Kennzeichen