Solved Ping list of computers from a txt file

August 18, 2009 at 08:59:26
Specs: Windows XP
I'm trying to figure out a way to ping a list of computer names from a txt file and then output the IP address to a text file using a Batch file. Can anyone help?

See More: Ping list of computers from a txt file

Report •


✔ Best Answer
September 28, 2009 at 07:55:36
Had a request to have more information added to the output file.

Column 1: Computer name
Column 2: IP if on network, statement if not on network
Column 3: If a folder exist on remote machine or not
Column 4: Remote computer mac address

Obviously for Columns 3&4 you will need to have permissions on the remote computer.
For the folder check, Line 15 will need to be set to what you are looking for.


@echo off

set ComputerList=C:\Path\to\computer\list.txt

Echo Computername,IP Address,Folder Exists,Mac Address>Final.csv
setlocal enabledelayedexpansion

for /f "usebackq tokens=*" %%A in ("%ComputerList%") do (
for /f "tokens=3" %%B in ('ping -n 1 -l 1 %%A') do (
If %%B==could set IPadd=Not On Network.
)
for /f "tokens=3" %%a in ('ping -n 1 -l 1 %%A ^|findstr Reply') do (
set IPadd=%%a
)
If /i "!IPAdd!" NEQ "Not on Network." If Exist \\%%A\C$\Exped (
Set Folder=Yes
) ELSE (
Set Folder=No
)
If /i "!IPAdd!" NEQ "Not on Network." (
for /f "Tokens=1" %%d in ('getmac /s %%A /nh') do set MacAdd=%%d)
echo %%A,!IPadd:~0,-1!,!Folder!,!MacAdd!>>final.csv
)



#1
August 18, 2009 at 10:22:26
Take a look at this single line of code posted by xterm11 in another topic:
http://www.computing.net/answers/pr...

It can be extended to comprehend more than one single command, like this:

for /f %%a in (filename including location) do (
command1
command2
commandN...
)


Report •

#2
August 18, 2009 at 12:51:02
for /f %%a in (filename including location) do (ping %%a)

In the file put the computer ip adress on each line. The text file should look like:

127.0.0.1
127.24.12.9
192.1.1.5

There are no stupid questions, just stupid people.


Report •

#3
August 18, 2009 at 12:53:54
there's about 1300 computers to ping. I would like to ping only once and then output the IP address to a txt file.

Report •

Related Solutions

#4
August 18, 2009 at 13:56:28
This will ping every computer in the domain specified.

@echo off
setlocal enabledelayedexpansion
for /f "skip=3 tokens=1" %%a in ('net view') do (
set pcname=%%a
set pcname=!pcname:~2!
ping !pcname!.DOMAIN.local -n 1 -l 1 -w 100
)

There are no stupid questions, just stupid people.


Report •

#5
August 18, 2009 at 14:31:33
This will do the same, and output both computer name and ip
address. Change domain to your domain.

@echo off
>>output.txt echo Computer name - IP address
setlocal enabledelayedexpansion
for /f "skip=3 tokens=1" %%a in ('net view') do (
set pcname=%%a
set pcname=!pcname:~2!
for /F "delims=" %%a in ('ping
!pcname!.DOMAIN.local^|findstr Reply') do set
output=%%a
for /F "tokens=3" %%a in ("!output!") do set output=%%a
echo !output!> %temp%\string.txt
for %%a in (%temp%\string.txt) do set /a length=%%~za
set /a length -=4
del %temp%\string.txt
call set output=%%output:~0,!length!%%
>>output.txt echo !pcname! - !output!
pause
)

There are no stupid questions, just stupid people.


Report •

#6
August 19, 2009 at 06:55:56
I have the list of computer names. just need to get the IP address of roughly 1300 computers.

Report •

#7
August 19, 2009 at 17:30:44
hi xterm11 :)

i tried to follow your procedure in Response Number 4 and Response Number 5.

however, i only get a black (or blank) output using a notepad saved as a batch file when i did run in command prompt.

what must been lacking in it considering that i have also change the domain name to our actual or existing domain in our network?

hope u can enlighten me of my inquiry.


thanks.

let's build bridges, and not walls :)


Report •

#8
August 21, 2009 at 07:47:35
hello again xterm11 ..

hoping u can update us on this aspect ..


cheers!

let's build bridges, and not walls :)


Report •

#9
August 22, 2009 at 10:15:59
Try something like this? Just change the second line to point to your list of computers.

@echo off
set ComputerList=C:\path\to\computer\list.txt

Echo Computername,IP Address>Final.csv
setlocal enabledelayedexpansion

for /f "usebackq tokens=*" %%A in ("%ComputerList%") do (
for /f "tokens=3" %%B in ('ping -n 1 -l 1 %%A ^|findstr Reply') do (
set IPadd=%%B
echo %%A,!IPadd:~0,-1!>>final.csv
))


Report •

#10
August 23, 2009 at 00:59:38
hi again gtaion ..

i'll definitely try the program u have created. it seems yur really an expert or have gained a lot of experienced with batch files or i'm sure with command line in DOS.

i'll just update u then with the results when i applied it.

cheers!

let's build bridges, and not walls :)


Report •

#11
August 24, 2009 at 10:19:06
Here's what I did and it worked. This will ping each computer name in your txt file and will output the ping result to a seperate txt file. This pings each machine once only. I then exported the txt file to Excel and filtered out the results.

@echo off
FOR /F "tokens=*" %%I in (c:\list.txt) do call :pinger %%I goto :eof

:pinger

echo %TIME% >> ping.txt
ping -n 1 %1 >> ping.txt
:: DONE


Report •

#12
August 24, 2009 at 18:27:04
That seems very labor intesive, did the command in response number 9 not work for you? It works on my computer just fine.

Report •

#13
August 25, 2009 at 10:10:07
hello gtaion ..

i having a hard-time or difficulty in following either i execute Response Number 4 and 5, or Response Number 9 and 11 ... or is it the combination of those?


please advise also if do i have to initiate either of that Response Number 4 and 5 ... then as a continues syntax, i'll include either Response Number 9 or 11 (or both if necessary)??


this is a beginners thought, hope its okay to ask.


cheers!

let's build bridges, and not walls :)


Report •

#14
August 25, 2009 at 13:33:51
paste you code here. also, what error do you get?

Report •

#15
August 25, 2009 at 16:20:30
From reading the processes responses 4 and 5 don't actually read from a text file, they are using the net view command to list all of the computers in a domain.
Response 9, if you point it to a text file that has computernames formated one per line, i.e.
computer1
computer2
It should go through and ping each one once, and record them into a .csv, which stands for comma seperated values or the poor mans way of making a spreadsheet.
Response 11 is outputting the entire results of the ping command to a text file, and then you would have to manually edit out all of the extra headings and stuff.
Responses 4,5,9 and 11 are all different ways of doing bussiness so unless you know what you are doing I wouldn't try to combine them together if that is what you are asking.

Report •

#16
August 25, 2009 at 17:07:00
hi gtaion ..

thanks really for the clarifications. actually, i have some idea but i just want to be sure considering that u have more experienced on this field.

at any rate, i'm now more inclined to follow Response Number 9. and kindly correct me if i'm wrong, but the way i understand it ... the line: set ComputerList=C:\path\to\computer\list.txt emphasized that their is already a list of Computer Names located in that direction with the file name list.txt?

in addition, is the line:

Computername,IP Address>Final.csv

specifies that the results of Ping be saved under the filename Final.csv that has column Computername and IP Address??

likewise, what does that line setlocal enabledelayedexpansion means?


sorry really for being so basic :)


cheers!

let's build bridges, and not walls :)


Report •

#17
August 25, 2009 at 18:34:53
Yes, I was using the "Set" command more help in command prompt at set /? to set a variable called computerlist to point to a text file called list.txt that would contain a list of computer names or webservers(i.e. www.google.com).
Then I was porting the Echo command out to final.csv which is a comma seperated value's file. Which means that it see's comma's as the trigger to move to the next column, so you are right. Computernames,IP addresses fed into a .csv file would create the headers of columns in the spreedsheet.

Then the setlocal command - help in the command prompt at, you've probably guessed it, setlocal /?, which in a nut shell allows a variable to be set and used in the for command, you can read the help file for delayed expansion at the end of the "set" helpfile, it can explain it better then I can.

Then if you follow the flow of how response 9 run's it grabs a computer name out of the list.txt, uses the ping command to ping the computername once, and ports the ping command through the "Findstr" command to find the string Reply, the down fall of that is, if the ping gets no reply it will skip over that computer,

But then it sets the Computername to a variable called %%A and the IP address to %%B coming out of the findstr command the IP address has a : at the end, so I set %%B as a delayed variable and used a variable modifier (:~0,-1)to remove the last charactor (:)
then agian used the echo command to port it to the .csv, but this time using double >> to append it to the end of the file, instead of creating a new file.


Report •

#18
August 26, 2009 at 14:51:36
wow!

beautiful too :)

hello gtaion ..

so gtaion, how did u learned all this stuff ... i mean did u had a formal schooling before about this subject or undergone a tutorial session or only by experienced? i really envy people who have those skills the same as yurs ... seems yur so gifted & lucky :)

any way, does it mean that i will input all the Computer Names in list.txt, as well, save in that direction ... a folder named path? that's the way i understand it considering if i'm going to list down all the computer names.


likewise, does the said file list.txt only contains Computer Names without the column for Computername and IP Address or its empty since it will create its own by the said path Echo Computername,IP Address>Final.csv?


lastly, if i'm going to create the set of programs ... do i have to saved it again as a .bat file?

cheers!

p.s. hoping u can show the way the step-by-step procedure on how to manage such program:)

let's build bridges, and not walls :)


Report •

#19
August 26, 2009 at 21:59:59
I put that at the beginning so that it stuck out and would be changed, I never would have thought people would have a file structure such as this on there computer.
set ComputerList=C:\path\to\computer\list.txt
So I would suggest where ever you plan to store this list of computers, you just change the path to the computer list.txt

Also the list of computers to be pinged would be in a plane text file that only containned the list of computers seperated by returns, so it would be simailar to this,
www.google.com
www.yahoo.com
www.computing.net
mycomputer
etc...
Anything that you can type into the ping command you can put into you textfile to have it be pinged, then obviously it will output the IP address into the format of a .csv in the directory that contains the .bat.

As far as being learnt, no I have not had any formal training. I actually have just picked it from either reading the command help files in DOS or googling what I needed to know, I tell you what though until I learned how to use the "FOR" command it made no sense to me how they made %%a do anything at all. Then really I was teaching myself VBScript and started to get bored so I started following some of the post on this site decided that maybe I should teach myself some of this DOS stuff, so I did. And I'll be honest I'm still learning, thats why I'm all excited about when somebody posts something that I haven't seen yet, I ran out of stuff a long time ago to make my job easier, I've automated so much of my job that I get so bored at work, I enjoy the challange of getting some impossible task simplified for somebody, but it's all a learning process, 'cause if you don't use it, you lose it. Also that is why it my take a few tries to get something to work, I don't know all of the in's and outs but i usually try to test something before I post it:)


Report •

#20
September 4, 2009 at 06:27:18
Hi gtaion, ( lol 3rd edit... )

Just came across this post while looking for something and thought I'd give this a try, .

output.txt file has
-'COMPUTERNAME' - ~0,10

which doesn't relate to the actual ip address :(

Regards PMUK


Report •

#21
September 6, 2009 at 12:40:56
PMUK:
I think I've read your post about 5 times, and I'm not totally sure what you are trying to say. If you could elaborate a little I would appreciate it.

Is this what you ran to get your output.txt?

@echo off
set ComputerList=C:\path\to\computer\list.txt

Echo Computername,IP Address>Final.csv
setlocal enabledelayedexpansion

for /f "usebackq tokens=*" %%A in ("%ComputerList%") do (
for /f "tokens=3" %%B in ('ping -n 1 -l 1 %%A ^|findstr Reply') do (
set IPadd=%%B
echo %%A,!IPadd:~0,-1!>>final.csv
))


Report •

#22
September 28, 2009 at 07:55:36
✔ Best Answer
Had a request to have more information added to the output file.

Column 1: Computer name
Column 2: IP if on network, statement if not on network
Column 3: If a folder exist on remote machine or not
Column 4: Remote computer mac address

Obviously for Columns 3&4 you will need to have permissions on the remote computer.
For the folder check, Line 15 will need to be set to what you are looking for.


@echo off

set ComputerList=C:\Path\to\computer\list.txt

Echo Computername,IP Address,Folder Exists,Mac Address>Final.csv
setlocal enabledelayedexpansion

for /f "usebackq tokens=*" %%A in ("%ComputerList%") do (
for /f "tokens=3" %%B in ('ping -n 1 -l 1 %%A') do (
If %%B==could set IPadd=Not On Network.
)
for /f "tokens=3" %%a in ('ping -n 1 -l 1 %%A ^|findstr Reply') do (
set IPadd=%%a
)
If /i "!IPAdd!" NEQ "Not on Network." If Exist \\%%A\C$\Exped (
Set Folder=Yes
) ELSE (
Set Folder=No
)
If /i "!IPAdd!" NEQ "Not on Network." (
for /f "Tokens=1" %%d in ('getmac /s %%A /nh') do set MacAdd=%%d)
echo %%A,!IPadd:~0,-1!,!Folder!,!MacAdd!>>final.csv
)


Report •


Ask Question