简体   繁体   English

Linux Bash脚本可同时ping通多个主机

[英]Linux Bash script to ping multiple hosts simultaneously

I have a text file with list of 500 server names. 我有一个包含500个服务器名称列表的文本文件。 I need to ping all of them simultaneously instead of one by one in a loop, and put the pingable ones in one file and unpingable ones in another file. 我需要同时对所有它们进行ping操作,而不是一个循环一个接一个地将它们连接到一个文件中,将无法ping的文件放在另一个文件中。

Can I run each ping in background or spawn a new process for each ping? 我可以在后台运行每个ping或为每个ping生成一个新进程吗? What is the quickest and most efficient way to achieve this? 最快,最有效的方法是什么?

You can control the parallelism by using xargs : 您可以使用xargs来控制并行度:

cat file-of-ips | xargs -n 1 -I ^ -P 50 ping ^

Here we're keeping at most 50 pings going at a time. 在这里,我们一次最多只能进行50次ping。 The ip itself is inserted at the ^ ; ip本身插入在^ you can put arguments before and after. 您可以在参数前后添加参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM