Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ping multiple servers and return IP address and Hostnames using batch script?

So I have to use batch only for this. Basically, the server HOSTNAMES are all listed in a txt file. I used the following code to ping all the servers and display their results in a txtfile.

For /f %%i in (testservers.txt) do ping -n 1 %%i >>pingtest.txt

The above pinged all the servers. Now, I want to output the IP addresses and the HOST Names in a separate file. How can I do this? I know that I can run a for loop searching for words like "TTL" and then look for the 3rd token (for the IP) and words like "PINGING" for the second token(HOSTNAME). But I am having errors and cant display it properly. The reason why I want to output IPs and Hostnames in a different file is to make a list of the DOWN and UP servers.

Help will be appreciated. :)

EDIT: Just so it isn't confusing, wanted to let you guys know there are 3 different files, testservers.txt has the HOSTNAMES in it, pingtest.txt has ping results, and result.txt will have the IPs along with Hostnames with their current status as DOWN or UP.

like image 960
Gutsygibbon Avatar asked Sep 13 '12 14:09

Gutsygibbon


People also ask

How do I ping multiple hosts and save results in a test file?

First, put all of your server names into a text file with each server name on a separate line. Let's call it "servers. txt" and save it (as you going to ping server names so make sure name resolution is happening).

How do I ping multiple devices using CMD?

To ping from one device to multiple devices, select Tools > Diagnostics > Ping All Devices From Device.... Move your mouse to the topology map and the cursor will become a crosshair. Click on the desired source device.

How do I ping an IP address multiple times?

Use the command "ping 192.168. 1.101 -t" to initiate a continuous ping. Again, replace the IP address with one specific to your device as needed. The -t can be placed before or after the IP address.


1 Answers

Well, it's unfortunate that you didn't post your own code too, so that it could be corrected.
Anyway, here's my own solution to this:

@echo off
setlocal enabledelayedexpansion

set OUTPUT_FILE=result.txt
>nul copy nul %OUTPUT_FILE%
for /f %%i in (testservers.txt) do (
    set SERVER_ADDRESS=ADDRESS N/A
    for /f "tokens=1,2,3" %%x in ('ping -n 1 %%i ^&^& echo SERVER_IS_UP') do (
        if %%x==Pinging set SERVER_ADDRESS=%%y
        if %%x==Reply set SERVER_ADDRESS=%%z
        if %%x==SERVER_IS_UP (set SERVER_STATE=UP) else (set SERVER_STATE=DOWN)
    )
    echo %%i [!SERVER_ADDRESS::=!] is !SERVER_STATE! >>%OUTPUT_FILE%
)

The outer loop iterates through the hosts and the inner loop parses the ping output. The first two if statements handle the two possible cases of IP address resolution:

  1. The host name is the host IP address.
  2. The host IP address can be resolved from its name.

If the host IP address cannot be resolved, the address is set to "ADDRESS N/A".

Hope this helps.

like image 53
Eitan T Avatar answered Oct 10 '22 05:10

Eitan T