Bash循环ping成功

Bash循环ping成功,bash,shell,unix,while-loop,ping,Bash,Shell,Unix,While Loop,Ping,我认为这需要更改为一个while子句,现在它将等待所有10000次ping完成,我需要它在ping成功时返回。程序“say”在OSX上,它使计算机说话 #!/bin/bash echo begin ping if ping -c 100000 8.8.8.8 | grep timeout; then echo `say timeout`; else echo `say the internet is back up`; fi 好吧,我没有权利回答我自己的问题,所以在玩过之后,这里是我的答案:

我认为这需要更改为一个while子句,现在它将等待所有10000次ping完成,我需要它在ping成功时返回。程序“say”在OSX上,它使计算机说话

#!/bin/bash
echo begin ping
if ping -c 100000 8.8.8.8 | grep timeout;
then echo `say timeout`;
else echo `say the internet is back up`;
fi
好吧,我没有权利回答我自己的问题,所以在玩过之后,这里是我的答案:

谢谢,是的,我不知道美元?直到现在。不管怎样,现在我已经去做了这个。我喜欢你的不会永远消失,但在我的情况下,我不需要它停止,直到它结束

#!/bin/bash
intertube=0
echo "begin ping"
while [ $intertube -ne 1 ]; do
        ping -c 3 google.com
        if [ $? -eq  0 ]; then
                echo "ping success";
                say success
                intertube=1;
        else
                echo "fail ping"
        fi
done
echo "fin script"

如果使用
-o
选项,Mac OS X的
ping
将在收到一个回复数据包后退出

进一步阅读:

编辑:关于使用ping的退出状态为您带来好处。我会这样做:

#!/usr/bin/env bash
echo 'Begin ping'
if ping -oc 100000 8.8.8.8 > /dev/null; then
    echo $(say 'timeout')
else
    echo $(say 'the Internet is back up')
fi
((count = 100))                            # Maximum number to try.
while [[ $count -ne 0 ]] ; do
    ping -c 1 8.8.8.8                      # Try once.
    rc=$?
    if [[ $rc -eq 0 ]] ; then
        ((count = 1))                      # If okay, flag to exit loop.
    fi
    ((count = count - 1))                  # So we don't go forever.
done

if [[ $rc -eq 0 ]] ; then                  # Make final determination.
    echo `say The internet is back up.`
else
    echo `say Timeout.`
fi

ping
将发送多达100000个数据包,然后以失败状态退出,除非它收到一个应答数据包,在这种情况下,它以成功状态退出。
if
将执行相应的语句。

您可能不应该依赖命令的文本输出来决定这一点,尤其是当
ping
命令:

如果从指定主机至少听到一个响应,ping实用程序返回退出状态为零;如果传输成功但未收到响应,则状态为2;如果发生错误,则从
获取另一个值

换言之,请使用以下内容:

#!/usr/bin/env bash
echo 'Begin ping'
if ping -oc 100000 8.8.8.8 > /dev/null; then
    echo $(say 'timeout')
else
    echo $(say 'the Internet is back up')
fi
((count = 100))                            # Maximum number to try.
while [[ $count -ne 0 ]] ; do
    ping -c 1 8.8.8.8                      # Try once.
    rc=$?
    if [[ $rc -eq 0 ]] ; then
        ((count = 1))                      # If okay, flag to exit loop.
    fi
    ((count = count - 1))                  # So we don't go forever.
done

if [[ $rc -eq 0 ]] ; then                  # Make final determination.
    echo `say The internet is back up.`
else
    echo `say Timeout.`
fi

您不需要使用echo或grep。您可以这样做:

ping -oc 100000 8.8.8.8 > /dev/null && say "up" || say "down"

以下是我的一行解决方案:

screen -S internet-check -d -m -- bash -c 'while ! ping -c 1 google.com; do echo -; done; echo Google responding to ping | mail -s internet-back my-email@example.com'
这将在新屏幕会话中运行无限ping,直到有响应为止,此时它将向
my发送电子邮件-email@example.com
。在电子邮件发送到手机的时代很有用


(您可能希望通过运行
echo test | mail-s test my来检查
mail
是否正确配置-email@example.com
首先。当然,从
开始,你可以做任何你想做的事;
以后,敲响警钟,启动网络浏览器,发挥你的想象力。)

我使用这个Bash脚本在OSX上每分钟测试一次互联网状态

#address=192.168.1.99  # forced bad address for testing/debugging
address=23.208.224.170 # www.cisco.com
internet=1             # default to internet is up

while true;
do
    # %a  Day of Week, textual
    # %b  Month, textual, abbreviated
    # %d  Day, numeric
    # %r  Timestamp AM/PM
    echo -n $(date +"%a, %b %d, %r") "-- " 
    ping -c 1 ${address} > /tmp/ping.$
    if [[ $? -ne 0 ]]; then
        if [[ ${internet} -eq 1 ]]; then   # edge trigger -- was up now down
            echo -n $(say "Internet down") # OSX Text-to-Speech
            echo -n "Internet DOWN"
        else
            echo -n "... still down"
        fi
        internet=0
    else
        if [[ ${internet} -eq 0 ]]; then     # edge trigger -- was down now up
            echo -n $(say "Internet back up") # OSX Text-To-Speech
        fi
        internet=1
    fi   
    cat /tmp/ping.$ | head -2 | tail -1
    sleep 60 ; # sleep 60 seconds =1 min
done

这也可以通过超时来完成:

# Ping until timeout or 1 successful packet
ping -w (timeout) -c 1

我喜欢paxdiablo的脚本,但想要一个无限期运行的版本。此版本将运行ping,直到建立连接,然后打印一条这样的消息

echo "Testing..."

PING_CMD="ping -t 3 -c 1 google.com > /dev/null 2>&1"

eval $PING_CMD

if [[ $? -eq 0 ]]; then
    echo "Already connected."
else
    echo -n "Waiting for connection..."

    while true; do
        eval $PING_CMD

        if [[ $? -eq 0 ]]; then
            echo
            echo Connected.
            break
        else
            sleep 0.5
            echo -n .
        fi
    done
fi

我还有一个软件包,我会根据需要进行修复和改进。

这与
ping
无关,但是你想通过
echo
ing“say”来实现什么呢?您的介绍性段落暗示您正在尝试执行
say
命令,如果您只是
echo
单词,则不会执行该命令。@Lawrence,这些是反勾号,不是引号。他们将执行
say
命令并回显其输出;我道歉。尽管我仍然不确定那里正在完成什么
say
从不向标准输出任何内容。不需要$?在你的解决方案中;如果ping。。。很好。此外,还可以使用break:while:;来避免额外的变量;做如平;然后打破;fi;这对我来说很有效:ping-c28.8.8.8>/dev/null&&echo“up”| | echo“down”标志
-c100000
表示尝试
100000次。如果没有这个标志,它将无休止地重试(您需要中断它)。由于
-o
的原因,它将在第一次成功ping时返回。这仅适用于OSX。Ping在windows或Linux上没有-o标志。是的,t原始问题和此答案均假定为macOS。请注意,指示它使用Mac的
say
命令报告成功的ping。在这个答案下面的第一条评论中,您将看到在linux上使用
ps
echo
的一个变体。count=1行应该是((count=1))来处理空格。@MSumulong,没错,很难相信我(和其他人)在过去的三年多里错过了这一点。修正了,所有分配给
count
的任务也保持一致。这正是我想要的。奇怪。它向我显示了“-bash:意外标记“(”附近的语法错误”