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?
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