Solved

Batch script to parse list of IPs, ping then take action.

Posted on 2009-04-10
5
1,573 Views
Last Modified: 2013-11-10
I have a text file of a few hundred IP addresses. I want to read each IP from the file.
After I obtain the IP, I'll ping the address.
If the ping result is a success I want to run a whole bunch of other batch commands (I've got about 10 lines of code) - then go to the next IP in the text file when done.
If the ping result is a failure, just go to the next IP on the list.

I think I'm better off not posting my current code that doesn't work! Thanks guys.
0
Comment
Question by:ZabagaR
5 Comments
 
LVL 63

Expert Comment

by:SysExpert
ID: 24118390
0
 
LVL 69

Assisted Solution

by:Qlemo
Qlemo earned 150 total points
ID: 24118411
@echo off
for /F %%I in (iplist.txt) do (
  ping -n 1 -w 100 %%I >nul && (
     REM put your commands here, IP is %%I
  )
)
0
 
LVL 67

Accepted Solution

by:
sirbounty earned 200 total points
ID: 24118416
this should do it...drop your code after the 'success' message
@echo off
for /f %%a in (ip.txt) do call :pingit %%a
goto :eof
 
:pingit
set ip=%1
ping %ip% -n 1
if %errorlevel%==0 goto success
goto :eof
 
:success
Echo %ip% success!

Open in new window

0
 
LVL 63

Assisted Solution

by:SysExpert
SysExpert earned 150 total points
ID: 24118421
0
 
LVL 15

Author Closing Comment

by:ZabagaR
ID: 31569048
SirBounty - that's the clearest answer for me. I actually was checking in to say I got the solution myself, similar to yours but instead of calling a local routine, I call another .bat and send my value to it as %1. I had already read 1 or 2 of the examples SysExpert sent - the first isn't any help but the last 2 are workable. But, I don't have a good understanding of the syntax. Same as Qlemo's input. I don't understand the double && or the use of the parenthesis. I'll have to look it up.

Thanks all !!  I really appreciate the quick replies. You guys and this site are great.
0

Featured Post

The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question