如何ping文件中的每个IP?

use*_*717 7 macos shell ping xargs

我有一个名为"ips"的文件,其中包含我需要ping的所有ips.为了ping这些IP,我使用以下代码:

cat ips|xargs ping -c 2
Run Code Online (Sandbox Code Playgroud)

但控制台告诉我ping的用法,我不知道如何正确地做到这一点.我正在使用Mac OS

Chr*_*our 14

您需要使用选项-n1以及xargs时传递一个IP,因为ping不支持多个IP:

$ cat ips | xargs -n1 ping -c 2
Run Code Online (Sandbox Code Playgroud)

演示:

$ 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
Run Code Online (Sandbox Code Playgroud)