@echo off
setlocal enableextensions enabledelayedexpansion
for /l %%a in (0) do (
set /a "A=!random! %% 255", ^
"B=!random! %% 255", ^
"C=!random! %% 255", ^
"D=!random! %% 255"
ping -w 1000 -n 1 "!A!.!B!.!C!.!D!" | find "TTL=" > nul && (
>>"online.txt" echo !A!.!B!.!C!.!D!
)
)
This creates and infinite loop (this for /l %%a in (0)
means for %%a starting in 0 up to 0 in steps of 0
)
For each iteration, four random numbers are generated for each of the the ip address octects. To do it, we generate a random number usign the build in !random!
variable (delayed expansion is needed) and get remainder of the division by 255 (modulo operator is %%
in batch files) using a set /a
command, the batch way of doing calcs.
Once the ip is generated (more delayed expansion, see previously linked answer), a ping is sent and its output checked for the presence of the TTL=
string (more information here). If this string is present, the target is reachable and the address is appended to a file.
To test if the string is present in the output of the ping
command, it is piped into a find
command searching for the indicated string. If the string is found errorlevel
variable is set to 0, if it is not found, errorlevel
will be set to 1.
This value is checked using conditional execution operator &&
. This means if the previous command does not set the errorlevel
to anything greater than 0, then execute the next command.
So, if the find
command finds the string, errorlevel
will not be 1 and the echo
command will be executed. This echo
is redirected to append to target file (>>
is an append redirection)