Gateway ping 확인하는 스크립트
eth0 – 192.168.100.1
eth1 – 192.168.200.1
default gateway는 192.168.100.1 로 등록되어 있는 상태에서 192.168.100.1로 핑을 계속 보내면서 네트워크를 확인한다.
최대 실패횟수가 초과하면 현재 default gateway를 제거하고 eth1의 게이트웨이를 등록하고 관리자에게 이메일을 발송한다.
[code lang=”shell”]
#!/bin/sh
#####################################################################
# edit config
GW1="192.168.100.1"
GW2="192.168.200.1"
MAX_FAIL_COUNT=5
ERROR_MAILTO="me@jongwan.com"
#####################################################################
# path exec
EXEC_PING="/bin/ping"
EXEC_ROUTE="/sbin/route"
EXEC_MAIL="/usr/bin/mail"
#####################################################################
# prevent duplicate run
ME=`basename "$0"`
CHK_RUN=`pgrep -o $ME`
if [ $CHK_RUN -ne $$ ]; then
exit
fi
#####################################################################
# checking
FAIL_COUNT=0
while(true) do
# check ping
CHK=`$EXEC_PING -c1 $GW1 > /dev/null; echo $?`
# 0 is reachable, 2 is unreachable
if [ $CHK -ne "0" ]; then
FAIL_COUNT=$(($FAIL_COUNT+1))
else
FAIL_COUNT=0 # init fail_count if success ping
fi
# check fail count
if [ $FAIL_COUNT -ge $MAX_FAIL_COUNT ]; then
# add/del gateway
echo "Gateway connection failed : $GW1"
CMD1=`$EXEC_ROUTE del default gw $GW1`
CMD2=`$EXEC_ROUTE add default gw $GW2`
# send email
CMD3=`echo "Gateway connection failed : $GW1" | $EXEC_MAIL -s "Gateway connection failed : $GW1" $ERROR_MAILTO`
# exit shell
break
fi
sleep 1
done
[/code]