如何使用 ping 返回值触发 bash 命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21539815/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 09:27:20  来源:igfitidea点击:

How to use ping return value to trigger a bash command

linuxbash

提问by sonance207

I am trying to make my openvpn client restart if it is not able to ping the destination 4.2.2.2 every 60 seconds, if it is then do nothing. Here is what I have. I want to to be continuously running also.I am running this on Alpine Linux. Any help is greatly appreciated.

我试图让我的 openvpn 客户端重新启动,如果它不能每 60 秒 ping 目标 4.2.2.2,如果它然后什么都不做。这是我所拥有的。我也想继续运行。我在 Alpine Linux 上运行它。任何帮助是极大的赞赏。

#!/bin/sh
#This is a script to continuously do 5 pings to 4.2.2.2
#every 60 seconds to keep the tunnel up if  pings fail,
#then it will restart the openvpn process. And record the
#time it failed.

PING=ping -w 5 4.2.2.2


exec &> /var/log/ping_SLA
while true
do
if 
#ping returns a fail value

    [ $PING -eq 0 ];
        sleep 60s
then
#Execute commands

    date > /var/log/ping_SLA_Fail
        rc-service openvpn stop
        killall -9 openvpn
        rc-service openvpn start
        sleep 30s
else
# colon is a null and is required
    :

fi

done

回答by MLSC

man ping:

人平:

-c count

Stop after sending count ECHO_REQUEST packets. With deadline option, ping waits for count ECHO_REPLY packets, until the timeout expires.

发送 count 个 ECHO_REQUEST 数据包后停止。使用截止时间选项,ping 等待计数 ECHO_REPLY 数据包,直到超时到期。

-s packetsize

Specifies the number of data bytes to be sent. The default is 56, which translates into 64 ICMP data bytes when combined with the 8 bytes of ICMP header data.

指定要发送的数据字节数。默认值为 56,当与 8 个字节的 ICMP 标头数据组合时,它转换为 64 个 ICMP 数据字节。

-w deadline

Specify a timeout, in seconds, before ping exits regardless of how many packets have been sent or received. In this case ping does not stop after count packet are sent, it waits either for deadline expire or until count probes are answered or for some error notification from network.

无论发送或接收了多少数据包,都指定在 ping 退出之前的超时时间(以秒为单位)。在这种情况下,发送计数数据包后 ping 不会停止,它会等待截止期限到期或直到计数探测得到应答或来自网络的某些错误通知。

-W timeout

Time to wait for a response, in seconds. The option affects only timeout in absense of any responses, otherwise ping waits for two RTTs.

等待响应的时间,以秒为单位。该选项仅在没有任何响应的情况下影响超时,否则 ping 将等待两个 RTT。

So we have:

所以我们有:

ping -c 1 -s 1 -W 1 4.2.2.2 1>/dev/null 2>&1  #The fast way to ping
#OR : nmap -sP 1 4.2.2.2 1  1>/dev/null 2>&1  #The fastest way to ping
if [ $? -eq 0 ]; then
   date >> /var/log/ping_SLA_Fail
   rc-service openvpn stop
   killall -9 openvpn
   rc-service openvpn start
   sleep 30
elif [ $? -ne 0 ]; then
   .
   .
   .
fi

回答by John B

I'm not familiar with any -woption with ping.

我不熟悉-wping 的任何选项。

Use the -coption with your ping command to determine the number of ICMP echo requests to send.

将该-c选项与您的 ping 命令一起使用以确定要发送的 ICMP 回显请求的数量。

Something like this should work:

这样的事情应该工作:

if ! ping -c 5 4.2.2.2 &> /dev/null; then
    date >> /var/log/ping_SLA_Fail
    rc-service openvpn stop
    killall -9 openvpn
    rc-service openvpn start
fi
sleep 60

回答by edoloughlin

You have to run the pingcommand and then test its exit value:

您必须运行该ping命令,然后测试其退出值:

ping -w 5 4.2.2.2 > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "Ping worked"
else
    echo "Ping failed"
fi