WHR-1166DHP2 の再起動スクリプト作成と運用

BUFFALO WHR-1166DHP2 を長時間運用していると、スループット低下が発生するので、

  • 再起動スクリプトを作成
  • Cron による週1回の再起動運用

構築される環境に応じて、適宜読み替えてください。

スクリプト本体 /hogehoge/bin/reboot.sh
ログファイル /hogehoge/log/reboot.log
wget の cookie ファイル(未使用) /hogehoge/tmp/reboot_cookie.txt
wget の html ファイル /hogehoge/tmp/reboot_html.txt
WHR-1166DHP2 の IP アドレス TXT_NODE=“192.168.0.1”
WHR-1166DHP2 のログインパスワード TXT_PASSWD=“hogepass”
  • 毎週月曜の 04:00root で実行

cd /hogehoge/bin
vi reboot.sh

#!/bin/sh

cd `dirname $0`
MESS_TITLE="`basename $0 .sh`"
LOG_FILE="$(pwd)/../log/${MESS_TITLE}.log"
COOKIE_FILE="$(pwd)/../tmp/${MESS_TITLE}_cookie.txt"
HTML_FILE="$(pwd)/../tmp/${MESS_TITLE}_html.txt"
TXT_NODE="192.168.0.1"
TXT_PASSWD="hogepass"

################################################################################
# NODE Status Check
################################################################################
Console=$(ping -c 5 $TXT_NODE)

if [ $? -ne 1 ]
then
    MESS="${MESS_TITLE}: Powered on"
    logger -p user.notice "${MESS}"
    echo "`date +"%Y"/"%m"/"%d"" ""%H":"%M":"%S"` : ${MESS}" > "${LOG_FILE}"
else
    MESS="${MESS_TITLE}: Powered off"
    logger -p user.notice "${MESS}"
    echo "`date +"%Y"/"%m"/"%d"" ""%H":"%M":"%S"` : ${MESS}" > "${LOG_FILE}"
    exit
fi

################################################################################
# Shutdown Process
################################################################################
MESS="${MESS_TITLE}: Rebbot process"
logger -p user.notice "${MESS}"
echo "`date +"%Y"/"%m"/"%d"" ""%H":"%M":"%S"` : ${MESS}" >> "${LOG_FILE}"

################################################################################
# Connect to WHR-1166DHP2
################################################################################
MESS="${MESS_TITLE}: Connection start"
logger -p user.notice "${MESS}"
echo "`date +"%Y"/"%m"/"%d"" ""%H":"%M":"%S"` : ${MESS}" >> "${LOG_FILE}"

Console=$(wget -q -O "${HTML_FILE}" "http://${TXT_NODE}/login.html")

if [ $? -ne 0 ]
then
    MESS="${MESS_TITLE}: Connection failure"
    logger -p user.notice "${MESS}"
    echo "`date +"%Y"/"%m"/"%d"" ""%H":"%M":"%S"` : ${MESS}" >> "${LOG_FILE}"
    exit
else
    MESS="${MESS_TITLE}: Connection successful"
    logger -p user.notice "${MESS}"
    echo "`date +"%Y"/"%m"/"%d"" ""%H":"%M":"%S"` : ${MESS}" >> "${LOG_FILE}"
fi

################################################################################
# Get nosave_session_num
################################################################################
Session=$(grep nosave_session_num "${HTML_FILE}" | xargs | awk -F[' =>'] '$6 == "value" {print $7}')

if [ ${#Session} -le 5 ]
then
    MESS="${MESS_TITLE}: nosave_session_num acquisition failure (value=${Session})"
    logger -p user.notice "${MESS}"
    echo "`date +"%Y"/"%m"/"%d"" ""%H":"%M":"%S"` : ${MESS}" >> "${LOG_FILE}"
    exit
else
    MESS="${MESS_TITLE}: nosave_session_num is ${Session}"
    logger -p user.notice "${MESS}"
    echo "`date +"%Y"/"%m"/"%d"" ""%H":"%M":"%S"` : ${MESS}" >> "${LOG_FILE}"
fi

################################################################################
# Login to WHR-1166DHP2
################################################################################
MESS="${MESS_TITLE}: Login start"
logger -p user.notice "${MESS}"
echo "`date +"%Y"/"%m"/"%d"" ""%H":"%M":"%S"` : ${MESS}" >> "${LOG_FILE}"

Console=$(wget -q -O "${HTML_FILE}" "http://${TXT_NODE}/login.html" --post-data "nosave_Username=admin&nosave_Password=${TXT_PASSWD}&MobileDevice=0&nosave_session_num=${Session}" --referer="http://${TXT_NODE}/login.html")

if [ $? -ne 0 ]
then
    MESS="${MESS_TITLE}: Login failure"
    logger -p user.notice "${MESS}"
    echo "`date +"%Y"/"%m"/"%d"" ""%H":"%M":"%S"` : ${MESS}" >> "${LOG_FILE}"
    exit
else
    MESS="${MESS_TITLE}: Login successful"
    logger -p user.notice "${MESS}"
    echo "`date +"%Y"/"%m"/"%d"" ""%H":"%M":"%S"` : ${MESS}" >> "${LOG_FILE}"
fi

################################################################################
# Get init.html
################################################################################
MESS="${MESS_TITLE}: Get init.html"
logger -p user.notice "${MESS}"
echo "`date +"%Y"/"%m"/"%d"" ""%H":"%M":"%S"` : ${MESS}" >> "${LOG_FILE}"

Console=$(wget -q -O "${HTML_FILE}" "http://${TXT_NODE}/init.html" --referer="http://${TXT_NODE}/index_adv.html&item=0")

if [ $? -ne 0 ]
then
    MESS="${MESS_TITLE}: Failed to get init.html"
    logger -p user.notice "${MESS}"
    echo "`date +"%Y"/"%m"/"%d"" ""%H":"%M":"%S"` : ${MESS}" >> "${LOG_FILE}"
    exit
else
    MESS="${MESS_TITLE}: Success in getting init.html"
    logger -p user.notice "${MESS}"
    echo "`date +"%Y"/"%m"/"%d"" ""%H":"%M":"%S"` : ${MESS}" >> "${LOG_FILE}"
fi

################################################################################
# Get nosave_session_num
################################################################################
Session=$(grep nosave_session_num "${HTML_FILE}" | xargs | awk -F[' =>'] '$6 == "value" {print $7}')

if [ ${#Session} -le 5 ]
then
    MESS="${MESS_TITLE}: nosave_session_num acquisition failure (value=${Session})"
    logger -p user.notice "${MESS}"
    echo "`date +"%Y"/"%m"/"%d"" ""%H":"%M":"%S"` : ${MESS}" >> "${LOG_FILE}"
    exit
else
    MESS="${MESS_TITLE}: nosave_session_num is ${Session}"
    logger -p user.notice "${MESS}"
    echo "`date +"%Y"/"%m"/"%d"" ""%H":"%M":"%S"` : ${MESS}" >> "${LOG_FILE}"
fi

################################################################################
# WHR-1166DHP2 reboot
################################################################################
MESS="${MESS_TITLE}: Reboot start"
logger -p user.notice "${MESS}"
echo "`date +"%Y"/"%m"/"%d"" ""%H":"%M":"%S"` : ${MESS}" >> "${LOG_FILE}"

Console=$(wget -q -O "${HTML_FILE}" "http://${TXT_NODE}/init.html" --post-data "nosave_reboot=1&nosave_session_num=${Session}" --referer="http://${TXT_NODE}/init.html")

for isTimeout in `seq 1 180`
do
    Console=$(ping -c 1 $TXT_NODE)

    if [ $? -ne 0 ]
    then
        MESS="${MESS_TITLE}: Reboot successful"
        logger -p user.notice "${MESS}"
        echo "`date +"%Y"/"%m"/"%d"" ""%H":"%M":"%S"` : ${MESS}" >> "${LOG_FILE}"
        break
    fi

    sleep 1s
done

if [ $isTimeout -eq 180 ]
then
    MESS="${MESS_TITLE}: Reboot failure"
    logger -p user.notice "${MESS}"
    echo "`date +"%Y"/"%m"/"%d"" ""%H":"%M":"%S"` : ${MESS}" >> "${LOG_FILE}"
    exit
fi

MESS="${MESS_TITLE}: Reboot..."
logger -p user.notice "${MESS}"
echo "`date +"%Y"/"%m"/"%d"" ""%H":"%M":"%S"` : ${MESS}" >> "${LOG_FILE}"
 
chmod 755 ./reboot.sh

./reboot.sh
  ルータが再起動すること

vi /etc/cron.d/reboot

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
# min hr. dom mon dow user  command
  0   4   *   *   1   root  /hogehoge/bin/reboot.sh
systemctl restart cron

  スクリプトログや syslog で確認すること

コメントを入力:
 
  • network/operation/whr-1166dhp2
  • 最終更新: 2019/11/04
  • by chibatono