how to ping each ip in a file?
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 ips127.0.0.1google.combbc.co.uk$ cat ips | xargs echo ping -c 2ping -c 2 127.0.0.1 google.com bbc.co.uk$ cat ips | xargs -n1 echo ping -c 2ping -c 2 127.0.0.1ping -c 2 google.comping -c 2 bbc.co.uk# Drop the UUOC and redirect the input$ xargs -n1 echo ping -c 2 < ipsping -c 2 127.0.0.1ping -c 2 google.comping -c 2 bbc.co.uk
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