Linux: Writing a tricky script to check connectivity

So, first and foremost, I'm having issues with my internet connection. Periodically, the connection drops across the network. The fix is simple enough: restart the modem. However, this gets old when the connection dies out every hour.

I can hit my surfboard on 192.168.100.1, and navigate to a page allowing me to reset the modem remotely (which is nice). I also know I can do this via the command line using telnet, and (I think) SSHing into it, and sending it the command 'reset'.

So, I'd like to create a script that (every 5 minutes or so) pings Comcast.net Home. If it gets a response, great, do nothing, and check again in 5 minutes.
If it doesn't, I want my computer to automatically SSH into the modem, and reset it.

I'm a programmer, but I generally bounce around with Java. Scripting is bizarre, but somewhat familiar. I know that I need the script to (generally)

-Run every 5 minutes
-ping
-store the results of the ping, preferabally in a variable, but otherwise in any readable format (I don't know if Linux is equipped with an automated text reader)
-if the results are good, do nothing
-if the results are bad:
-SSH
-Log into the SSH using the username and password (which I have)
-Pass it the command 'reset'
-Close the SSH

So, this is a pretty hefty script. I don't really know where to begin, or, more importantly, if all of these things are even possible.

I could use some input, and would greatly appreciate it.

Thank you
KFJ

Not sure what modem that you are using, check how to SSH to modem and get it reset, then put in script. For a start, try to google for understand shell scripting about the syntax and etc....

The best way is replace with reliable modem, mine is online for 24x7 and is working great... Thanks!

...and if this Motorola Surfboard has stats you can reach over SNMP or HTTP you wouldn't need to ping until you're sure it's not a problem between you and wherever it terminates on the ISP side. Think layers of dependencies. A more elaborate check then could look something like: machine -> LAN (cabling, link, router), machine -> modem (modem down, ISP), machine -> 1st hop router (routing, ISP), machine -> ping somewhere (or tcptrace in case remote doesn't like ICMP).

Here's some Bash scripting guides that may help:
BASH Programming - Introduction HOW-TO
Bash Guide for Beginners
Advanced Bash-Scripting Guide

Try some and post here as it's more efficient to correct your work, IMHO.

Thanks for the replies guys.

Yeah, it's a Motorola Surfboard.

Ok, so, as for layers of dependencies, That seems deep for me. I'm trying to keep this simple. If it can't ping, I don't really care why, I just care to fix it (And I know it would be more efficient to find the actual problem, but I'm taking a more general stance, ie, If the roof leaks, it's cheaper and quicker to find the leak, but replacing the whole roof works too.).

I'll take a look at the guides, but what I'm really asking is that, with the knowledge that I can, in fact, SSH into the modem and pass it the proper command, Is the rest of it POSSIBLE?

I'll figure it out, but I need to know if I can have a script:
Run every 5 minutes
Save information about a ping in a useful fashion (ie, variable containing time in ms, or a failure state)
SSH (I'm pretty sure this one works, could someone confirm that scripts can SSH?)

If you guys could tell me (if you know) if these things are possible, I'll figure out how to implement it all, but I don't want to spend hours learning scripting only to find out that some of these break rules or aren't possible for the language to handle.

Thanks
KFJ

Sure scripts can ssh, scripts can do pretty much anything. You can send a command
from a script over ssh like so

ssh <user>@<host> <command>

The issue you get to is if you don't want to type your password every iteration of the
script you want to see about copying your ssh key over to the remote server... Which
you can usually do like this:

# generate local key
ssh-keygen -t rsa

# copy key to remote host
ssh-copy-id -i ~/.ssh/id_rsa.pub <user>@<remote host>

However that may not work on your modem. After that you can script ssh commands
from that user with out having to type in a password everytime.

I had some similar code at home, heres a slightly modified version for you.

Note that there are two reset functions included, depending on if you will ssh or telnet. And you'll prolly need to read a few man-pages to get things right.

---------------------------------------
#!/usr/bin/env bash

MY_MODEM_IP=123.12.1.1
HOST_TO_CHECK=www.comcast.net
SOME_TIME=$(( 5 * 60 )) # seconds

ping_fails () {
# check your ping manpage for flags so that it
# o sends one ping then quits
# o waits only a second or so for the pong
! ping -XXX $HOST_TO_CHECK > /dev/null 2>&1
}

reset_modem_ssh () {
# Make sure you've made a public and private key if you're going
# with ssh.
{
echo 'reset';
} | ssh $MY_MODEM_IP
}

reset_modem_telnet () {
# If you go with telnet, dont forget to echo your password. I've
# noticed that some applications are a little slow, adding sleeps
# is a simple workaround.
{
sleep 0.2
echo "yourpassword"
sleep 0.2
echo "reset"
} | telnet $MY_MODEM_IP
}

while sleep $SOME_TIME ; do
ping_fails && reset_modem_ssh
done

------------------------------------
And to be on the safe side, specify the ping host with its ip address, not its hostname.