Shell Monitor dropped adsl connection status

I have this crappy ISP that constantly drops the adsl connection. I'd like to run a script that pings the modem every 10 mins, then pings every 5 or 10 seconds if it finds that the connection is down, plus logs these actions.

I found the below script, but my only problem with it is the variable wait/sleep time described above between up and down events. I also thought of using the ping -i 10 notation, but could not figure out how to end the loop when using that. Any better ideas to what I have?
Code:
#!/bin/sh

IP=8.8.8.8  # for example
status=1  # default is up

while true;
    do
    ping -c 1 -q -n "$IP" > /tmp/ping.$
    if [ "$?" -ne 0 ]; then
        echo "i-2"
        if [ "$status" -eq 1 ]; then # was up now down
            echo -n $(date +"%a, %b %d, %r") " down" > /home/me/adsl.log
        else
            echo -n $(date +"%a, %b %d, %r") " down" > /home/me/adsl.log
        fi
        internet=0
    else
        if [ "$status" -eq 0 ]; then # was down now up
            echo -n $(date +"%a, %b %d, %r") " connected"
            echo -n $(date +"%a, %b %d, %r") " connected" > /home/me/adsl.log
        fi
        status=1
    fi
    # enable if you want to see ping result
    # cat /tmp/ping.$ | head -2 | tail -1
    sleep 300 ;
done
 
Untested and surely there are better tools to do this, but if I were to script it I would go something like this. Also, I did fix up your script a bit, the indentation made it very hard to to follow what fi covers what if when they are all at the same indentation.
Code:
#!/bin/sh
good="300"
bad="10"
interval=$good

IP="8.8.8.8"

while sleep $interval; do
    ping -c 1 -q -n "$IP"
    status=$?

    if [ $status -eq 0 ] && [ $interval -eq $good ]; then
        echo "link is normal"
    elif [ $status -eq 0 ] && [ $interval -eq $bad ]; then
        echo "link back up"
        interval=$good
    elif [ $status -ne 0 ] && [ $interval -eq $bad ]; then
        echo "link still down"
    elif [ $status -ne 0 ] && [ $interval -eq $good ]; then
        echo "link just went down"
        interval=$bad
    fi
done
 
Thanks. Your code is way more elegant than mine.

The modem does not have logging capability and any other tool that I use from my FreeBSD platform is going to be more complicated to setup and consume more resources. This is the simplest solution I think.

I added the "W" flag to reduce wait time when connection is down, and found that the proper date format for logfile as below.
Code:
ping -c 1 -W 3 -q -n "$IP"
$(date +"%m-%d, %T%n")
 
Back
Top