how to ping each ip in a file?

user1687717 picture user1687717 · Jan 21, 2013 · Viewed 10.5k times · Source

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

Answer

Chris Seymour picture Chris Seymour · Jan 21, 2013

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