Solved

Script that reads a TXT file, ping computername and return IP address

Posted on 2008-06-24
11
2,246 Views
Last Modified: 2013-11-25
Hi Experts,

Is it possible to have a script reading a text file with computer names, ping computer name and return the IP address?
I've seen script that compear column in Excel, ping IP address in text file etc, but I can't get the script pinging computer name and return the IP address. Even better if the script could write the IP next to the computer name.

In advanced, thank you.
0
Comment
Question by:Bjarte Fjelland
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
11 Comments
 
LVL 13

Expert Comment

by:TheCapedPlodder
ID: 21853444
Try this:

@echo off
for /f %%a in (comptuer_list.txt) do set target=%%a & call :ping
goto end

:ping
ping %target% -n 1 >ping_output.txt
for /f "tokens=3 skip=2" %%b in ('find "Reply" ping_output.txt') do set address=%%b & call :write
goto end

:write
del ping_output.txt
echo %target% %address% >>output.txt

:end




computer_list.txt needs to look like this:

server1
server2
server3
etc.

At the end of the process output.txt will look like this:

server1 10.0.0.1:
server2 10.0.0.2:
server3 10.0.0.3:
etc.
0
 
LVL 1

Author Comment

by:Bjarte Fjelland
ID: 21854371
Tryed your suggestion TheCapedPlodder, but the Output.txt does not appear, only the last ping status in ping_output.txt file.
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 13

Expert Comment

by:TheCapedPlodder
ID: 21854392
Ok, REM out the @echo off line and run it again. e.g.:

REM @echo off


  Then post the output here.  Initially only have two servers in the input file for testing.
0
 
LVL 1

Author Comment

by:Bjarte Fjelland
ID: 21854691
It does fetch the computer name from the computer_list.txt and continue the rutine, but it does not write the output file.

C:\>REM @echo off
C:\>for /F %a in (comptuer_list.txt) do set target=%a   & call :ping
C:\>set target=Server1 & call :ping
C:\>ping Server1  -n 1  1>ping_output.txt
C:\>for /F "tokens=3 skip=2" %b in ('find "Reply" ping_output.txt') do set address=%b   & call :write
C:\>goto end
0
 
LVL 13

Expert Comment

by:TheCapedPlodder
ID: 21854726
Interesting, can you post the contents of ping_output.txt for me please?

This worked for me but I run Vista so the output might be slightly different.

Cheers,

TCP
0
 
LVL 1

Author Comment

by:Bjarte Fjelland
ID: 21854982
Got it to work, but on a English OS.
I do not have English installed on my computer because of support.

Thank you.
0
 
LVL 13

Expert Comment

by:TheCapedPlodder
ID: 21855006
Aha!

That makes sense.

You can change it to work with your language.  Just post me the ouput of ping_output.tx and I'll let you know.

Cheers,

TCP
0
 
LVL 1

Author Comment

by:Bjarte Fjelland
ID: 21855154
Of course :o)

Pinger Server1.Domain.NO [IP address] med 32 byte data:
Svar fra IP address: byte=32 tid=5ms TTL=120
Ping-statistikker for IP address:
    Pakker: sendt = 1, mottatt = 1, tapt = 0 (0% tap),
Gjennomsnittlig tid for tur-retur i millisekunder:
    minimum = 5ms, maksimum = 5ms, gjennomsnittlig = 5ms
0
 
LVL 13

Accepted Solution

by:
TheCapedPlodder earned 500 total points
ID: 21855250
Updated (I just replaced the word Relpy with the word Svar).  Give it a whirl:


@echo off
for /f %%a in (comptuer_list.txt) do set target=%%a & call :ping
goto end

:ping
ping %target% -n 1 >ping_output.txt
for /f "tokens=3 skip=2" %%b in ('find "Svar" ping_output.txt') do set address=%%b & call :write
goto end

:write
del ping_output.txt
echo %target% %address% >>output.txt

:end


I've also updated it to make it a bit easier to read if you want a neater version try this:
@echo off
for /f %%a in (comptuer_list.txt) do (
      set target=%%a
      call :ping
)
goto :eof

:ping
ping %target% -n 1 >ping_output.txt
for /f "tokens=3 skip=2" %%b in ('find "Svar" ping_output.txt') do (
      set address=%%b
      call :write
)
goto :eof

:write
del ping_output.txt
echo %target% %address% >>output.txt




Cheers,

TCP
0
 
LVL 1

Author Closing Comment

by:Bjarte Fjelland
ID: 31470058
Thanks, that was more than I've expected.
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
This article provides a convenient collection of links to Microsoft provided Security Patches for operating systems that have reached their End of Life support cycle. Included operating systems covered by this article are Windows XP,  Windows Server…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month8 days, 2 hours left to enroll

617 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