检测服务器是否连接着网络

#!/bin/bash

# 检测服务器是否连接着网络,如果网络不通 则 3次后 关机
# crontab -e
# */5 * * * * ./check.sh

echo "Starting test network was clear..."

if test -e ./checkInfo
	then
	echo "CheckInfo File Exist..."
else
	cat /dev/null > ./checkInfo
fi

last_res=`head -1 ./checkInfo`

checkInternet(){
	ping_res=1

	for url in "8.8.8.8" "61.139.2.69" "114.114.114.114" "168.95.1.1" "223.5.5.5" "180.76.76.76"
	do
		echo "PING ${url}"

		ping=`ping -c 3 ${url}|awk 'NR==7 {print $4}'`

		if [ ${ping} -eq 0 ]
			then
				ping_res=1
			else
				ping_res=2
		fi

		if [ ${ping_res} -eq 2 ]
			then
			break
		fi
	done

	return ${ping_res}
}

checkInternet

result="$?"

if [ ${result} -eq 1 ]
	then
		if [ "${last_res}" = "1" ]
			then
				echo "2" > ./checkInfo
		elif [ "${last_res}" = "2" ]
			then
				cat /dev/null > ./checkInfo
				init 0
			else
				echo "1" > ./checkInfo
		fi
	else
		cat /dev/null > ./checkInfo
fi

 

转载于:https://my.oschina.net/Kxvz/blog/822218

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值