You are almost there, you just never use the variable %%z
of the inner for /L
loop:
@ECHO OFF
SET startTime=%time%
ECHO Starting the IP Scan
FOR /L %%i IN (1,1,2) DO (
ECHO Pinging IP Range: 10.163.%%i._
FOR /L %%z IN (1,1,10) DO (
ECHO Pinging IP: 10.163.%%i.%%z
PING -n 1 -w 500 10.163.%%i.%%z | FIND /i "Reply">>"IPScanResults.txt"
)
)
ECHO Run Time = %startTime% to %time%>>"IPScanResults.txt"
ECHO *** IP Scan Complete. ***
ECHO Check the file called "IPScanResults.txt" to see the results
ECHO Run Time = %startTime% to %time%
I used this variable as the fourth octet (rather than the constant 15
), so the console output is this:
Starting the IP Scan
Pinging IP Range: 10.163.1._
Pinging IP: 10.163.1.1
Pinging IP: 10.163.1.2
...
Pinging IP: 10.163.1.10
Pinging IP Range: 10.163.2._
Pinging IP: 10.163.2.1
Pinging IP: 10.163.2.2
...
Pinging IP: 10.163.2.10
*** IP Scan Complete. ***
Check the file called "IPScanResults.txt" to see the results
Run Time = 0:05:07.24 to 0:05:17.16
...and the content of the log file IPScanResults.txt
might look like this:
Reply from 10.163.1.10: bytes=32 time<1ms TTL=128
Run Time = 0:05:07.24 to 0:05:17.16
In addition, I indented the for /L
loops to improve legibility, and I put quotation marks around the log file name, so it might be changed so that it even contains white-spaces without any trouble.
Furthermore, I exchanged %500%
by %time%
, because I assumed you wanted to store the start time. %500%
is an invalid variable name, because %
followed by a numeric figure (%5
in this case) means script argument expansion -- see call /?
for more information on that.
Hint:
If you do not have continuous values for one of the octets, for instance, 160
, 162
, 163
, you can use a standard for
loop instead of for /L
:
FOR %%j IN (160 162 163) DO (
PING -n 1 -w 500 10.%%j.1.15
)
Anyway, I would accomplish your task with the following script most probably due to its flexibility:
@echo off
setlocal EnableExtensions EnableDelayedExpansion
rem Define constants here:
set "LOGFILE=IPScanResults.txt"
rem Define IP octets here:
set "OCTET1=10" & rem (a single value)
set "OCTET2=160 162 163" & rem (space-separated list of values)
set "OCTET3=1,1,254" & rem (comma-separated range (min.,step,max.))
set "OCTET4=15,1,16" & rem (comma-separated range (min.,step,max.))
rem Prepare `for` switch `/L` dynamically:
for /L %%O in (1,1,4) do (
if not "!OCTET%%O!"=="!OCTET%%O:,=!" set "SWITCH%%O=/L"
)
set "STARTTIME=%TIME%"
if exist "%LOGFILE%" (>> "%LOGFILE%" echo()
echo Starting the IP Scan
for %SWITCH1% %%I in (%OCTET1%) do (
for %SWITCH2% %%J in (%OCTET2%) do (
echo Pinging IP Range: %%I.%%J.__.__
for %SWITCH3% %%K in (%OCTET3%) do (
for %SWITCH4% %%L in (%OCTET4%) do (
echo Pinging IP: %%I.%%J.%%K.%%L
ping -n 1 -w 500 %%I.%%J.%%K.%%L | >> "%LOGFILE%" find "TTL"
)
)
)
)
>> "%LOGFILE%" echo Run Time = %STARTTIME% -- %TIME%
echo *** IP Scan Complete. ***
echo Check the file called "%LOGFILE%" to see the results.
echo Run Time = %STARTTIME% -- %TIME%
endlocal
exit /B