Router reboot wrapper script

This is a memory sink article. I found this nice python script which reboots a B525 router and wanted to write a wrapper script around it.

#!/bin/bash

# refs: https://github.com/jinxo13/HuaweiB525Router, https://github.com/mkorz/b618reboot

RUNDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

MAX_ATTEMPTS=10
ATTEMPT=0
TIMEOUT=60
LOCK_FILE=$RUNDIR/lock
REMOTE=google.com
LOCAL=192.168.7.1

if [ -f $LOCK_FILE ]; then
    echo "Already running, exiting"
    exit -1
fi

function finish {
  rm $RUNDIR/lock
}
trap finish EXIT

touch $RUNDIR/lock

while (( $ATTEMPT < $MAX_ATTEMPTS ))
do
    DATE=$(date +%Y%d%m-%H%M%S)
    echo -e "GET http://$REMOTE HTTP/1.0\n\n" | nc $REMOTE 80 > /dev/null 2>&1

    if [ $? -eq 0 ]; then
        echo "$DATE: Online"
        break
    else
        ping -c1 -w1 LOCAL > /dev/null 2>&1

        if [ $? -eq 0 ]; then
            echo "$DATE: Internet offline, router online, trying to reboot router, waiting $TIMEOUT"
            $RUNDIR/b618reboot/reboot_router.py
        else
            echo "$DATE: Internet offline, router offline, waiting $TIMEOUT"
        fi

        sleep $TIMEOUT
        ATTEMPT=$(( ATTEMPT + 1 ))
        TIMEOUT=$(( TIMEOUT * 2 ))
    fi
done

Leave a Reply

*