1

I have made a program that creates random IPs and pings them to see if they exist. It records the output using '>' and '>>'. I want to check if the ping times out and if it does exclude it from the text document. This is my code so far:

`@ECHO OFF
:LOOP
 SET /A N1=%RANDOM% * 255 / 32768
 SET /A N2=%RANDOM% * 255 / 32768
 SET /A N3=%RANDOM% * 255 / 32768
 SET /A N4=%RANDOM% * 255 / 32768
 PING %N1%.%N2%.%N3%.%N4%>>"%USERPROFILE%\Desktop\IP.txt"
 GOTO LOOP`

After the ping is done I want it to record IPs that respond, not the ones that time out.

-Thanks in advance

Aeolus
  • 996
  • 1
  • 8
  • 22
  • Here is a good start http://stackoverflow.com/questions/3050898/how-to-check-if-ping-responded-or-not-in-a-batch-file – Rahul Jun 06 '15 at 21:14

2 Answers2

0
@echo off
setlocal EnableDelayedExpansion

rem Set the number of IP's to test
set num=20

echo Creating and testing %num% IP's
echo/

for /L %%i in (1,1,%num%) do (

   rem Create the random IP
   set /A N1=!random! %% 255, N2=!random! %% 255, N3=!random! %% 255, N4=!random! %% 255
   set "IP=!N1!.!N2!.!N3!.!N4!"

   rem Show the IP and leave the cursor after it
   set /P "=%%i- Testing !IP!: " < NUL

   rem Test the IP with ping and get just the 3rd word of the 3rd line from ping output
   set "word="
   for /F "skip=2 tokens=3" %%a in ('ping !IP!') do if not defined word set "word=%%a"

   rem If that word is not "out", the IP is correct
   if "!word!" neq "out" (
      echo CORRECT
      echo !IP!>> IP.txt
   ) else (
      echo failed...
   )

)
Aacini
  • 65,180
  • 12
  • 72
  • 108
  • Could you explain this to me? As I said before I'm a bit new and I don't know some of these commands. – Aeolus Jun 07 '15 at 21:48
0
@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)

Community
  • 1
  • 1
MC ND
  • 69,615
  • 8
  • 84
  • 126