shell検出ping通

2476 ワード

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

 
もう1つ:
#!/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