Computing.Net > Forums > Programming > Batch Ping, Errorlevel script help

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Batch Ping, Errorlevel script help

Reply to Message Icon

Name: bignold
Date: December 4, 2008 at 04:55:20 Pacific
OS: Vista Ultimate
CPU/Ram: 8g
Product: Custom / CUSTOM
Comment:

Hi, Im creating a program to help me with simple network monitoring over many networks. The program I'm creating is a bunch of small tools to make my job more efficient. One part of this program is pinging a host to see whether it is up or down. Below is a small fragment i have adapted to show on the site. I would like the outcome not to include the details of the ping response. I know i can do this through Goto **** but i would like to avoid that in this part of the script.

Script now -

echo.
echo Please specify target. Type 127.0.0.1 for client PC.
set /p t=Target:
echo.
ping -n 1 -l 1 %t%
if %errorlevel%==0 echo Host is UP!
if %errorlevel%==1 goto Host is DOWN!
set /p a=Continue? (Y/n):
if "%c%"=="n" exit

Current script outcome -

Please specify target. Type 127.0.0.1 for client PC.
Target:127.0.0.1

Pinging 127.0.0.1 with 1 bytes of data:
Reply from 127.0.0.1: bytes=1 time<1ms TTL=128

Ping statistics for 127.0.0.1:
Packets: Sent = 1, Received = 1, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 0ms, Maximum = 0ms, Average = 0ms
Host is UP!
Continue? (Y/n):

- Preferred outcome

Please specify target. Type 127.0.0.1 for client PC.
Target:127.0.0.1

Host is UP!
Continue? (Y/n):



Sponsored Link
Ads by Google

Response Number 1
Name: klint
Date: December 4, 2008 at 05:35:56 Pacific
Reply:

Just add >nul after ping.


0

Response Number 2
Name: bignold
Date: December 4, 2008 at 17:16:45 Pacific
Reply:

thanks, worked great.


0

Response Number 3
Name: klint
Date: December 5, 2008 at 01:36:40 Pacific
Reply:

I would also like to point out a potential vulnerability in your code:

set /p t=Target:
echo.
ping -n 1 -l 1 %t%

Now suppose, at the Target: prompt, the user of your batch file types

hostname && del *.*

Then the batch file will execute the following command:

ping -n 1 -l 1 hostname && del *.*

... which will unfortunately go ahead and delete all your files. Maybe you're not that bothered, if the only user of your batch file will be you (and you trust yourself not to type the above) but in principle you should try and make your batch files more secure. You can do that using delayed expansion:

setlocal enabledelayedexpansion
set /p t=Target:
echo.
ping -n 1 -l 1 !t!

Then the command line interpreter will parse the ping line before it expands the string !t! and thus it will not try and run any rogue commands.


0

Response Number 4
Name: bignold
Date: December 5, 2008 at 08:09:04 Pacific
Reply:

Thanks for the advice, i will be the only user of the batch program for now anyway. I will try to insert this into my future projects. Thanks again


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Batch Ping, Errorlevel script help

Script help - New line and character removal? www.computing.net/answers/programming/script-help-new-line-and-character-removal/19883.html

Batch - Ping Ip Address scan www.computing.net/answers/programming/batch-ping-ip-address-scan/17335.html

Batch Script Help Needed www.computing.net/answers/programming/batch-script-help-needed/14475.html