Batch file to ping host names

Lee HLee H Member Posts: 1,135
Hi

How can you ping multiple host names and send the results to a txt file, could simply past the host names into the batch file then run it

Thanks in advance

Lee
.

Comments

  • HeroPsychoHeroPsycho Inactive Imported Users Posts: 1,940
    PowerShell is your friend.

    $computers = get-content c:\path\listofcomputers.txt
    $computers | foreach-object {ping $_} | out-file results.txt

    You may want to check out the PowerShell Community Extensions, which actually has a ping-host cmdlet within it, so you technically don't even need to use foreach-object. You could also use that to export the results to a CSV for easier processing of the results.

    Another way to do this via PowerShell is using WMI. One of the WMI classes is Win32_PingStatus.

    http://www.computerperformance.co.uk/powershell/powershell_win32_pingstatus.htm

    Don't forget PowerShell can also be handy for generating the list of computers to ping using calls to Active Directory, or allowing you to generate IP lists quickly. For example...

    $ips = 1..254 | %{"192.168.1." + $_}
    $ips |%{ping $_} | out-file results.txt

    Hope this helps!
    Good luck to all!
  • Lee HLee H Member Posts: 1,135
    thanks hero, sounds good

    my colleague in work was trying to do it using nslookup, he had about 8 host names and wanted to know there IP's, instead of pinging individual he was trying to generate a txt file using nslookup

    any idea what he should have been doing

    thanks

    Lee
    .
  • HeroPsychoHeroPsycho Inactive Imported Users Posts: 1,940
    He should use whatever gets the job done. icon_wink.gif

    I would have used PowerShell with the PowerShell Community Extensions' resolve-host cmdlet.

    $computers = get-content c:\path\listofcomputers.txt
    #Or use $computers = @("www.yahoo.com","www.google.com")
    resolve-host $computers

    At this point, you'd have gotten a return like this...

    HostName Aliases AddressList


    www-real.wa1.b.yahoo.com {} {69.147.76.15}
    Google {} {208.69.32.231, 208.69.32.230}

    If you're not interested in let's say aliases, you could do...

    resolve-host $computers | select-object hostname,addresslist

    To generate a file out of this information to keep, you could tack on | outfile file.txt or | export-csv file.csv.

    Hope this helps!
    Good luck to all!
  • RobertKaucherRobertKaucher Member Posts: 4,299 ■■■■■■■■■■
    You can do all sorts of things with PowerShell and ping or nslookup. Because the results are returned as objects you can do boolean checks for success or timeouts and even conditionally format an Excel spread sheet to use a green check image for success and a red stop sign for timeouts.
Sign In or Register to comment.