Linux: How to ping multiple hosts directly or over ssh proxy
Ping multiple hosts in parallel, directly or over SSH proxy.
Recently i had an interesting task, to ping multiple hosts in parallel, well it might sound not so interesting to many because of the many tools that allow that, but here is a catch “the hosts are behind an ssh proxy and we cannot install anything there”.
I will use the following tools to accomplish the task
- cat : to read the ip addresses from a csv file
- xargs : to parallelize the ping jobs
- bash : it might sound odd but i actually create a new bash session for each ping job
- ssh : for the case that i need to ping over a proxy
- echo : print messages on screen
Ping multiple hosts directly
Lets see the the simple case, how to ping multiple hosts that are directly reachable, of course the are myriad tools to do this but i will add my one-liner since the over ssh proxy version is based on this.
Create a file named servers.csv and add the servers that you want to ping, in my imaginary environment the servers areserver1
server2
server3
server4
And run thiscat servers.csv | xargs -I {} -P8 bash -c "ping -c4 {} &> /dev/null && echo {}:success || echo {}:fail"
The expected output if all hosts are pingable will be something like thisserver1:success
server2:success
server3:success
server4:success
if one or more servers are not pingable like server3 the output will beserver1:success
server2:success
server3:fail
server4:success
How it works
catpipes content ofservers.csvtoxargsxargsruns thebash -c “”command, everything between“”will be executed by bash.-Iparameter instructsxargsto replace{}with each line from theservers.csvfile.-P8this parameter creates 8 parallelxargsprocesses.ping -c4 {}this ping command sends 4 ICMP packets to the hostname/ip that will replace{}&> /dev/nullthis will forward all stdout and stderr from ping to/dev/nullin order to hide any ping output from screen.&& echo {}:success || echo {}:failif ping has succeeded to get a ping response from the host then it will print the name/ip of the host and will append :success to it, if it fails to get a ping response it will print the name/ip of the host and will append :fail to it.
Ping multiple hosts over an ssh proxy
in my case the name of the ssh proxy is proxy01, i need to do the following changes to the previous commandcat servers.csv | xargs -P8 -I {} bash -c "ssh proxy01 'ping -c4 {} &> /dev/null && echo {}:success || echo {}:fail'"
The new parts here is the ssh proxy01 ‘’, this command will connect to server proxy01 and will execute the ping commands from there, as you can see the change from the previous example is not huge but the result is, all commands now executed from proxy01 which has access to the servers i want to ping
Useful notes:
-P: changing this changes the number of parallel ping jobs-c4: changing this will change the number of ICMP (ping packets) towards to the servers you want to pingssh: ssh can accept many different parameters that i have not used, like username; if you don't use ssh keys it will ask you for a password unless you use a tool like sshpass.
I hope you found this article interesting and enjoyable :)