shell 检测是否ping 通

#!/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

 

  • 3
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值