mflammia
mflammia

Reputation: 87

Batch file FOR Loop via a text file of IP Addresses not working

Grateful for any help.

Have been trying to write a batch file that simply pings a list of IP address from a text file.

I know it can be done using the following code:

FOR /F %i in (yourFile.txt) DO yourcommand %i

and have used the format to create the below:

::@echo off
pause
for /F %%a in (vlans.txt) do (

pause
ECHO Checking connection to subnet %%a, please wait....
PING -n 1 %%a|find "Reply from " >NUL
IF NOT ERRORLEVEL 1 goto :SUCCESS
IF ERRORLEVEL 1 goto :FAIL

:FAIL
ECHO Subnet %%a is unreachable
pause

:SUCCESS
ECHO Subnet %%a has passed!
pause

)

ECHO All subnets have now been tested....................
Pause

It needs a little tidying up, and I've commented out the @Echo off so I can see whats going on. The following is outputted:

C:\for /F "tokens=*" %a in (vlans.txt) do (

pause
ECHO Checking connection to subnet %a, please wait....
PING -n 1 %a  | find "Reply from "  1>NUL
IF NOT ERRORLEVEL 1 goto :SUCCESS
IF ERRORLEVEL 1 goto :FAIL

ECHO Subnet %a is unreachable
pause

ECHO Subnet %a has passed!
pause
)

C:\>(

pause
ECHO Checking connection to subnet 10.169.169.129, please wait....
PING -n 1 10.169.169.129  | find "Reply from "  1>NUL
IF NOT ERRORLEVEL 1 goto :SUCCESS
IF ERRORLEVEL 1 goto :FAIL

ECHO Subnet 10.169.169.129 is unreachable
pause

ECHO Subnet 10.169.169.129 has passed!
pause
)

Press any key to continue . . .

Checking connection to subnet 10.169.169.129, please wait....

C:\>ECHO Subnet %a has passed!

Subnet %a has passed!

C:\>pause

Press any key to continue . . .

So lots of things are happening there that I don't understand or didn't expect, like it not cycling through every IP address in the text file. It repeating itself, first without filling in the variables, then with the variables. Also some variables are not populating with an IP address further on as the batch file runs.

I'm new to writing batch files so I know my syntax is wrong but not sure where I am going wrong?

The vlan.txt just has a list of IP address, one each line.

Thanks in advance.

Upvotes: 1

Views: 8381

Answers (3)

Zip Zinzel
Zip Zinzel

Reputation: 69

MOST OF THE EXAMPLES ABOVE WILL FAIL FOR VARIOUS REASONS
My example below DOES work in Win7

PROBLEMS with the above examples=
1. You CANNOT %Errorlevel% check a ping, because it ALWAYS returns a zero(0) errorlevel,
. . . unless the IP-Protocol stack is completely hosed.
The Errorlevel from the PING.EXE command only indicates that PING.EXE 'ran' as expected,
. . . and indicates nothing whatsoever about what the responses or output was.
* * * * * * * *

2. GREAT-IDEA to pipe the PING command through the "FIND" command,
(. . . prev I was piping the PING command to a txt-file,
and then "finding" against that TXT-FILE.)

-------   HOWEVER, 'Finding' on "Reply from":
. . is a failure for this reason:

Pinging 192.168.1.100 with 32 bytes of data:
Reply from 192.168.1.34: Destination host unreachable.

In this case, 192.168.1.34 (second line) is your localhost reporting that target is 'unreachable'
-------------------------------------------
NOTE:ANY successful ping response will ALWAYS have TTL=(some number less than 65)

Pinging 192.168.1.1 with 32 bytes of data:
Reply from 192.168.1.1: bytes=32 time

TAKEAWAY: pipe your ping output into FIND.EXE, using "TTL=" instead*
* * * * * * * *


= = = = = MY WORKING EXAMPLE:

@ECHO OFF
setlocal EnableDelayedExpansion

for /F %%a in (cpuipaddrs.txt) do (
    ECHO.
    ECHO PINGING %%a, please wait....
    PING -n 2 %%a|find "TTL=" >NUL
    IF !ERRORLEVEL! neq 0 (ECHO       UNREACHABLE, %%a is UNREACHABLE
       ) ELSE (ECHO  %%a has pinged successfully )
)



= = = = = SCREEN OUTPUT FROM ABOVE:

PINGING 192.168.1.1, please wait....
 192.168.1.1 has pinged successfully

PINGING 192.168.1.100, please wait....
      UNREACHABLE, 192.168.1.100 is UNREACHABLE

PINGING 192.168.1.111, please wait....
 192.168.1.111 has pinged successfully

ADDITIONAL NOTES ON THIS BATFILE:
1.Note that inside the 'FOR-Loop' I am referencing the %ERRORLEVEL% as !ERRORLEVEL!
readup on "EnableDelayedExpansion" to understand, why the normal %Percent-Variables don't work as normally inside these loops.

2. ALSO take SPECIAL note on how I split up the 'open' & 'close' parenthesises,
ESPECIALLY after the "DO" on line 3, and before the "ELSE" on line 8
There are some goofy rules on how to break lines, with 'parens', DO's, ELSE's, & etc

3. In this example cpuipaddrs.txt is a regular txt-file with 3 lines,

192.168.1.1
192.168.1.100
192.168.1.111

Upvotes: 1

jeb
jeb

Reputation: 82192

There is a major problem in the code.

GOTO in a loop (or in any block of code) breaks the loop/block.
So after the first GOTO :somewhere your loop ends immediately.
And labels in blocks can cause strange errors, so this should also be avoided.

These two lines could be optimized

IF NOT ERRORLEVEL 1 goto :SUCCESS
IF ERRORLEVEL 1 goto :FAIL

to

IF ERRORLEVEL 1 ( echo FAIL ) ELSE (echo Success)

Another problem is the IF ERRORLEVEL 1 syntax itself.
It's true if ERRORLEVEL is equal or greater than the number.
Therefore I prefer the more obvious syntax of

IF !ERRORLEVEL! EQU 0 echo SUCCESS

In blocks you can't access the errorlevel with percent expansion, as percent expansion is done when the block is parsed not when it's been executed.

For the delayed expansion (with exclamation marks) you need to enable it first via setlocal EnableDelayedExpansion.

@echo off
setlocal EnableDelayedExpansion
for /F %%a in (vlans.txt) do (
    ECHO Checking connection to subnet %%a, please wait....
    PING -n 1 %%a >NUL
    IF !ERRORLEVEL! == 0 (
        ECHO Subnet %%a has passed^^!
    ) else (
        ECHO Subnet %%a is unreachable
    )
)

Upvotes: 1

Endoro
Endoro

Reputation: 37569

Try this:

@echo off
for /F %%a in (vlans.txt) do (
    ECHO Checking connection to subnet %%a, please wait....
    PING -n 1 %%a|find "Reply from " >NUL
    IF %ERRORLEVEL% neq 0 (ECHO Subnet %%a is unreachable) else ECHO Subnet %%a has passed!
)
ECHO All subnets have now been tested....................
Pause

Upvotes: 1

Related Questions