Linux: Writing a tricky script to check connectivity


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Linux: Writing a tricky script to check connectivity
# 1  
Old 02-03-2009
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
# 2  
Old 02-03-2009
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!
# 3  
Old 02-03-2009
...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.
# 4  
Old 02-05-2009
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
# 5  
Old 02-05-2009
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.
# 6  
Old 02-06-2009
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Check connectivity with multiple hosts - BASH script available here

Hi everyone! Some time ago, I had to check connectivity with a big list of hosts, using different formats (protocol://server:port/path/, server:port, ....). I developed a script that checks the connectivity using different commands (ping, telnet, nc, curl). It worked for me so I'm sharing it... (9 Replies)
Discussion started by: Fr3dY
9 Replies

2. Shell Programming and Scripting

Help with shell script to check the tcp network connectivity between server

Hello, I have a requirement to check the tcp network connectivity between server it's running on and the list of host's and ports combination. i have written the below code but it doesn't work, but when i execute the nc command outside the script it works fine. please help me where i am... (8 Replies)
Discussion started by: sknovice
8 Replies

3. Shell Programming and Scripting

Check the connectivity of the DB through script, exit if no connection

check the connectivity of the DBs through script, script should exit if no connection and display the output as below. connectivity for DB1 is OK connectivity for DB2 is OK connectivity for DB3 is FAILED for DB in 1 2 3 do (sqlplus -s... (5 Replies)
Discussion started by: only4satish
5 Replies

4. Shell Programming and Scripting

Writing a script to run weekly/monthly - check for weekday or day-of-the-month

Hi all, I currently have a UNIX file maintenance script that runs daily as a cron job. Now I want to change the script and create functions/sub inside it that runs on a weekly or monthly basis. To run all the scripts' daily maintenance, I want to schedule it in cron as simply maint.sh... (1 Reply)
Discussion started by: newbie_01
1 Replies

5. Solaris

Sybase Connectivity Check through Shell Script

Hi, I need to check the sysbase database connectivity through the Unix Shell Script. Can you help me on the how to write the script to test the sysbase database connection. Thanks in Advance Nandha (0 Replies)
Discussion started by: nandha2387
0 Replies

6. Shell Programming and Scripting

How to check if a script is writing to a log

Hello, I have various scripts that writes to logs ( >> ) in different directories. I have create a script to automatically check different direcories for different errrors. However I do not want to remove logs if a script is still writing to a log. Is the log file opened for writing when the... (7 Replies)
Discussion started by: drbiloukos
7 Replies

7. IP Networking

Connectivity check.

How to check connectivity with an external server if I know the IP. Please note traceroute is not working for me, as it is saying command not found. (6 Replies)
Discussion started by: nixhead
6 Replies

8. Shell Programming and Scripting

Help writing a script check log not update.

:wall:Dear All.:p How to check log size every 10min. by script (not crontab) if log size not change with alert "Log not update" My Path :: /usr/home/logical/mono/log/tplink/ My Log :: mono11_tplink.log , mono12_tplink.log , etc I want oup put. EX. if log not update. . . . Fri Jan ... (1 Reply)
Discussion started by: ooilinlove
1 Replies

9. Shell Programming and Scripting

Check connectivity script

This past weekend I had some issues with my ISP. So for future purpose I'm going to have some logging on my internet so I'm able to attach log files to my complaint email if this issue reoccurs. Decided to do a simple ping script that runs every 5 or 10 min with crontab if ping fail write date... (5 Replies)
Discussion started by: chipmunken
5 Replies

10. Shell Programming and Scripting

Script to check connectivity

I want to write a script to check if a unix box say abc.tdc.cin.net can be connected or not on certain port say 22. right know i have to telnet them manually from DOS prompt and if it is successful then isay it is connected. Also to check Database connectivity I am using tnsping From DOS prompt.... (3 Replies)
Discussion started by: kukretiabhi13
3 Replies
Login or Register to Ask a Question