Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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" exitCurrent script outcome -
Please specify target. Type 127.0.0.1 for client PC.
Target:127.0.0.1Pinging 127.0.0.1 with 1 bytes of data:
Reply from 127.0.0.1: bytes=1 time<1ms TTL=128Ping 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.1Host is UP!
Continue? (Y/n):

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.

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

![]() |
![]() |
![]() |

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