@echo off
color A
title Ping Tester
set /p IP= Enter your IP/Domain:
:top
PING -n 1 %IP% | FIND "TTL="
ping -n 2 -l 10 127.0.0.1 >nul
GoTo top
CodePudding user response:
You know how to use set /p so just amend it to request part of the IP only.
@echo off
color A
title Ping Tester
:top
set "partIP=192.168.0."
set /p "eightbit=%partIP%"
ping -n 1 %partIP%%eightbit% | FIND "TTL="
goto top
CodePudding user response:
I'm not sure if you are aware that there may be up to three digits in the fourth octet of a decimal dotted IPv4 address, (in the range 0..255). If you are wanting the end user to have free input, you will need to ensure that what they input, is validated before using it. The Set /P construct allows the end user to enter nothing, or anything at all, (including malicious strings):
@Echo Off
SetLocal EnableExtensions
:Ask
Rem Request fourth octet of decimal dotted IPv4 address.
Set "octet="
Set /P "octet=Please enter fourth octet>"
Rem Verify and ask again if a valid octet was not entered.
(Set octet) 2>NUL | %SystemRoot%\System32\findstr.exe /XRC:"octet=[0123456789]" /C:"octet=[0123456789][0123456789]" /C:"octet=[01][0123456789][0123456789]" /C:"octet=2[01234][0123456789]" /C:"octet=25[012345]" 1>NUL || GoTo Ask
Rem Define the maximum number of failed attempts.
Set "maxTries=10"
:Run
For /L %%G In (1,1,%maxTries%) Do (
%SystemRoot%\System32\PING.EXE -n 1 192.168.0.%octet% | %SystemRoot%\System32\find.exe "TTL=" && GoTo Ask
Echo No response, re-trying
%SystemRoot%\System32\timeout.exe /T 1 /NoBreak 1>NUL
)
GoTo Ask
If you really do need only one digit, as specifically stated in your question title, then you may have the option of using the choice command. The benefit is that you would not need to individually determine if an entry was made, and if so, whether it was a single digit, as opposed to one or more unwanted characters.
@Echo Off
SetLocal EnableExtensions EnableDelayedExpansion
:Ask
Rem Define the maximum number of failed attempts.
Set "maxTries=10"
Rem Request single digit non zero input.
%SystemRoot%\System32\choice.exe /C 123456789 /N /M "Please enter last digit>"
:Run
%SystemRoot%\System32\PING.EXE -n 1 192.168.0.%ErrorLevel% | %SystemRoot%\System32\find.exe "TTL=" || (
Set /A maxTries -= 1
Echo No response, re-trying . . .
%SystemRoot%\System32\timeout.exe /T 2 /NoBreak 1>NUL
If !maxTries! GEq 1 GoTo Run
)
GoTo Ask

