0

I want to make a basic pinger in batch as my learning project and i came into a problem.Here is the code:

@echo off
color B
title Pinger v1.0 
:OK
echo.
echo.
set /p t=Target I.P.:
echo.
echo.
echo This is not an IP address!
echo.
goto OK
set /p a=Packet size:
echo.
:start
set ifer=
set /p ifer=Start Ping (y/n):
if %ifer%==y goto 8
if %ifer%==Y goto 8
if %ifer%==n goto OK
if %ifer%==N goto OK
:8
echo.
echo.
echo.
echo.
ping %t% -t -l %a%

My question is, How can i check if t contains a valid IP address an not some random data? and also, how can i make a code that translates a web address to an ip address(Don't have to answer this though)

user3402353
  • 33
  • 2
  • 5
  • Ping will already accept either host name or ip .. – Alex K. Mar 10 '14 at 15:34
  • See: [ValidateIP.bat subroutine](http://stackoverflow.com/questions/20299266/ip-verification-in-batch-script-first-match-by-findstr-secondly-verify-by-for/20301833#20301833) – Aacini Mar 10 '14 at 16:06

1 Answers1

0

I don't know exactly what you're trying to get at here. I made a program that will take your IP or host name and ping it with a user determined packet size, and amount of times to ping. I only tried 1.1.1.1 and www.google.com, but it should work for anything else.

@echo off
@title Pinger v1.1

color B
setlocal EnableDelayedExpansion

:ipInput
cls
set /p tryIP=Target IP:
:: pinging ip once (-n 1) with one byte of data (-l 1) to check if it's valid
ping -n 1 -l 1 !tryIP! >nul & if errorlevel 1 ( 
    echo IP: !tryIP! is invalid, try correcting your input or using a different IP
    pause
    goto ipInput
)
cls
echo IP: !tryIP! is Valid
set /p pingCount=Enter number of pings:
set /p packets=Enter packet size:
echo Pinging !tryIP! with !packets! bytes for !pingCount! time^(s^)
:: You can either use this line (beneath)
REM ping -n !pingCount! -l !packets! !tryIP!


:: Or you can use this for loop from here.........
:: for /l indicates type of operation in this case it will take (start at, add or 
subtract by, end at)
:: %%a in this case, based on /l will always equal the number 
:: of times it's looped through..
:: (starting at 1, moving up by 1 each time, and ending after 32)
for /l %%a in (1,1,%pingCount%) do (
    ping -n 1 -l !packets! !tryIP! >nul
    cls
    echo Pinging !tryIP! with !packets! bytes for !pingCount! time^(s^)
    set /a percentage=%%a*10000/!pingCount!
    if !percentage! LSS 100 echo ......!percentage:~0,0!.!percentage:~0,2!%%
    if !percentage! GEQ 100 if !percentage! LSS 1000 echo ......!percentage:~0,1!.!percentage:~1,2!%%
    if !percentage! GEQ 1000 echo ......!percentage:~0,2!.!percentage:~2,2!%%
)
    :: the if statements are used to correct the formating on the percentages
    :: since batch can't use floating point decimals,
    :: you have to get a little creative sometimes.
    :: dual % signs are so it doesn't think you're trying to set a variable
    :: using ( or ) without that >>>^ (eg on lines 20 and 34) 
    :: will sometimes mess things up. 
:: ........ to here. the benefit of a for loop is that you can see your progress,
:: and it's much much faster as the ping command will wait between pings.
echo Done
pause
goto ipInput