Time Warner Cable
About

In Milwaukee, I have Time Warner Road Runner cable internet. Our connection has been very flaky, and Time Warner Support insists it is my router even though I have tried two different routers and two computers connected directly to the modem. Each device has the same result: the connections drops randomly throughout the day. Since Time Warner refuses to help me, I wrote a script that logs internet downtime and restarts the cable modem after three minutes of inactivity. This doesn't solve the problem but at least it limits me to three (or occasionally six and nine) minutes of downtime.

The Logging Script

This script logs internet downtime, counts the minutes of downtime, and calls the script to restart the cable modem. I inserted comments to explain the different sections.

#! /bin/bash

FILE=/tmp/timewarner
TIMESTAMP=`date`
LOGFILE=$HOME/logs/timewarner.log
IP_ADDR=134.48.6.1
RESTART_CABLE_MODEM=$HOME/bin/restart_cable_modem

if ping -w 1 $IP_ADDR &> /dev/null; then
    echo "Internet available" >> /dev/null
    # Reset the downtime count when the connection is available
    echo 0 > $FILE
else
    # Log that the internet is down
    echo "$TIMESTAMP: Internet is not available." >> $LOGFILE

    # If the file doesn't exist, create it
    if [ ! -f $FILE ]; then
        echo 0 > $FILE
    fi

    # Get the current value and increment it. When it reaches 3, reset it and
    # restart the cable modem
    VALUE=`cat $FILE`
    case $VALUE in
        0|1)
            let "NEW_VALUE=$VALUE+1"
            echo $NEW_VALUE > $FILE
            ;;
        *)
            echo 0 > $FILE
            $RESTART_CABLE_MODEM
            ;;
    esac
fi
The Restart Script

This script restarts the cable modem if it is available and logs the restart to a log file. It has been tested and used with a Motorola Surfboard modem. I can't guarantee it will work with any other cable modems.

#! /bin/bash

TIMESTAMP=`date`
LOGFILE=$HOME/logs/cablemodem.log
MODEM_IP=192.168.100.1

post_data="BUTTON_INPUT=Restart+Cable+Modem"
url="http://${MODEM_IP}/configdata.html"

if ping -w 1 $MODEM_IP &> /dev/null; then
    echo "${TIMESTAMP}: Restarting the cable modem." >> $LOGFILE
    curl -d "$post_data" $url > /dev/null 2>&1
else
    echo "Cable modem not available" >> /dev/null
fi
Cron

You can use Cron to run this script every minute with an entry similar to the one below.

# Check the internet connection every minute
*/1 *   *   *   *   $HOME/bin/timewarner.sh > /dev/null 2>&1
Statistics

Logging is only useful when it can be parsed into something useful. This script outputs daily or monthly downtime in minutes.

#! /bin/bash

LOG_FILE=$HOME/logs/timewarner.log

if [ "$1" == "monthly" ]; then
    DAYS=$(awk '{print ".*" $2 ".*" $6 ".*"}' $LOG_FILE | sort -mu)
else
    DAYS=$(awk '{print $1 ".*" $2 ".*" $3 ".*" $6 ".*"}' $LOG_FILE | sort -mu)
fi

IFS="
"
for DAY in $DAYS; do
    OUTPUT=$(echo -n $DAY | awk -F'\.\*' '{print $1 " " $2 " " $3 " " $4 " "}')
    echo -n $OUTPUT && egrep -c $DAY $LOG_FILE
done