Ping multiple host names and record results

I would like to create a method to ping a large number of devices using a list of host names. I would like to do this through a script and then return the details to a file/ excel.
There are a number of postings on this but I can't get any of them to work without taking up all of the memory (Windows 7 4GB) on my machine or taking longer to run than me performing the exercise manually.
I appreciate that there are free tools to do this but I need to create my own as I don't have access to new tools.
Is it possibly to gather the MAC address as well as IP address through this method?
jay_waughAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

matrix8086Commented:
Try a software. Angry IP is an example
jay_waughAuthor Commented:
As I stated on my original question. Sadly I have to create my own method.
Steve KnightIT ConsultancyCommented:
You can only get the MAC address easily of computers on the same subnet, look at the output of ARP-A, e.g. this will ping all hosts in a /24 subnet and then show the arp table:

@echo off
set subnet=192.168.1
for /l %%a in (1,1,254) do PING -n 1 -w 50 %subnet%.%%a
Arp -A > arplist.txt
start arplist.txt

Open in new window


Lots of ways of doing ping's I have lots of similar batch files monitoring systems using PING and TCPING to check specific ports too.  I have put some of them on my site here: http://scripts.dragon-it.co.uk/ search for PING

e.g.

http://scripts.dragon-it.co.uk/links/batch-ping-list-of-hosts-3
http://scripts.dragon-it.co.uk/links/batch-monitor-ping-email

These are mainly for continuous testing, e.g. to then email etc. when one fails to respond but if you can tell us some more info. can make them more specific.

If you just want to read a text file and check each from there, then you could do something like this.  It will work down a text file called pinglist.txt and make an output file pingoutput.csv, change the source & dest to point to different dirs etc. if wanted.

@echo off
set source="pinglist.txt"
set dest="pingoutput.csv"
echo Started at %date% %time% > %dest%
for /f "tokens=1,2 delims=	 " %%a in ('type %source%') do (
  PING %%a -n 1 -w 100 >NUL
  if errorlevel 1 (
    echo %date%,%time%,FAIL,%%a,%%b >> %dest%
    echo %date% %time% FAIL %%a %%b
  ) ELSE (
    echo %date%,%time%,OK,%%a,%%b >> %dest%
    echo %date% %time% OK %%a %%b
  )
)
echo Completed at %date% %time%>> %dest%

Open in new window


As it stands it will show on the screen:

Started at Tue 05/05/2015 17:14:51.57
Tue 05/05/2015 17:14:51.57 OK 128.127.1.1 router
Tue 05/05/2015 17:14:51.57 OK 128.127.1.200 jamie
Tue 05/05/2015 17:14:51.57 FAIL 8.3.1.4 fail
Completed at Tue 05/05/2015 17:14:52.02

Open in new window


And in the log file:

Tue 05/05/2015,17:14:51.57,OK,128.127.1.1,router
Tue 05/05/2015,17:14:51.57,OK,128.127.1.200,jamie
Tue 05/05/2015,17:14:51.57,FAIL,8.3.1.4,fail

Open in new window


The pinglist.txt file needs to have:

x.x.x.x  hostname
y.y.y.y  hostname

i.e. IP address and space or tab then host name for it.

Steve
Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

jay_waughAuthor Commented:
Thanks for the response. I have tried the batch-ping-check-hosts from your website for 15 host names and this has taken over an hour. If I perform the same effort manually it takes less than a minute. I am using a Windows 7 machine. Is there anything else I need to be aware of?
Steve KnightIT ConsultancyCommented:
I don't understand.  Please show us what you are running exactly and the output, I don't understand why anything should take longer than the number of checks, pings and hosts...

By default a PING of 15 hosts that aren't responding would be about a minute at 4 checks each.

Have you tried the one I posted above?

Steve
jay_waughAuthor Commented:
Steve

I tried:-

echo off
 set ServerFile=c:\temp\servers.txt
 set LogFile=c:\temp\pinglog.txt
 for /f "tokens=1*" %%A in ('type "%ServerFile%"') do (
   ping -n 1 %%A | find "Lost = 0 " >NUL
   if ERRORLEVEL 1 (
     echo %DATE% %TIME% Bad:  %%A        %%B>>"%LogFile%"
       ) else (
     echo %DATE% %TIME% Good: %%A        %%B>>"%LogFile%"
   )
 )

I added my target hostnames to a server.txt file which was done in the format:-

Server1
Server2
Server3

I was unable to use the version you posted as my goal is to find IP Addresses for a list of Hostnames.

I am not receiving any output from the executed batch file.
Steve KnightIT ConsultancyCommented:
Right, re-reading I see what you mean now.  do you actually want to PING them or just resolve their names using DNS, i.e.  NSLOOKUP servername

Steve
jay_waughAuthor Commented:
To be honest I think that the NSLOOKUP option would be best and then to return the results to a file.

I need to do this action for circa 1000 host names.
Steve KnightIT ConsultancyCommented:
Ok try this then, needs to exclude the lines of output from NSLookup that aren't needed, specifically the DNS server address and the other ones mentioned in the find /v statements.

Output should be of the form:

"Name","IPAddresses"
"www.google.co.uk","2a00:1450:4009:80c::2003","216.58.210.3"
"dragon-it.co.uk","195.26.89.230"
"scripts.dragon-it.uk","195.26.89.230","scripts.dragon-it.co.uk"
"gmail.com","2a00:1450:4009:80c::2005","216.58.210.5"

Open in new window


Could be combined with actually pinging the addresses I suppose too.

Steve

@echo off
setlocal enabledelayedexpansion

set ServerFile="c:\temp\servers.txt"
set LogFile="c:\temp\pinglog.txt"
set DNSServer=8.8.8.8
set result=
(
echo "Name","IPAddresses"
for /f "tokens=1*" %%A in ('type "%ServerFile%"') do (
   echo Checking address for %%A > CON
   set result=
   for /f "tokens=2 delims= " %%I in ('nslookup %%A %dnsserver% 2^>NUL ^| find /v "Server:" ^| find /v "Name:"^| find /v "Aliases:" ^| find /v "%dnsserver%"') do set result=!

result!,"%%I"
   echo "%%A"!result!
))> %LogFile%
start notepad %logfile%

Open in new window

Steve KnightIT ConsultancyCommented:
Here is a version which uses NSLOOKUP to resolve each name, add a line to the file for each IP Address it returns and then an OK/FAIL for PING response:

rem @echo off
setlocal enabledelayedexpansion

set ServerFile="servers.txt"
set LogFile="pinglog.txt"
set DNSServer=8.8.8.8
set result=

(echo "Name","IPAddresses"
for /f "tokens=1*" %%A in ('type "%ServerFile%"') do (
   echo Checking address for %%A > CON
   for /f "tokens=2 delims= " %%I in ('nslookup %%A %dnsserver% 2^>NUL ^| find /v "Server:" ^| find /v "Name:"^| find /v "Aliases:" ^| find /v "%dnsserver%"') do (
     ping -n 1 %%I | find "Lost = 0 " >NUL
     if errorlevel 1 (
        echo "%%A","FAIL","%%I"
     ) ELSE (
        echo "%%A","OK","%%I"
     )
   )
))> %LogFile%

start notepad %logfile%

Open in new window


giving you:

"Name","IPAddresses"
"www.google.co.uk","FAIL","2a00:1450:4009:80c::2003"
"www.google.co.uk","OK","216.58.210.3"
"dragon-it.co.uk","FAIL","195.26.89.230"
"scripts.dragon-it.uk","FAIL","195.26.89.230"
"scripts.dragon-it.uk","FAIL","scripts.dragon-it.co.uk"
"gmail.com","FAIL","2a00:1450:4009:80c::2005"
"gmail.com","OK","216.58.210.5"

Open in new window


for this servers.txt:

www.google.co.uk
dragon-it.co.uk
scripts.dragon-it.uk
gmail.com

Steve

[Edited : pasted wrong versions]
jay_waughAuthor Commented:
Hi Steve,

Apologies to change the original request but on second thoughts I believe that ping would be better than nslookup. I have decided this as multiple DNS servers are in play.

On a separate note I have not been able to get the script to move on to the second hostname in my servers.txt

Is there a particular format that I must use in my text file?

I have been using the following format.

server1
server2
server3

Thanks
Steve KnightIT ConsultancyCommented:
Strange.  The above does PING and NSLOOKUP each too.  I suggest you use one DNS server as your client will only use one DNS server to do the queries anyway.

Can you show us the script you are using if you have changed it, and what output your are getting to the screen and to the log file.  You can remove the word @echo off from the top of the batch file to see more what is going on as it runs

Steve
Steve KnightIT ConsultancyCommented:
BTW I add this to my scripts database too:

http://scripts.dragon-it.co.uk/links/batch-lookup-hosts-and-ping

There is a copy of the batch and test files there.  Perhaps try it with those files (which make the output shown) and then try it with your server list.

Steve
jay_waughAuthor Commented:
Thanks again Steve. I can only assume that it is security restrictions that are preventing these batch files from executing.
Steve KnightIT ConsultancyCommented:
if you can show us the out oupuits and scripts that you are using can help you debug.
jay_waughAuthor Commented:
Enclosed is the script I used:-

rem @echo off
setlocal enabledelayedexpansion

set ServerFile="c:\temp\servers.txt"
set LogFile="c:\temp\pinglog.txt"
set DNSServer=1.1.1.1
set result=

(echo "Name","IPAddresses"
for /f "tokens=1*" %%A in ('type "%ServerFile%"') do (
   echo Checking address for %%A > CON
   for /f "tokens=2 delims= " %%I in ('nslookup %%A %dnsserver% 2^>NUL ^| find /v "Server:" ^| find /v "Name:"^| find /v "Aliases:" ^| find /v "%dnsserver%"') do (
     ping -n 1 %%I | find "Lost = 0 " >NUL
     if errorlevel 1 (
        echo "%%A","FAIL","%%I"
     ) ELSE (
        echo "%%A","OK","%%I"
     )
   )
))> %LogFile%

start notepad %logfile%


I only receive any output for my first server hostname. My computer's resources are used up significantly by many cmd.exe and find processes that start running. Confused by this also as there are only 2 server hostnames in my Servers.txt file.

I have also included the output in the pinglog.txt.(but have removed the actual hostnames and IP Addresses.

"Name","IPAddresses"

C:\temp>(
echo Checking address for Server1  1>CON  
 for /F "tokens=2 delims= " %I in ('nslookup Server1  1.1.1.1 2>NUL | find /v "Server:" | find /v "Name:"| find /v "Aliases:" | find /v "1.1.1.1"') do (
ping -n 1 %I   | find "Lost = 0 "  1>NUL  
 if errorlevel 1 (echo "Server1  ","FAIL","%I" )  ELSE (echo "Server1  ","OK","%I" )
)
)

C:\temp>(
ping -n 1 1.1.1.1   | find "Lost = 0 "  1>NUL  
 if errorlevel 1 (echo "Server1  ","FAIL","11.1.1.1" )  ELSE (echo "Server1  ","OK","1.1.1.1" )
)

Only a slightly separate note I am looking to run this script for circa 1000 devices so it would be very useful for this pinglog.txt output to be formatted in a list that could be copied into Excel. Do you think that is possible?

Thanks
Steve KnightIT ConsultancyCommented:
What did you call the script, not ping.cmd or ping.bat by any chance?

Steve
jay_waughAuthor Commented:
no BLAH3.bat
Steve KnightIT ConsultancyCommented:
Will try script as posted in a bit to make sure no errors crept in. Normally multiple issues like that is due to running something within the script that re runs itself, i.e. infinite loop.

It is already output to the log in csv format for excel.

Another way is to get excel to do the work... List them in excel and use CBA's to do the checking. Is that an option?
Steve KnightIT ConsultancyCommented:
That was VBA... Phone autoinfection..
 Pah auto correcting...
jay_waughAuthor Commented:
Ok thanks for that.

Should be able to direct through Excel if that's an option too? Sounds tidier if you could help with that, that would be great.
Steve KnightIT ConsultancyCommented:
Will have a go if I have time. Would suggest btw something odd going on with your machine there.  Can you try the script on a different machine perhaps for starters.  The exact script above changing DNS server to say 8.8.8.8 and using servers.txt with three random host names/ip's and it works...putting the @echo off back it produces just the CSV output.

Start a CMD prompt.
Run the script.  Still fails?
If so
ping 1.1.1.1
Anything odd...
dir | find "."
Anything odd?
Steve KnightIT ConsultancyCommented:
Was writing some code from bits I have written and other snippets and a quick showed someone has already invented the wheel... The page below has some routines which will get IP from name, name from IP and PING as VBA which you can use in cells, or as I have in a tiny script to loop through all the cells - if you add loads in formulas in the cells might be a bit unpredicatble.  The code I have put in behind the refresh button is just this:

Row = 2
Do
    If Cells(Row, 1) = "" Then Exit Sub
    Cells(Row, 2) = GetIpAddress(Cells(Row, 1).Text)
    Cells(Row, 3) = Ping(Cells(Row, 1).Text)
    Cells(Row, 4) = Now()
    Row = Row + 1
Loop

Open in new window


Page with script on (already in the attached sheet):
http://www.codeproject.com/Articles/712335/Followup-DNS-Lookup-and-Ping-in-Excel
PingIt.xlsm

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jay_waughAuthor Commented:
Looks good,

Sadly having trouble with the Hostent and IN_ADDR dimensions at the moment. Looking on the internet to see if it's my Win7 installation.
jay_waughAuthor Commented:
Correction, It works perfectly. Your continued engagement has been greatly appreciated
Steve KnightIT ConsultancyCommented:
No problem, will keep a copy of that in my tools too... nice quick check of all site's kit being up when no monitoring of their own.

Steve
jay_waughAuthor Commented:
Steve

I have since learned that the code we discussed fails on the line
"GetValue hHostent.h_name, GetHostByName(Hostname + String(64 - Len(Hostname), 0)), Len(hHostent)" (Excel then restarts at this point.)

If the Hostname returns "Ping request could not find host" when pinged this seems to trigger this outcome. The Hostname I am trying to ping in this instance is valid but is from a different domain. If I am to include the fully qualified domain name then all is well and the ping application completes successfully.

I have tried to add a loop around the "GetValue hHostent.h_name, GetHostByName(Hostname + String(64 - Len(Hostname), 0)), Len(hHostent)" line to include a call to a list of alternative domains which can be added to the end of the hostname and as such increase the coverage of the hostname, ping attempt.

the logic being:-

For Domain 1
If iserror(ping(hostname&Domain)) then Endif
Else
IPAddress = result(ping(hostname&Domain)
End if
Next

Can you help confirm the code to deliver this please?

Many Thanks
Steve KnightIT ConsultancyCommented:
Have seen this and message but have been busy on customer sites.  More of the same (well different site) tomorrow but will look back when I can Thurs/Fri.

Sounds like you might need completion domains.  Have a look in your IPv4 settings in network card.  Then in the Advanced button, go to DNS tab and instead of saying to append primary and connection specific + parent domain names then use "append these dns suffixes in order".  Make sure you add your AD domain ones if you need to and then add others in the order you want to try them.

Steve
Steve KnightIT ConsultancyCommented:
Should have said that means.... if you ping ABC123 it will try

ABC123.domain1.co.uk
ABC123.domain2.com
ABC123.domain3.whatever

and the first one to resolve will be returned to the PING for working with.

Steve
jay_waughAuthor Commented:
Thanks again Steve, but sadly I don't have the opportunity to alter DNS settings so the VB option is the only one available.

Cheers
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft DOS

From novice to tech pro — start learning today.