简体   繁体   English

在 bash 脚本中停止 Ping 进程?

[英]Stopping the Ping process in bash script?

I created a bash script to ping my local network to see which hosts is up and I have a problem in stopping the Ping process by using ctrl+C once it is started the only way i found to suspend it but even the kill command doesn't work with the PID of the Ping我创建了一个 bash 脚本来 ping 我的本地网络以查看哪些主机已启动,并且我在使用 ctrl+C 停止 Ping 进程时遇到问题,一旦它启动是我发现暂停它的唯一方法,但即使是 kill 命令也没有t 使用 Ping 的 PID

submask=100
for i in ${submask -le 110}
do
    ping -n 2 192.168.1.$submask
    ((submask++))
done

I suggest you to limit the amount of packets sent with ping with the option -c .我建议您使用选项-c限制使用ping发送的数据包数量。

I also corrected the bash syntax, guessing what you intend to do.我还纠正了 bash 语法,猜你打算做什么。

Finally, it is faster to run all the ping processes in parallel with the operand & .最后,与操作数&并行运行所有ping进程会更快。

Try:尝试:

for submask in ${100..110}
do
    echo ping -c 1 192.168.1.$submask &
done

Ctrl + C exit ping, but another ping starts. Ctrl + C 退出 ping,但另一个 ping 开始了。 So you can use trap.所以你可以使用陷阱。

#!/bin/bash

exit_()
{
        exit
}

submask=100
while [ $submask -le 110 ]
do
    fping -c 2 192.168.77.$submask
   ((submask++))
   trap exit_ int
done

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

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