在家裡架了一台主機,使用動態IP連線,利用NO-IP服務進行 Domain 連結,為了讓主機都可以隨時保持連線,不管是 AP關閉 or 主機重開,都可以自動連線。
#! /bin/bash
# Program:
# System initial & check some service.
# History:
# 2013/10/08 Shazi First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
tag_URL="http://localhost/resp.php?w=pi"
function write_log(){
echo $1
logger "testPiLog ||||| $1"
}
URL="http://google.com"
wget -q --tries=10 --timeout=20 $URL -O /dev/null
# 判斷網路連線是否正常, 以 wget google.com 為測試
if [[ $? -ne 0 ]]; then
# 網路無法正常連線 , 進行撥號連線
write_log "wget ${URL} - Offline"
# 判斷 PPPOE 是否已執行 ,若「有」先刪除程序
PPP_PID=$(pgrep ppp)
if [$PPP_PID != ""]
then
write_log "TURN OFF PPPOE"
/usr/bin/poff dsl-provider
fi;
write_log "TURN ON PPPOE"
/usr/bin/pon dsl-provider
sleep 1s
# 判斷是否有取得IP
PPPOE_IP=$(/sbin/ifconfig | awk -F'[ :]+' '/P-t-P/{print $4}');
if [$PPPOE_IP = ""]
then
# 無法取得IP , 數據機有可能未開啟
write_log "PPPOE NOT AVAILABLE"
exit 0
fi;
# 更新 DNS 設定
write_log "UPDATE DNS to 8.8.8.8";
echo "nameserver 8.8.8.8" > /etc/resolv.conf
sleep 1s
# 更新 NO-IP 資訊
write_log "UPDATE noip info.(${PPPOE_IP})";
/usr/local/bin/noip2 -i ${PPPOE_IP}
fi;
# 進行登入目前IP
RESP=$(curl -A "Mozilla/4.0" $tag_URL)
if [ "$RESP" = " OK" ]; then
write_log "Tag Finish.($RESP)";
exit 0
else
write_log "Tag Error.($RESP)";
fi;
exit 0
沒有留言:
張貼留言