how to ping each ip in a file?

12,154

Solution 1

You need to use the option -n1 with xargs to pass one IP at time as ping doesn't support multiple IPs:

$ cat ips | xargs -n1 ping -c 2

Demo:

$ cat ips
127.0.0.1
google.com
bbc.co.uk

$ cat ips | xargs echo ping -c 2
ping -c 2 127.0.0.1 google.com bbc.co.uk

$ cat ips | xargs -n1 echo ping -c 2
ping -c 2 127.0.0.1
ping -c 2 google.com
ping -c 2 bbc.co.uk

# Drop the UUOC and redirect the input
$ xargs -n1 echo ping -c 2 < ips
ping -c 2 127.0.0.1
ping -c 2 google.com
ping -c 2 bbc.co.uk

Solution 2

With ip or hostname in each line of ips file:

( while read ip; do ping -c 2 $ip; done ) < ips

You can also change timeout, with -W flag, so if some hosts isn'up, it wont lock your script for too much time. Also -q for quiet output is useful in this case.

( while read ip; do ping -c1 -W1 -q $ip; done ) < ips

Solution 3

If the file is 1 ip per line (and it's not overly large), you can do it with a for loop:

for ip in $(cat ips); do
  ping -c 2 $ip;
done

Solution 4

You could use fping. It also does that in parallel and has script friendly output.

    $ cat ips | xargs fping -q -C 3
    10.xx.xx.xx   : 201.39 203.62 200.77
    10.xx.xx.xx  : 288.10 287.25 288.02
    10.xx.xx.xx   : 187.62 187.86 188.69
    ...

Solution 5

With GNU Parallel you would do:

parallel -j0 ping -c 2 {} :::: ips

This will run as many jobs in parallel as you have ips or processes.

It also makes sure the output from different jobs are not mixed together, so if you use the output you are guaranteed that you will not get half-a-line from two different jobs.

GNU Parallel is a general parallelizer and makes is easy to run jobs in parallel on the same machine or on multiple machines you have ssh access to.

If you have 32 different jobs you want to run on 4 CPUs, a straight forward way to parallelize is to run 8 jobs on each CPU:

Simple scheduling

GNU Parallel instead spawns a new process when one finishes - keeping the CPUs active and thus saving time:

GNU Parallel scheduling

Installation

If GNU Parallel is not packaged for your distribution, you can do a personal installation, which does not require root access. It can be done in 10 seconds by doing this:

(wget -O - pi.dk/3 || curl pi.dk/3/ || fetch -o - http://pi.dk/3) | bash

For other installation options see http://git.savannah.gnu.org/cgit/parallel.git/tree/README

Learn more

See more examples: http://www.gnu.org/software/parallel/man.html

Watch the intro videos: https://www.youtube.com/playlist?list=PL284C9FF2488BC6D1

Walk through the tutorial: http://www.gnu.org/software/parallel/parallel_tutorial.html

Sign up for the email list to get support: https://lists.gnu.org/mailman/listinfo/parallel

Share:
12,154
user1687717
Author by

user1687717

Updated on July 28, 2022

Comments

  • user1687717
    user1687717 almost 2 years

    I have a file named "ips" containing all ips I need to ping. In order to ping those IPs, I use the following code:

    cat ips|xargs ping -c 2
    

    but the console show me the usage of ping, I don't know how to do it correctly. I'm using Mac os