Ping list of computers from txt file

July 13, 2010 at 07:17:08
Specs: Windows Vista
Continuing from post http://www.computing.net/answers/pr...
-----------------------------------------------------------
I need to try and modify the script below to tell me if the machine was pingable or not. This script doesn't work well if the machine's IP address was cached in the DNS server.


@echo off
set ComputerList=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
))


See More: Ping list of computers from txt file

Report •

#1
July 13, 2010 at 10:28:33
You can't nest For loops like this because variables (%%a) created in the first For Loop are local and can not be seen by the second For Loop. The solution is to setup a subroutine like so...

@echo off
set ComputerList=list.txt

Echo Computername,IP Address>Final.csv
setlocal enabledelayedexpansion

for /f "usebackq tokens=*" %%A in ("%ComputerList%") do (
  call :sub1 %%A
)
goto :EOF

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


Report •

#2
July 13, 2010 at 14:12:50
Unfortunately the best way to do this code is with a subroutine.

@echo off
setlocal ENABLEEXTENSIONS

set OutputFile=result.txt
set ListFile=servers.txt

echo Ping Test > "%OutputFile%"
for /f %%a in (%ListFile%) do call :Sub %%a
notepad "%OutputFile%"
goto :eof

:Sub
  set state=Pingable
  ping %1 -n 1 > NUL
  if ERRORLEVEL 1 set state=No Reply
  echo %1 is %state% >> "%OutputFile%"
goto :eof


Report •

#3
July 13, 2010 at 15:35:13
@ace, are you sure? I've never had a problem like you described. His issue may be something else altogether...

Report •

Related Solutions

#4
July 14, 2010 at 07:44:27
True, I have no Idea What he is doing with the "final.csv" file. Also, if he is having DNS issues then you could simply put in a...

ipconfig /flushdns
ipconfig /registerdns

to reload the DNS entries to the local cache of the computer. If his computer is not seeing the DNS then he has other issues like his DHCP server not being configured properly. His code is all I have to go on until he gives us more details.


Report •

#5
July 14, 2010 at 08:16:16
I work at a helpdesk and do a lot of remote administration and sometimes we get issues with a list of machines and I have their computer names. I am trying to ping all of the machines in a list and resolve thier IP address and if they are currently active. The problem I had with the script from the previous forum posting was that is got the information from the DNS server with the last known IP address and didn't accurately tell me that the machine wasn't available.

Report •

#6
July 14, 2010 at 08:47:30
Why do you need scripts to do this. Why not just use a free IP scanner like NMAP or IPscan? ...

I use this one all of the time...

http://www.eusing.com/ipscan/free_i...
http://nmap.org/zenmap/

it can even do port scanning.

I only use the above script in an alert program that I run on the server that will alert me of a server going down.


Report •

#7
July 14, 2010 at 08:50:34
You probably haven't gotten much help from the long-timers because we keep getting some variation of this question, for years.

Report •

#8
July 14, 2010 at 09:10:00
I work on government networks and can't use programs like that.
Just trying to make a script when doing a lot of remote admin stuff, to see which machines are available instead of doing a ping everytime for one machine.

Report •

#9
July 14, 2010 at 09:58:29
And the script I provided you above does not work? If so what is the error you are getting?

Are you still having DNS issues with the script? If so did you try Response Number 4 to refresh the cache?

"was that is got the information from the DNS server with the last known IP address and didn't accurately tell me that the machine wasn't available"

Can you explain this? Do you want to get a list of IPs to ping from the DNS server? Is that the issue?

Simply define exactly what it is want the script to do.

"I work on government networks and can't use programs like that."

Also if you are working on a government system you should not be running scripts like this with out admin approval because they might be monitoring ICMP traffic.

If it is just a problem of installing then try the first App, I gave you. It is stand alone and does not need to be installed. It will run just by double clicking on it.


Report •

#10
July 14, 2010 at 12:25:57
I am an admin. We can't use unauthorized software whether or not it requires installation.

Scenario:
I have a list of 50 machines on the network that I have computer names for. I don't know if they are on or not. I don't know the IP addresses either(which doesn't really matter). I need to ping the machines and see if they are available before I remotely connect to them to patch them. I want the script to make a csv file that will have the computername,IP address,ping status. If it isn't reachable, then I need to call someone there to turn the machine on. Right now, we ping each machine independently.

Here's what I am using now, but it for some reason keeps listing the same IP address.

@echo off

set ComputerList=list.txt

Echo Computername,IP Address,State>Final5.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
for /f %%A in (%ComputerList%) do call :Sub %%A
echo %1,!IPadd:~0,-1!,!state!>>final5.csv
))

:Sub
set state=Pingable
ping %1 -n 1 > NUL
if ERRORLEVEL 1 set state=No Reply


Report •

#11
July 14, 2010 at 15:20:49
Try this...

@echo off
setlocal ENABLEEXTENSIONS

set OutputFile=final.csv
set ListFile=list.txt
echo y|del %OutputFile%

for /f %%a in (%ListFile%) do (
  call :Sub %%a)
type "%OutputFile%"
pause
goto :eof

:Sub
  set IP=0.0.0.0
  set state=Pingable
  ping %1 -n 1 > NUL
  if ERRORLEVEL 1 set state=No Reply
  if "%state%"=="Pingable" call :RDNS %1
  echo %1,%IP%,%state% >> "%OutputFile%"
goto :eof

:RDNS
 for /f "tokens=1,2 delims= " %%A in ('nslookup %1') do (
  if "%%A"=="Address:" call :SetIP %%B
  if "%%A"=="Addresses:" call :SetIP %%B)
goto :eof

:SetIP
  set IP=%1  
goto :eof

I could not get it to work with computers that had multiple NICs and IPs


Report •

Ask Question