Batch script that returns an ip address and a ping reply.

Posted on 2005-05-03
Last Modified: 2010-05-18
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?
Question by:mystikal1000
    LVL 9

    Expert Comment

    This might be what your looking for:

    I could do it with a few more steps and file edits.  Create a list of IPs in excel, save as CSV file.  open it with notepad and replace commas with dots.  save it as "ips.txt".  From DOS, type "for /F %i in (c:\ips.txt) do ping -n 1 %i | find "Reply""

    You could also do a "net view" which will return all workstations/servers in the domain.  A few edits and you could have a list that you could use just like the ips.txt file above.

    Author Comment

    I also want to know if the host is pingable or not.

    Down below is what I have, but it only retrieves the ip address and host name, not ping reply or request time out...
    Does anyone know?

    @echo off
    del /q ServerIP.csv

    for /F "delims=" %%x in (servers.txt) DO CALL :PING %%x
    goto :EOF

     echo Server : %1%
     ping -n 1 -w 10 %1 | for /f "usebackq tokens=2 delims=[]" %%y in (`find /i "%1"`) DO @echo %1;%%y >> ServerIP.txt
    goto :EOF
    LVL 9

    Expert Comment

    If you're just trying to see what replies, The line I posted above will do that.  Here's the output:

    Microsoft Windows XP [Version 5.1.2600]
    (C) Copyright 1985-2001 Microsoft Corp.

    C:\>ping -n 1 | find "Reply"
    Reply from bytes=32 time=107ms TTL=114

    C:\>ping -n 1 | find "Reply"


    Author Comment

    No, I also want the request time outs too.

    I want a txt file that has all the hosts I want to ping.  I also want a batch file that will take the txt file and capture replies, request timeouts and hostname.
    LVL 9

    Accepted Solution

    OK.  If you remove the find statement, do this:

    "for /F %i in (c:\ips.txt) do ping -n 1 -a %i >> c:\results.txt"

    All results will be redirected to c:\results.txt.  The ping will attempt to resolve the hostname through DNS.  If there is not a PTR record for the IP, no name will be returned.  Maybe you can provide an example of what your text file would ideally look like.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
    One of my most closely kept secrets is revealed in this discussion How to output text on the same line This question was recently posted in EE by Simon336697 (…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    759 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

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now