Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Creating a "ping alert" tool

31 views
Skip to first unread message

Cryptogr...@yahoo.com

unread,
May 18, 2006, 4:38:46 PM5/18/06
to
I would like to create a batch file that will alert me when a computer
is successfully pinged. It could play a sound or some sort of visual
alert.

The tool would start by asking for the IP address to be pinged. Would
then ping that IP until it receives a successful response at which
point would alert the user. This is what I have so far.

@echo off
set /p ip=Type the IP address or hostname:
ping %ip%

This works for pinging a PC once. Or I could add the -t switch and
it would ping indefinitely but it will not alert me when it is
successful. Any suggestions?

Message has been deleted
Message has been deleted
Message has been deleted

Cryptogr...@yahoo.com

unread,
May 18, 2006, 8:55:38 PM5/18/06
to
I'm sorry I can't seem to make it work I don't really understand
VB script either. Why don't we start with something simpler like
using the FOR /F on ping. I know it looks something like this

FOR /F "tokens= delims=" %%A IN ('ping 127.0.0.1') DO set
alive=%%A

What I would like to do is set the words Reply from 127.0.0.1 (or
request timed out) as a variable. Then I could simply use something
like.

If "%alive%"=="reply from" (do something) else goto start

Message has been deleted

zackrspv

unread,
May 19, 2006, 12:53:53 AM5/19/06
to
Alright, after much refinement (and deleting of my other posts), here's
what we are going to do:

1. Set a variable from the user (automatically, and if not provided
asking for one) for the PCNAME.
2. Pings PCNAME and returns either a 1 or a 0 if it is 'online' or
'offline'.
3. If PCNAME drops offline, opens a popup box saying it is offline.
4. If PCNAME is online, opens a popup box saying it is online.

We are going to accomplish this mainly by use of WSH and CMD language.

Tested on Win XP SP2:

===Script: PNG.cmd===
001 @echo off
002 :top
003 if "%~1"=="" (
004 set /p pcname=Enter PC Name/IP Address:
005 if /i "%pcname%"=="" goto :top
006 goto :test
007 )
008 set pcname=%1
009 :test
010 for /f "tokens=1 delims= " %%a in ('ping -n 1 -w 900 %pcname% ^|
find /i /c "ttl"') do set online=%%a
011 if /i "%online%" GEQ "1" goto :online
012 goto :offline
013 :online
014 echo WScript.Echo "Computer: %PCNAME% -- Is Online!">pop.vbs
015 start pop.vbs
016 ping -n 9 localhost > nul
017 goto :test
018 :offline
019 echo WScript.Echo "Computer:%PCNAME% -- is OFFLINE!">pop.vbs
020 start pop.vbs
021 ping -n 9 localhost > Nul
022 goto :test
====End: PNG.cmd===

Now, you see what is happening? Whenever the ping request goes out
every 10 seconds, it pop's up a display box for the computer being
ONLINE or OFFLINE. I wrote it this way to show you how it would work
to display a popup (LIke you asked) for the condition desired.

YOU Just ned to tailor your script to what you want to see. So, for
example, to just make it popup when the computer comes online...

===Script: PNG.cmd===
001 @echo off
002 :top
003 if "%~1"=="" (
004 set /p pcname=Enter PC Name/IP Address:
005 if /i "%pcname%"=="" goto :top
006 goto :test
007 )
008 set pcname=%1
009 :test
010 for /f "tokens=1 delims= " %%a in ('ping -n 1 -w 900 %pcname% ^|
find /i /c "ttl"') do set online=%%a
011 if /i "%online%" GEQ "1" goto :online
012 goto :test
013 :online
014 echo WScript.Echo "Computer: %PCNAME% -- Is Online!">pop.vbs
015 start pop.vbs
016 ping -n 9 localhost > nul
017 goto :test
===End: PNG.cmd===

And, if you only want it to popup when the computer goes OFFLINE:

===Script: PNG.cmd===
001 @echo off
002 :top
003 if "%~1"=="" (
004 set /p pcname=Enter PC Name/IP Address:
005 if /i "%pcname%"=="" goto :top
006 goto :test
007 )
008 set pcname=%1
009 :test
010 for /f "tokens=1 delims= " %%a in ('ping -n 1 -w 900 %pcname% ^|
find /i /c "ttl"') do set online=%%a
011 if /i "%online%" LEQ "0" goto :offline
012 goto :test
013 :offline
014 echo WScript.Echo "Computer:%PCNAME% -- is OFFLINE!">pop.vbs
015 start pop.vbs
016 ping -n 9 localhost > Nul
017 goto :test
====End: PNG.cmd===

Now, line 014 in both situations, which stars pop.vbs can be changd to
start /wait pop.vbs if you want to click 'ok' before the sript retests.
In that situation, i would alter your scrip to look like the below:

Test if OFFLINE:

===Script: PNG.cmd===
001 @echo off
002 :top
003 if "%~1"=="" (
004 set /p pcname=Enter PC Name/IP Address:
005 if /i "%pcname%"=="" goto :top
006 goto :test
007 )
008 set pcname=%1
009 :test
010 for /f "tokens=1 delims= " %%a in ('ping -n 1 -w 900 %pcname% ^|
find /i /c "ttl"') do set online=%%a
011 if /i "%online%" LEQ "0" goto :offline
012 goto :test
013 :offline
014 echo WScript.Echo "Computer:%PCNAME% -- is OFFLINE!">pop.vbs
015 start /wait pop.vbs
016 goto :test
====End: PNG.cmd===

And, if you want to test for an ONLINE condition:

===Script: PNG.cmd===
001 @echo off
002 :top
003 if "%~1"=="" (
004 set /p pcname=Enter PC Name/IP Address:
005 if /i "%pcname%"=="" goto :top
006 goto :test
007 )
008 set pcname=%1
009 :test
010 for /f "tokens=1 delims= " %%a in ('ping -n 1 -w 900 %pcname% ^|
find /i /c "ttl"') do set online=%%a
011 if /i "%online%" GEQ "1" goto :online
012 goto :test
013 :online
014 echo WScript.Echo "Computer: %PCNAME% -- Is Online!">pop.vbs
015 start /wait pop.vbs
016 goto :test
====End: PNG.cmd===

Now, in either event, if you want to only do the PING UNTIL the
condition you want is met, you can remove line 015. This way the
script ends after the conditional popup is met.

Make sense?

zackrspv

unread,
May 19, 2006, 1:05:04 AM5/19/06
to
>Now, in either event, if you want to only do the PING UNTIL the
condition you want is met, you can remove line 015. This way the
script ends after the conditional popup is met.

Er, that should be line 016 lol it's 1156pm and i'm a bit tired.

I have tested all of these on my system and they work perfectly well.
So, make sure you follow the basic rules:

1. New lines start with a line number (0XX)
2. Wrapped lins do not start witha line number; you should merge these
onto the previous line.
3. REMOVE LINE NUMBERS before running the script.

This script creates a .vbs file, which under windows will open (by
default unles you specify cscript) will open using wscript. If you
don't have windows scripting (for some odd reason) on your system
(which seems weird to me) then the WScript.Echo inside the POP.vbs
won't work for you. In that situation, we can try something else.

If all you want is some form of other feedback, consider just using a
beep.

Like, for example, the below script, which tests when a computer has
dropped offline:

===Script: PNG.cmd===
001 @echo off
002 :top
003 if "%~1"=="" (
004 set /p pcname=Enter PC Name/IP Address:
005 if /i "%pcname%"=="" goto :top
006 goto :test
007 )
008 set pcname=%1
009 :test
010 for /f "tokens=1 delims= " %%a in ('ping -n 1 -w 900 %pcname% ^|
find /i /c "ttl"') do set online=%%a
011 if /i "%online%" LEQ "0" goto :offline
012 goto :test
013 :offline

014 echo &&echo %PCNAME% is OFFLINE!!!
015 goto :test
====End: PNG.cmd===

Note line 014 there, it has the special character ^G, which is CONTROL
G, or BEEP to cmd.exe. It is created (at least easilly for me) by
opening up the command line, and holding down the CTRL key and pressing
G. You can echo that to a temporary file and copy the contents of the
file into the clipboard and paste it on whatever line after the 'echo'
command to make the speakers beep.

in the above script, if the computer goes offline, the computer will
beep. Now, note that it will beep quite fast as there is no delay
between line 014 and 015. If you want, you can input a 10 second delay
by inserting another line between 014 and 015 to read as follows:

014a ping -n 9 -w 900 localhost > nul

So, line 014-016 should look like this:

014 echo &&echo %PCNAME% is OFFLINE!!!
015 ping -n 9 -w 900 localhost > nul
016 goto :test

That will give you AUDIABLE feedback. The first set of scripts gave
you visual with a popup box.

Si Ballenger

unread,
May 19, 2006, 11:48:01 AM5/19/06
to

The below is adapted from prevously posted info. It shows some
options you could use (watch for line wrap on the ringin.wav).

@echo off
::ping -n 1 127.0.0.1 |find "timed out" >nul
ping -n 1 213.176.76.28 |find "timed out" >nul
if not errorlevel 1 echo off line
if not errorlevel 1 echo off line >ping.txt
if not errorlevel 1 start /min sndrec32.exe /play /close
%windir%\media\ringin.wav
if errorlevel 1 echo on line
if errorlevel 1 echo on line >ping.txt
pause

foxidrive

unread,
May 19, 2006, 12:06:47 PM5/19/06
to
On Fri, 19 May 2006 15:48:01 GMT, Si Ballenger wrote:

> On 18 May 2006 13:38:46 -0700, Cryptogr...@yahoo.com wrote:
>
>>I would like to create a batch file that will alert me when a computer
>>is successfully pinged. It could play a sound or some sort of visual
>>alert.
>>
>>The tool would start by asking for the IP address to be pinged. Would
>>then ping that IP until it receives a successful response at which
>>point would alert the user. This is what I have so far.
>>
>>@echo off
>>set /p ip=Type the IP address or hostname:
>>ping %ip%
>>
>>This works for pinging a PC once. Or I could add the -t switch and
>>it would ping indefinitely but it will not alert me when it is
>>successful. Any suggestions?
>
> The below is adapted from prevously posted info. It shows some
> options you could use (watch for line wrap on the ringin.wav).
>

And I adapted Si's code: Searching for TTL= is one sure way to know that
the IP address is online.

I took out the logging, Si, as the OP didn't mention it.

@echo off
set /p "ip=Type the IP address or hostname: "

:loop
ping -n 1 %ip% |find "TTL=" >nul
if errorlevel 1 echo off line
if not errorlevel 1 (


start /min sndrec32.exe /play /close "%windir%\media\ringin.wav"

echo on line
goto :EOF
)
goto :loop

zackrspv

unread,
May 19, 2006, 12:08:12 PM5/19/06
to
Now, i just have to comment on Si's script. I'd do it like the below,
which makes more sense to me, instead of having to check for all of
those errorlevels.

===Script: 2Png.cmd===


001 @echo off
002 :top

003 if "%~1"=="" (set /p pcname=Enter PC Name/IP Address:


005 if /i "%pcname%"=="" goto :top
006 goto :test
007 )
008 set pcname=%1
009 :test

010 ping -n 1 %pcname% | find /i "timed out" > nul
011 if NOT ERRORLEVEL 1 (echo %PCNAME% -- Offline
012 echo Computer: %PCname% -- Offline >>ping.txt
013 start /min sndrec32.exe /play /close %windir%\media\ringin.wav
014 )
015 If ERRORLEVEL 1 (echo %PCNAME% -- Online!
016 echo Computer: %PCNAME% -- Online >> ping.txt
017 )
=====End Script====

Note, that it is not necessary to constantly check the errorlevel, as
you can nest it within a command wrap. IE, in this case you can wrap
lines 11-14 and 15-17 all in one () group.

I would, however, like to chime in my opinion here, and state that when
checking if a computer is 'online' or 'offline' it is better to simply
count.

for /f "tokens=1 delims= " %%a in ('ping -n 1 %pcname% ^| find /i /c


"ttl"') do set online=%%a

The above will set %online% to a 1 or a 0 and then you can do your
checks from there. Also, if you wish to expand your script passed the
primary command, %online% will still be set, whereas an ERRORLEVEL will
change dependant on the next command.

Also, one should consider the below:

The following syntax is also used to 'shorten' errorlevel chcks, at
least in this situation:

===Script: PNG4.cmd===


001 @echo off
002 :top

003 if "%~1"==""(set /p pcname=Enter PC Name/IP Address:


005 if /i "%pcname%"=="" goto :top
006 goto :test
007 )
008 set pcname=%1
009 :test

010 ping -n 1 %pcname% > nul || (echo %PCNAME% is Offline
011 echo %PCNAME% is Offline >> ping.txt
012 start /min sndrec32.exe /play /close %windir%\media\ringin.wav
013 goto :eof
014 )
015 echo %PCNAME% -- Online!
016 echo %PCNAME% -- Online >> ping.txt
==== End Script ===

Note the above syntax, and how it did not check for ERRORLEVELS. It
didn't need to. Because of the double redirect, ie ||, it tells the
command processor that if the previous command 'failed' then it was to
do whatever was within the command group: (). Line 13 tells the
command group to terminate the script. If the command was sucessful,
it would automatically skip to line 15 as lines 11.5-14 are all within
the command group: ().

However, note also from the above, that if you wanted to use the detail
from the ping report, ie a 1 or a 0, or a condition set, you wouldn't
be able to within that type of script. This is more usable for a one
time ping, or a quick test.

Just my thoughts on the above.

Cryptogr...@yahoo.com

unread,
May 22, 2006, 9:21:26 PM5/22/06
to
Thank you everyone! I now have a working .bat file. one more question
is there anyway to add a 20m timeout?

My working script:

@echo off
:start
set ip=
set /p ip=Enter IP or Hostname:
if "%ip%"=="" goto start
:loop
for /f "tokens=1 delims= " %%a in ('ping -n 1 -w 900 %ip% ^| find /i /c


"ttl"') do set online=%%a

if /i "%online%"=="0 " echo %ip% offline
if /i "%online%"=="0 " (goto chktime) else goto flash

:flash
echo wscript.echo "Computer: %ip% -- Is Online!" > C:\flash.vbs
call C:\flash.vbs

:chtime
FOR /F "tokens=2 delims=:" %%G IN ('time /t') DO set time_var=%%G
if "%time_start%"=="%time_var%" (goto loop) else goto time_out

:time_out
echo wscript.echo "Pinging Computer %ip% failed - Operation Timed out"
> C:\flash.vbs
call C:\flash.vbs

:end
pause

----------END Script--------

as you can see with the :time_out I started making my own timeout that
will stop the program when AM changes to PM or vise versa is there a
better way?

iamj...@gmail.com

unread,
Apr 24, 2017, 6:19:24 AM4/24/17
to
Thanks
0 new messages