#!/system/bin/sh
while true
do
ping -c 8 -w 100 www.baidu.com
if [[ $? != 0 ]];then
echo " ping fail "
sleep 5
else
echo " ping ok"
/system/bin/reboot
fi
done
-c: 表示次数,1 为1次
-w: 表示deadline, time out的时间,单位为秒,100为100秒。
-w>=-c
如何用shell脚本判断一个主机是否可以连通
假设判断与 android.xx.xxx.com是否可以相连
运行ping -c 3 -w 5 android.xx.xxx.com
然后运行echo $?
如果显示0说明主机android.xx.xxx.com可以连通
如果显示非0(例如,显示1)说明主机android.xx.xxx.com不可连通
ping -c 3 -w 5 android.xx.xxx.com
if [[ $? != 0 ]];then
echo " can not connect "
fi
使用ping命令进行连通性校验的shell脚本
#!/bin/sh
IP="10.10.100.1"
kill_deamon() {
/etc/init.d/deamon stop
}
start_deamon(){
/etc/init.d/deamon start
}
check_ping() {
local total="0"
local i="0"
while [ $i -lt 10 ]; do
local line=""
#echo "ping $IP"
line=`ping $IP -c 1 -s 1 -W 1 | grep "100% packet loss" | wc -l`
if [ "${line}" != "0" ]; then
echo "ping failed!"
total=$((total+1))
else
echo "ping ok!"
total="0"
fi
i=$((i+1))
sleep 1
done
if [ $total -gt 5 ]; then
echo "check failed!"
return 1
else
echo "check ok!"
return 0
fi
}
start(){
local rtl=""
while [ 1 ]; do
check_ping
rtl=$?
if [ "$rtl" != "0" ]; then
echo " restart deamon start "
kill_deamon
start_deamon
else
sleep 600;
fi
done
}
start
其它的也有这样的,如下,都是用一个循环去判断而已:
#!/bin/sh
line="0"
total="0"
dst=`uci get network.mesh.gateway`
while [ 1 ]; do
line=`ping $dst -c 1 -s 1 -W 1 | grep "100% packet loss" | wc -l`
if [ "${line}" != "0" ]; then
total=$((total+1))
else
total="0"
fi
if [ "${total}" == "5" ]; then
reboot -f
fi
sleep 12
done
另外一个:
#!/bin/bash
for siteip in $(seq 1 254)do
site="192.168.1.${siteip}"
ping -c1 -W1 ${site} &> /dev/null
if [ "$?" == "0" ]; then
echo "$site is UP"
else
echo "$site is DOWN"
fi
done