26

I'm aware of the methods where you can run a Bash for loop and ping multiple servers, is there a Linux CLI tool that I can use which will allow for me to do this without having to resort to writing a Bash script to ping a list of servers one at a time?

Something like this:

$ ping host1 host2 host3

NOTE: I'm looking specifically for CentOS/Fedora, but if it works on other distros that's fine too.

5
  • 8
    Why are you against a little one-liner script doing that -- the beauty and philosophy of Linux ;-)? (Have small, simple programs and write the glue for special needs yourself.) – Peter - Reinstate Monica Jun 15 '19 at 14:24
  • 1
    It's far easier to install a package from a repo vs. copying a script to 2000+ VMs 8-). I could easily write a script solution for myself. Also since I self-answered this I'm creating content on the Internet since I have users whom are asking for a single cmd vs. a shell script and were unsuccessful in finding a simple A'er to what they perceive as a basic Q. – slm Jun 15 '19 at 15:55
  • 3
    If your intent is to do a multi-ping from 2000+ VMs, ... I still can only scratch my head – Hagen von Eitzen Jun 15 '19 at 19:30
  • @HagenvonEitzen - nope, I'm installing tooling on 2000+ VMs so that users have tooling available to them to do such. – slm Jun 15 '19 at 20:21
  • 1
    Why would you need this? – Tvde1 Jun 17 '19 at 10:24

12 Answers 12

33

If you look into the NMAP project you'll find that it includes additional tools on top of just nmap. One of these tools is nping, which includes the following ability:

Nping has a very flexible and powerful command-line interface that grants users full control over generated packets. Nping's features include:

  • Custom TCP, UDP, ICMP and ARP packet generation.
  • Support for multiple target host specification.
  • Support for multiple target port specification.
  • ...

nping is in the standard EPEL repos to boot.

$ repoquery -qlf nmap.x86_64 | grep nping
/usr/bin/nping
/usr/share/man/man1/nping.1.gz

Usage

To ping multiple servers you merely have to tell nping the names/IPs and which protocol you want to use. Here since we want to mimic what the traditional ping CLI does we'll use ICMP.

$ sudo nping -c 2 --icmp scanme.nmap.org google.com

Starting Nping 0.7.70 ( https://nmap.org/nping ) at 2019-06-14 13:43 EDT
SENT (0.0088s) ICMP [10.3.144.95 > 45.33.32.156 Echo request (type=8/code=0) id=42074 seq=1] IP [ttl=64 id=57921 iplen=28 ]
RCVD (0.0950s) ICMP [45.33.32.156 > 10.3.144.95 Echo reply (type=0/code=0) id=42074 seq=1] IP [ttl=46 id=24195 iplen=28 ]
SENT (1.0091s) ICMP [10.3.144.95 > 45.33.32.156 Echo request (type=8/code=0) id=42074 seq=2] IP [ttl=64 id=57921 iplen=28 ]
SENT (2.0105s) ICMP [10.3.144.95 > 45.33.32.156 Echo request (type=8/code=0) id=42074 seq=2] IP [ttl=64 id=57921 iplen=28 ]
RCVD (2.0107s) ICMP [45.33.32.156 > 10.3.144.95 Echo reply (type=0/code=0) id=42074 seq=2] IP [ttl=46 id=24465 iplen=28 ]
SENT (3.0138s) ICMP [10.3.144.95 > 64.233.177.100 Echo request (type=8/code=0) id=49169 seq=2] IP [ttl=64 id=57921 iplen=28 ]

Statistics for host scanme.nmap.org (45.33.32.156):
 |  Probes Sent: 2 | Rcvd: 2 | Lost: 0  (0.00%)
 |_ Max rtt: 86.053ms | Min rtt: 0.188ms | Avg rtt: 43.120ms
Statistics for host google.com (64.233.177.100):
 |  Probes Sent: 2 | Rcvd: 0 | Lost: 2  (100.00%)
 |_ Max rtt: N/A | Min rtt: N/A | Avg rtt: N/A
Raw packets sent: 4 (112B) | Rcvd: 2 (108B) | Lost: 2 (50.00%)
Nping done: 2 IP addresses pinged in 3.01 seconds

The only drawback I've found with this tool is the use of ICMP mode requiring root privileges.

$ nping -c 2 --icmp scanme.nmap.org google.com
Mode ICMP requires root privileges.
5
  • 3
    What's with that output? The statistics say it sent 2 to each host, but the previous output shows it sent 3 to scanme.nmap.org (45.33.32.156) and 1 to google.com (64.233.177.100). It happens to me too when I run it. – JoL Jun 15 '19 at 2:26
  • 4
    It lies... (of course though, since it's inconsistent with itself). On running it with strace, I can see it sending a ping to google.com, but outputting a line saying it sent it to scanme.nmap.org. Interesting bug. It doesn't always happen, though. – JoL Jun 15 '19 at 2:34
  • nmap also supports ICMP directly by specifying option -sn. See my answer for details. – scai Jun 17 '19 at 11:27
  • @scai - thanks for your A'er. I found that one as well while researching this. I found my approach to be the cleanest. – slm Jun 17 '19 at 11:28
  • 1
    'The only drawback I've found with this tool is the use of ICMP mode requiring root privileges.' Yes that's an interesting thing but it has to do with capabilities (in Linux at least; you could also do setuid exec for root). Not that I recommend using setcap or giving it setuid .... – Pryftan Jun 18 '19 at 14:58
20

fping is in a Fedora package of the same name, and allows for many hosts, or a set of ip addressses.

$ fping -a -A -c 1 hosta hostb
192.168.0.20 : xmt/rcv/%loss = 1/1/0%, min/avg/max = 0.64/0.64/0.64
192.168.1.3  : xmt/rcv/%loss = 1/1/0%, min/avg/max = 0.50/0.50/0.50

fping will send out a ping packet and move on to the next target in a round-robin fashion... if a target replies, it is noted and removed from the list

10
oping host1 host2 host3

Description:

oping uses ICMP packages (better known as "ping packets") to test the reachability of network hosts. It supports pinging multiple hosts in parallel using IPv4 and/or IPv6 transparently.

This package contains two command line applications: "oping" is a replacement for tools like ping(1), ping6(1) and fping(1). "noping" is an ncurses-based tool which displays statistics while pinging and highlights aberrant round-trip times.

1
  • Just to be pedantic: technically ICMP is for error reporting in general and there are many different capabilities it has. In fact ping uses ECHO_REQUEST/ECHO_REPLY types of ICMP (8 for the former iirc but it's been a long time - and I’m too lazy to check). Maybe the description you quote is actually for the oping itself but either way it's not strictly correct (or it's much more simplified than it should be ... Or maybe not, I guess for many it probably is better that way but not for me anyway). – Pryftan Jun 18 '19 at 15:01
9

I would suggest using GNU Parallel

parallel -u ping ::: host1 host2 host3

output will be interleaved

1
  • 2
    Good answer and thank you for bringing our attention parallel. It allows me to use my custom ping script in parallel which is exactly what I needed. – Marian Minar Apr 12 '20 at 14:33
6

I know it's specifically not what you are asking for, but a bash script to accomplish this:

#!/bin/bash

for host; do
    ping -c5 "$host" 2>&1 | tail -3 &
done

wait

This will take your endpoints as command line arguments and send a 5 count ping to each one as a background process and then wait for all to finish before exiting. It will print the last three lines of the ping output which contains useful stats about the success rate and latency.

7
  • 3
    You don't need to remember the pids. A simple wait will wait for all active child processes, so you can avoid the second loop. I think part of the reason why it was asked to not require writing a bash script is to have a solution that works on any machine without having to carry around or write a script. Making it short seems more favorable, so I would think it better to avoid that argument assignment and just do for host; do in your first loop. If this were zsh, you could avoid the do and done and just do, interatively: for host in google.com; ping -c5 "$host" |& tail -3 & wait – JoL Jun 15 '19 at 2:51
  • 4
    A SIGINT trap would make this script a lot more practical. – Gilles 'SO- stop being evil' Jun 15 '19 at 9:14
  • 2
    For interactive use, this is simply using background command (i.e. command &) in a subshell (i.e. wrapping with parens) and with wait to combine any number of commands into one artificial command, for example: (for f in google.com yahoo.com microsoft.com; do ping $f & done; wait). Their outputs will be interleaved, and when you ctrl+c this after this command, all three child processes will be killed. The shell has a lot of powerful job control constructs and this is one of them. – Lie Ryan Jun 15 '19 at 10:12
  • 2
    Thanks all. Yeah I was hesitant to post it but I figure Q/A's on here are not just for OP but for everyone experiencing a similar problem and I figure there are going to be people that stumble across this question and don't want to install additional software to accomplish the task. – jesse_b Jun 15 '19 at 13:08
  • @JoL But certainly those tools aren't installed by default on all hosts. That doesn't make what you say untrue of course but... – Pryftan Jun 18 '19 at 15:03
2

I suppose this can do for you ?

eval $(printf 'ping "%s" & ' host1 host2 host3)

It takes advantage of printf's ability to "auto-iterate" its arguments while re-using its format string over each argument. The above printf therefore produces a sequence of ping <hostname> & strings for each host provided as argument, and feeds such sequence of commands through Command Substitution to the eval command to have them executed immediately.

printf and eval commands are POSIX standard, as well as Command Substitution.

Enclosing whole such command in a subshell comprising an and-ed wait like this:

(eval $(printf 'ping "%s" & ' host1 host2 host3) && wait)

provides the ability to interrupt everything at will with a simple Ctrl+C.

Else you can control each ping command singularly through the shell's usual job control.

If your shell has support also for Process Substitutions, you may also use the following:

. <(printf 'ping "%s" & ' host1 host2 host3)

for a few chars less to type.

The gist is the same as for the eval, but feeds the sequence of pings to the . (aka source) command through the Process Substitution.

2

Nmap supports ping scans (ICMP) and multiple hosts:

nmap -sn -n 127.0.0.1 8.8.8.8

You can also create a file containing all of your target IPs (separated by spaces or newlines) called targets.txt. Then run:

nmap -sn -n -iL targets.txt

Options explained:

  • -sn Ping Scan.
  • -n Disable DNS resolution.
  • -iL Input file name.

Other interesting options in case you want to ping a really large number of targets:

  • -T4 Increase timing to reduce scan duration.
  • --min-parallelism 100 Increase number of parallel probes.
  • -oG <file> Write scan results to file in Grepable format.

Without creating a file

Keep in mind that you can also forgo the creation of a file and use a - to take input from either a pipe, |, or via traditional methods for redirecting output via STDIN.

Examples:

$ ( echo www.google.com; echo www.yahoo.com ) | sudo nmap -sn -n -iL -

-or-

$ sudo nmap -sn -n -iL - < <(echo www.google.com; echo www.yahoo.com)

-or-

$ sudo nmap -sn -n -iL - <<< $'www.google.com\nwww.yahoo.com'

References

2

By using the common xargs command to build an execute multiple ping requests:

echo host1 host2 host3 | xargs -n1 -P0 ping -c 4

Where host1 host2 host3 can be a variable number of hosts (any combination of IP or hostname).

This alters the xargs defaults to force 1 input argument per ping execution, and allow an unlimited number of parallel child processes (1 per pinged host). It is probably wise to set -P (aka --max-procs) to a sane value if intending to ping a large number of hosts (they'll all be processed; just fewer simultaneously).

Its short enough to use directly, could be added as a function to your shell profile or rc file, or turned into tiny script in your $PATH. In the examples below, -P has been set to 10 to avoid excessive resource consumption.

Example script: /usr/bin/mping

#!/bin/sh

echo $@ | xargs -n1 -P10 ping -c4

Example function within ~/.bashrc

function mping() {
    echo $@ | xargs -n1 -P10 ping -c4
} 

And use as:

mping host1 host2 host3 ...
2
  • 1
    You might consider using -w 2 to make ping wait no longer than 2 seconds. The default is 10 seconds, which is per-invocation of ping, so this could take 30+ seconds to complete. – Criggie Jun 18 '19 at 0:22
  • 1
    Yes, that's a good idea if pinging more hosts than you set for --max-procs / -P. However, for host counts less than -P, all of pings are being executed in parallel - which means that it will take as long as the single longest ping execution, rather than the sum of them all. – Jason Musgrove Jun 18 '19 at 7:37
1

I do not know what you want exactly but you could change the last 8 bit-set into the decimal 255, so your hosts will receive a broadcast, actually,it will transmit ping packets to all devices that exist in a network.

ping -c 1 xx.xx.xx.255
2
  • 1
    That's not what I'm looking for. – slm Jun 14 '19 at 20:38
  • 8
    Welcome to the site - This is a good thought and shows an understanding of the Broadcast IP and its purpose in an IP network, and is completely correct. However it doesn't match OP's specific requirement in this narrowly-defined case. Do please carry on and have a crack at answering other questions. – Criggie Jun 16 '19 at 3:45
0
ping google.com && ping localhost

Output

Pinging google.com [xxxxxxx] with 32 bytes of data:
Reply from xxxxxxx: bytes=32 time=210ms TTL=49
Reply from ::1: time<1ms
Reply from xxxxxxx: bytes=32 time=212ms TTL=49
Reply from ::1: time<1ms
Reply from xxxxxxx: bytes=32 time=211ms TTL=49
Reply from ::1: time<1ms
1
  • Downsides of this are 1) that && operator only allows the second command to run if the first command completed successfully, (ie an exit code of 0) And 2) the first ping will never end without a ^C to interrupt it. Consider adding a -c and a -w parameter. Welcome to the site! – Criggie Jun 18 '19 at 0:24
-1

Just for fun and profit...

#!/bin/sh -

# sends six "pings" to a list of hosts defined in "hosts" below

hosts="
host.a
host.b
host.c
host.d
host.e
"

for p in $hosts
do
# ONLY CHOOSE ONE OF THE FOLLOWING, NOT BOTH
# dump results to file
    ping -c 6 $p >>./PINGED
# dump output to console
    ping -c 6 $p
done

exit

This could be easily enhanced. Which makes it pretty useful. :)

For additional options please see the man pages for bsd ping and Linux ping

HTH

EDIT: slightly updated to terminate the ping queries @6 pings each, and add man page options.

6
  • The first ping call never returns unless it hits a fatal error. – Gilles 'SO- stop being evil' Jun 15 '19 at 9:14
  • Really? I tested this before posting it here, and it worked as described. You do know that not every host will respond to being pinged. Perhaps the hosts you queried weren't responding. In any case. In an effort to provide quicker feedback, I've limited the queries to 6 requests per host. – somebody Jun 15 '19 at 9:28
  • 2
    Now at least the script has a chance of terminating in practice. But without parallelism, it's still pretty useless. If the first host responds, the user doesn't get any information about the other hosts for the first 5 seconds. – Gilles 'SO- stop being evil' Jun 15 '19 at 9:43
  • Well this was intended to be more of an example to build upon, as one needs. I could easily create another that fully saturates your pipe. But is that what you're really after? :) – somebody Jun 15 '19 at 9:49
  • 1
    What I should have done is to create a link to the ping man page. Honestly. I only created it as an easy template for one to easily modify for their own means. ping works drastically different on different OS's, and under the myriad of different network conditions that each individual may be working under. So there is no "perfect" default for everyone. Each has their own "sweet spot". :) Did I mention that it works perfectly for me? ;) – somebody Jun 16 '19 at 9:59
-2

Use below simple command:

$ getip(){ ping -c 1 -t 1 $1 | head -1 | cut -d ' ' -f 3 | tr -d '()' 2>&1 | tee >> /tmp/result.log & }
$ getip 'hostname.number1.net'
$ getip 'hostname.number2.net'
$ getip 'hostname.number3.net'
$ getip 'hostname.number4.net'
$ getip 'hostname.number5.net'
$ getip 'hostname.number6.net'
$ getip 'hostname.number7.net'
$ getip 'hostname.number8.net'
$ cat /tmp/result.log
ABC.DEF.GHI.XY1
ABC.DEF.GHI.XY2
ABC.DEF.GHI.XY3
ABC.DEF.GHI.XY4
ABC.DEF.GHI.XY5
ABC.DEF.GHI.XY6
ABC.DEF.GHI.XY7
ABC.DEF.GHI.XY8

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.