首页 > 解决方案 > android终端中的Bash ping脚本文件

问题描述

我有一个循环ping的小代码:

#!/system/bin/sh
_count=0
_target="dns.google.com"
echo "target :: $_target"
while true;
do
_time="$(date +%H%M%S)"
_count=$((_count + 1))
    echo "::::::::::::::::::LOOP"
    echo "time :: $_time"
    echo "counter :: $_count"
    ping -w 1 -c 1 $_target >/dev/null 2>&1 &&
    echo "$? ... is reachable" ||
    echo "$? ... is down"
done

我从 android 终端调用它(使用 SManager)。它应该使用循环中的 ping 检查主机可用性。问题是我只对第一个 ping 得到了真正的响应,而其他所有的都是坏的(输出):

$ /system/bin/sh /storage/emulated/0/pinger.sh
target :: dns.google.com
::::::::::::::::::LOOP
time :: 174912
counter :: 1
0 ... is reachable
::::::::::::::::::LOOP
time :: 174912
counter :: 2
1 ... is down
::::::::::::::::::LOOP
time :: 174913
counter :: 3
1 ... is down
::::::::::::::::::LOOP
time :: 174914
counter :: 4
1 ... is down
.
..
...etc.

这种行为的原因是什么?

卢卡斯

编辑2:

新脚本:

#!/system/bin/sh
_count=0
_target="dns.google.com"
echo "target :: $_target"
while true;
do
_time="$(date +%H%M%S)"
_count=$((_count + 1))
    echo "::::::::::::::::::LOOP"
    echo "time :: $_time"
    echo "counter :: $_count"
    ping -w 1 -c 1 $_target
done

输出:

$ /system/bin/sh /storage/emulated/0/pinger.sh
target :: dns.google.com
::::::::::::::::::LOOP
time :: 205444
counter :: 1
PING dns.google.com (8.8.4.4) 56(84) bytes of data.
64 bytes from dns.google (8.8.4.4): icmp_seq=1 ttl=64 time=7.40 ms

--- dns.google.com ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 7.404/7.404/7.404/0.000 ms
::::::::::::::::::LOOP
time :: 205445
counter :: 2
PING dns.google.com (8.8.4.4) 56(84) bytes of data.

--- dns.google.com ping statistics ---
2 packets transmitted, 0 received, 100% packet loss, time 999ms
::::::::::::::::::LOOP
time :: 205447
counter :: 3
PING dns.google.com (8.8.4.4) 56(84) bytes of data.

--- dns.google.com ping statistics ---
1 packets transmitted, 0 received, 100% packet loss, time 0ms
.
..
...etc.

标签: androidbashloopsterminalping

解决方案


推荐阅读