android - 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.
解决方案
推荐阅读
- r - 如何在我的 ggplot 上绘制逻辑增长曲线
- gnuplot - 如何使用文件中的数据绘制 3D gnuplot splot 曲面图?
- c# - 如何在 MapControl 中动态使用具有多个引脚的多个自定义弹出窗口
- node.js - 当以具有不同 mimetype 的一种形式上传多个文件时,如何将我的 CSV 转换为 JSON 并将其保存到 MongoDB?
- python - 使用 BeautifulSoup 标记后无法立即获取文本
- php - 在 PHPUnit 中模拟接口时出现 TypeError
- flutter - 使用 Flutter 显示/隐藏小部件的问题
- javascript - 如何重新打开表单以使用 ReactJS 更新数据
- sqlite - Sqlite 和代号在模拟器上有效,但在我的手机上无效
- django - ImageField 未上传到指定目录