Question

Batch file to ping list of computers

Asked by: makila

How do I create a batch file that will read a csv or txt file as input to ping a list of computers and then output a file of the computers that did not reply? I will not be pinging IP addresses; I will be pinging computer names. I've been using this as a batch script but it takes forever and then I have to hunt for all the bad computer names line by line.

@ECHO OFF                  
ping      ORC11116413      -w      1000      >      ping.txt
ping      ORD11102296      -w      1000      >>      ping.txt

Thanks in advance!
-Makila

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2005-01-05 at 12:53:09ID21263130
Tags

ping

,

batch

,

file

Topic

MS DOS

Participating Experts
4
Points
125
Comments
21

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. Extract Information from ping result (Windows Batch Progr…
    I'd like to write a small batch file that is pinging a host and logs the data in a csv. The CSV looks like that: %date%;%time%;88 // -->88ms I'm not a newbie but I've got a problem extracting the value "ms" from the ping result. ping 10.1.1.2 -n 1 | find &qu...
  2. Batch file - echo a backspace
    I've love to know how to echo a backspace in a batch file. For example: echo Deleting file... del file.txt echo Done ! Will give a two line result not including any result from the delete command. Can I echo a backspace to create a result that would show: Deleteing file....
  3. batch compare
    I would like to have a batch file during logon that compares a patch against a file. ex: -------------------- logon.bat: @echo off start patch.bat ------------------- patch.bat: @echo off setlocal set PATHTOFIXES=\\server\scripts$\qchain\patches %PATHTOFIXES%\kb822150.exe ...
  4. batch file for pinging
    Hi, I am trying to create a simple batch file to use with scheduled tasks in win98se. All I need the file to do is ping my router and then close the window, or ping my router without opening a dos screen if possible. I tried a simple file with "ping 192.168.0.1" ...
  5. Batch script that returns an ip address and a ping reply.
    Does anyone know how to create a batch job to query a specific number of hosts (servers/workstations) and in return it will give me back their ip address and see if it receives a reply back from a ping?
  6. Batch file ping with user input.
    Im relatively new to batch file programming, and need to write a batch file that will run a ping command on a user specified ip address. example: run the batch file, it then asks "What is the ip address?" user inputs the ip, and press enter the batch file proceeds t...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: leewPosted on 2005-01-05 at 12:57:07ID: 12966283

Make the list of computer names, one per line and save it in a text file.  In the example below, I assume the text file is named "names.txt".  Then run this command:

for /f "tokens=1" %a in (names.txt) DO @ping -n 1 -w 100 %a ^| find /i "request" > ping.log

The above command is for running in a command prompt.  If you want to include this in a batch file, use %%a instead of %a

 

by: leewPosted on 2005-01-05 at 13:09:22ID: 12966411

Actually, you probably want to replace the ">" quoted in my example to a ">>" as well.

 

by: makilaPosted on 2005-01-05 at 13:10:36ID: 12966425

I just tried that as a batch file and it gave me this error message:

"The process cannot access the file because it is being used by another process."

I put the batch file (ping.bat) and the input file (names.txt) in the C: directory and ran it from the command prompt window.

 

by: leewPosted on 2005-01-05 at 13:21:59ID: 12966559

DON'T USE PING.BAT - PING is the EXE you need to use.  Windows checks the current directory for the command first - .bat, .exe, .com, etc - if found, it tries to use that one.  So it never gets a chance to run correctly.

rename ping.bat to something like "pinglist.bat"

If you want to get fancy, you could say "pinglist names.txt" then in the script, change the for line to the following:

for /f "tokens=1" %a in (%1) DO @ping -n 1 -w 100 %a ^| find /i "request" >> ping.log

 

by: makilaPosted on 2005-01-05 at 13:27:38ID: 12966611

Ah ha! That worked. The ping.log has a bunch of lines of this error (Bad parameter |.) in between all the bad computer names. How do I get rid of those?

 

by: leewPosted on 2005-01-05 at 13:32:31ID: 12966658

turns out you don't need the "^" symbol before the "pipe - "|" symbol.  To get rid of that error, just remove the "^"

 

by: leewPosted on 2005-01-05 at 13:55:50ID: 12966911

Here, I went a little nuts - see if you like this:

@echo off
If /i "%0" == "ping" Then Goto NameError
If /i "%0" == "if" Then Goto NameError
If /i "%0" == "for" Then Goto NameError
If /i "%0" == "find" Then Goto NameError
If /i "%0" == "type" Then Goto NameError
If /i "%0" == "echo" Then Goto NameError
If /i "%0" == "set" Then Goto NameError
if "%1" == "" Goto Usage
if /i "%1" == "showresults" Goto Usage
if /i "%1" == "alive" Goto Usage
If /i "%1" == "dead" goto usage
Set systemlist=%1
if /i "%2" == "alive" Set Criteria="Reply"
if /i "%3" == "alive" Set Criteria="Reply"
if /i "%4" == "alive" Set Criteria="Reply"
if /i "%2" == "dead" Set Criteria="Request"
if /i "%3" == "dead" Set Criteria="Request"
if /i "%4" == "dead" Set Criteria="Request"
if "%2" == "" SET LogFile=%0.log
if /i "%2" == "showresults" SET LogFile=%0.log
if /i "%2" == "alive" SET LogFile=%0.log
if /i "%2" == "dead" SET LogFile=%0.log
if exist ping.log del ping.log
for /f "tokens=1" %%a in (names.txt) DO @ping -n 1 -w 100 %%a | find /i "%criteria%" >> ping.log
if /i "%4" == "showresults" Type %logfile%
if /i "%3" == "showresults" Type %logfile%
if /i "%2" == "showresults" Type %logfile%
Goto End
:usage
Echo %0 SystemList.txt [logfile.log] [showresults] [alive^|dead]
echo.
echo      Where SystemList.txt is a text file with system names or IPs, one per line.
Echo        And logfile.log is the name of the log file to record the output to.
Echo            Default log file is %0.log
Echo.
Echo        showresults tells the script to display the log file upon script completion.
Echo.
Echo        alive tells the script to output all systems that respond to the ping request.
Echo        dead tells the script to output only the systems that do not respond to the
Echo                ping request.
Goto End
:NameError
Echo The name of this script cannot be "PING" as the "PING" command
Echo is an integral part of the script.  It cannot be named "IF", "FOR",
Echo "SET", "ECHO", "TYPE", or "FIND" either.
:end

 

by: makilaPosted on 2005-01-05 at 14:18:08ID: 12967132

Wow! It didn't work but thanks for trying :) Removing the ^ did the trick for the error message in the log. Thanks so much for your help. I will continue to play around with the nutso script!

 

by: makilaPosted on 2005-01-05 at 14:25:06ID: 12967192

Forgot to mention. This is how I ran it and what the error was from the nutso script:

C:\>pinglist2.bat names.txt ping2.log showresults dead
File not found - REQUEST
The system cannot find the file specified.

 

by: leewPosted on 2005-01-05 at 14:29:29ID: 12967236

Try this one - I think I debugged it:

@echo off
If /i "%0" == "ping" Then Goto NameError
If /i "%0" == "if" Then Goto NameError
If /i "%0" == "for" Then Goto NameError
If /i "%0" == "find" Then Goto NameError
If /i "%0" == "type" Then Goto NameError
If /i "%0" == "echo" Then Goto NameError
If /i "%0" == "set" Then Goto NameError
if "%1" == "" Goto Usage
if /i "%1" == "showresults" Goto Usage
if /i "%1" == "alive" Goto Usage
If /i "%1" == "dead" goto usage
Set systemlist=%1
if /i "%2" == "alive" Set Criteria="Reply"
if /i "%3" == "alive" Set Criteria="Reply"
if /i "%4" == "alive" Set Criteria="Reply"
if /i "%2" == "dead" Set Criteria="Request"
if /i "%3" == "dead" Set Criteria="Request"
if /i "%4" == "dead" Set Criteria="Request"
If "%criteria%" == "" Then Set Criteria="Request"
if "%2" == "" SET LogFile=%0.log
if /i "%2" == "showresults" SET LogFile=%0.log
if /i "%2" == "alive" SET LogFile=%0.log
if /i "%2" == "dead" SET LogFile=%0.log
if exist %logfile% del %logfile%
for /f "tokens=1" %%a in (names.txt) DO @ping -n 1 -w 100 %%a | find /i %criteria% >> %logfile%
if /i "%4" == "showresults" Type %logfile%
if /i "%3" == "showresults" Type %logfile%
if /i "%2" == "showresults" Type %logfile%
Goto End
:usage
Echo.
Echo %0 SystemList.txt [logfile.log] [showresults] [alive^|dead]
echo.
echo      Where SystemList.txt is a text file with system names or IPs, one per line.
Echo        And logfile.log is the name of the log file to record the output to.
Echo            Default log file is %0.log
Echo.
Echo        showresults tells the script to display the log file upon script completion.
Echo.
Echo.
Echo       alive tells the script to output all systems that respond to the ping request.
Echo       dead tells the script to output only the systems that do not respond to the
Echo              ping request.  dead is the default setting.
Goto End
:NameError
Echo The name of this script cannot be "PING" as the "PING" command
Echo is an integral part of the script.  It cannot be named "IF", "FOR",
Echo "SET", "ECHO", "TYPE", or "FIND" either.
:end

 

by: makilaPosted on 2005-01-05 at 14:41:42ID: 12967332

That one works better. The only problem is when I try to use the showresults parameter. It gives this error:

The syntax of the command is incorrect.

Thanks so much for all the help!

 

by: leewPosted on 2005-01-05 at 14:47:59ID: 12967388

Hmmm... without providing me the text file, how exactly are you running the file?

 

by: leewPosted on 2005-01-05 at 14:52:21ID: 12967449

New revision - don't think it will solve your problem, but try it anyway:



@echo off
REM PINGLIST.CMD - REVISION 3
If /i "%0" == "ping" Then Goto NameError
If /i "%0" == "if" Then Goto NameError
If /i "%0" == "for" Then Goto NameError
If /i "%0" == "find" Then Goto NameError
If /i "%0" == "type" Then Goto NameError
If /i "%0" == "echo" Then Goto NameError
If /i "%0" == "set" Then Goto NameError
if "%1" == "" Goto Usage
if /i "%1" == "showresults" Goto Usage
if /i "%1" == "alive" Goto Usage
If /i "%1" == "dead" goto usage
Set systemlist=%1
if /i "%2" == "alive" Set Criteria="Reply"
if /i "%3" == "alive" Set Criteria="Reply"
if /i "%4" == "alive" Set Criteria="Reply"
if /i "%2" == "dead" Set Criteria="Request"
if /i "%3" == "dead" Set Criteria="Request"
if /i "%4" == "dead" Set Criteria="Request"
If "%criteria%" == "" Then Set Criteria="Request"
if "%2" == "" SET LogFile=%0.log
if /i "%2" == "showresults" SET LogFile=%0.log
if /i "%2" == "alive" SET LogFile=%0.log
if /i "%2" == "dead" SET LogFile=%0.log
if exist %logfile% del %logfile%
for /f "tokens=1" %%a in (%systemlist%) DO @ping -n 1 -w 100 %%a | find /i %criteria% >> %logfile%
if /i "%4" == "showresults" Type %logfile%
if /i "%3" == "showresults" Type %logfile%
if /i "%2" == "showresults" Type %logfile%
Goto End
:usage
Echo.
Echo %0 SystemList.txt [logfile.log] [showresults] [alive^|dead]
echo.
echo      Where SystemList.txt is a text file with system names or IPs, one per line.
Echo        And logfile.log is the name of the log file to record the output to.
Echo            Default log file is %0.log
Echo.
Echo        showresults tells the script to display the log file upon script completion.
Echo.
Echo.
Echo       alive tells the script to output all systems that respond to the ping request.
Echo       dead tells the script to output only the systems that do not respond to the
Echo              ping request.  dead is the default setting.
Goto End
:NameError
Echo The name of this script cannot be "PING" as the "PING" command
Echo is an integral part of the script.  It cannot be named "IF", "FOR",
Echo "SET", "ECHO", "TYPE", or "FIND" either.
:end
REM Cleanup
Set Criteria=
Set Logfile=
Set SystemList=

 

by: makilaPosted on 2005-01-05 at 15:00:06ID: 12967519

I ran it all these ways in the command prompt window and they didn't work:

pinglist2.bat names.txt ping2.log showresults
pinglist2.bat names.txt ping2.log showresults alive
pinglist2.bat names.txt ping2.log showresults dead
pinglist2.bat names.txt showresults
pinglist2.bat names.txt showresults alive
pinglist2.bat names.txt showresults dead

But if I leave the showresults out, it works fine. The names.txt is just a file with one computer name per line like:
computer1
computer2
computer3

 

by: leewPosted on 2005-01-05 at 15:05:07ID: 12967556

I'll try it myself later or tomorrow - have to run tonight.  (One of these days I'm going to gather all my scripts and post them on the web.  So I DO want to perfect it at some point).

 

by: DrWarezzPosted on 2005-01-06 at 10:22:32ID: 12974971

Despite Leew getting the well earned points, here's just another script that you can run, it gives a slightly, more user-friendly output:

:====8<-----[ FileName.cmd ]-----
@ECHO OFF
TITLE Network Check

REM -----------------------------------
REM

SET MachineList=names.txt
SET ResultsFile=results.txt

REM
REM -----------------------------------

CLS
ECHO.
IF NOT EXIST "%MachineList%" (
  ECHO Cannot locate Machine List: %MachineList%
  PAUSE>NUL
  GOTO :EOF
)

ECHO Processing all machine names in %MachineList% . . .
ECHO.

FOR /f "tokens=*" %%M in (%MachineList%) do CALL :CHECK "%%M"

GOTO :EOF

:CHECK
SET Machine=%~1
SET Machine=%Machine: =%

PING -w 1000 %Machine%>NUL
IF %ERRORLEVEL% NEQ 0 ECHO %Machine% did not respond at %Time%>>%ResultsFile%

EXIT /B

:EOF
:====8<-----[ FileName.cmd ]-----

HTH
[r.D]

 

by: makilaPosted on 2005-01-06 at 10:49:40ID: 12975281

That worked beautifully! Thank you!

 

by: leewPosted on 2005-01-06 at 10:50:02ID: 12975284

DrWarezz -

I thought I had too many If statements... you know any good tutorials on errorlevels?  

 

by: DrWarezzPosted on 2005-01-07 at 00:32:52ID: 12980899

Glad it helped, makila.

Hey Leew,
erm.. The best 'tutorial' that covers Errorlevels that I've come across, and learnt just about everything I know about them from, is: <at command line> type IF /?

It covers Errorlevels somewhere down there.

But there's really not too much to them. Here's the jist of them:

All programs return a number %ERRORLEVEL% that represents how the program performed. Typically, 0 = successful, -1 = failed. Obviously though, it differs with programs, and certain errorlevels mean different things.. typically.. Then, it's just a case of use 'NEQ', 'EQU', 'GEQ', etc, operators to process the errorlevels.

Here's a rather important note, that I *think* is covered in the 'IF /?' output:

IF ERRORLEVEL 0 ECHO Errorlevel is 0

(for example)

Most people use that command, and believe that it will ECHO "Errorlevel is 0" if the errorlevel *is* 0. However, it doesn't, when you use that syntax, it will perform the task (in this case, ECHO Errorlevel is 0) if the %ERRORLEVEL% is Greater than, or equal to 0. So, it will do that operation even if the ERRORLEVEL is 999. So, just be careful with that one, and you'll figure the rest  ^_^

HTH
[r.D]

 

by: cmsJustinPosted on 2005-02-22 at 10:28:33ID: 13374344

The issue I always run into with errorlevel is when I run pipes. Sometimes it will detect the errorlevel of the wrong pipe. For some reason if %errorlevel%==0 seems to work better than if errorlevel 0.

-Justin

 

by: TBIRD2340Posted on 2009-12-16 at 18:15:45ID: 26068588

DrWarezz:your script is awesome! What code would I need to modify for the results to show me the computers that were up instead of the ones that are down?

Thanks!

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...