How to ping each ip in a file?

I have a file called "ips" containing all the ips I need for ping. To check these IP addresses, I use the following code:

cat ips|xargs ping -c 2

but the console will show me the use of ping, I don’t know how to do it correctly. I am using mac os

+5
source share
7 answers

You need to use the -n1c option xargsto transfer one IP at a time when it pingdoes not support multiple IP addresses:

$ 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
+12
source

With ip or hostname in each line of the file ips:

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

- -W, , , script . -q .

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

1 ip ( ), for:

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

fping. script .

    $ 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
    ...
+1

GNU Parallel :

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

, ips .

, , , , , .

GNU Parallel - , , ssh.

32 , 4- , - 8 :

Simple scheduling

GNU Parallel , - , , :

GNU parallel scheduling

GNU Parallel , , root. 10 , :

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

. http://git.savannah.gnu.org/cgit/parallel.git/tree/README

: http://www.gnu.org/software/parallel/man.html

: https://www.youtube.com/playlist?list=PL284C9FF2488BC6D1

: http://www.gnu.org/software/parallel/parallel_tutorial.html

, : https://lists.gnu.org/mailman/listinfo/parallel

+1

:

cat ips | xargs -i% ping -c 2 %
0

As suggested by @Lupus, you can use "fping", but the result is not human-friendly - it will scroll from your screen in a few seconds, without thinking about what is happening. For this, I just released ping-xray. I tried to make it as visual as possible when using the ascii terminal, plus it creates CSV logs with accurate millisecond resolution for all purposes.

https://dimon.ca/ping-xray/ enter image description here

I hope you will like it.

0
source

All Articles