Solved

Need Bash script to monitor and alert via email on a Buffalo router

Posted on 2010-11-24
11
891 Views
Last Modified: 2013-12-26
Hi Guys

I'm using a buffalo router at home with a dd-wrt firmware buffalo router model: WZR-HP-G300NH .
If you are familiar with dd-wrt firmware, you know you can run scripts from the router so I need a bash or python script to ping one of my computer every 10 min to check is the interface is up   and if is down to send me an email.  Just to let you know I have no programing experience but I'm interested to learn Linux shell and python if anyone can recommend me the best books out there to learn Shell and Python scripts I will really appreciated.  I hope this is not too complex to accomplish.  I know I can easily accomplish this using nagios or other mon systems but when I'm not home the only thing that is on is my router also I don't think nagios can be run withing my router.  I also browse the dd-wrt site but did not find anything similar of what I'm looking for or may be I missed the article.  Thank you so much in advance guys.
0
Comment
Question by:toroblanco2002
  • 6
  • 5
11 Comments
 
LVL 3

Expert Comment

by:Vryali
ID: 34210875
The best way, in my opinion, especially if you're trying to learn some of the bits about Linux, would be to use a simple shell script and then put it into a cronjob (rather than have it run perpetually, though that'd work too).

That said, you can do a cronjob that runs every 10 minutes like so:

Run the command crontab -e (This brings you into the crontab editor).  It uses vi, so if you aren't familiar with that, you may want to look at a cheat sheet.  Hitting 'i' puts you in insert mode, then you'd want to add the following line:

10,20,30,40,50 * * * * /home/toroblanco/script/monitor.sh

Then hit the escape key and type ':wq' without the quotes (That says to write to the crontab and then quit the editor).  You'd changet that last argument to whatever the path is to your script, by the by.

The wikipedia link for cron is pretty good: http://en.wikipedia.org/wiki/Cron

I can't recommend any books on shell scripting, as honestly, I've never found one.  The best way to learn in my experience has just been to do it and struggle through it.  That said, here's a link that should help: http://linuxconfig.org/Bash_scripting_Tutorial

When I want to learn a new language, I use the following website to give myself 'something to do' such that I can use it: http://projecteuler.net/

Finally, the actual script that I named, for example, monitor.sh

#!/bin/bash
ping -c 3 somepcname
if [[ $? != 0 ]]; then
    date '+%m/%d - %H:%M' : Connection Unavailable' >> /var/log/monitor.log
    mail -s "Server down, oh noes!" worriedhuman@ouchserver.com < /var/log/monitor.log
else
    date '+%Y-%m-%d %H:%M:%S Connection Available' >> /var/log/monitor.log
fi

Cheers =)
0
 
LVL 3

Accepted Solution

by:
Vryali earned 500 total points
ID: 34210897
Realized I made a mistake in my edits of the script I posted, let me fix that:

#!/bin/bash
ping -c 3 somepcname
if [[ $? != 0 ]]; then
    date '+%m/%d - %H:%M : Connection Unavailable' >> /var/log/monitor.log
    mail -s "Server down, oh noes!" worriedhuman@ouchserver.com < /var/log/monitor.log
else
    date '+%m/%d %H:%M : Connection Available' >> /var/log/monitor.log
fi

Also, a bit of a further explanation on the bash script, since if you're new I image it's not self-explanatory

#!/bin/bash
This declares the shell to use to run the script.  It's simply good practice, though in general scripts will work on most modern systems without it.

ping -c 3 somepcname
Ping your PC 3 times, nothing special here.

if [[ $? != 0]]; then
This is (obviously) a basic if check, and the evaluated expression breaks down like so:
$? - the return value of the last command to run, in this case ping
!= - ! is the not operator, so this is 'not equal to'
0 - value returned by ping for an error/unsuccessful ping.

Next I just run the date command to put the date in a format I like, with a message that I then pipe to a log file I called /var/log/monitor.log.  Do note that, when using >, one > means overwrite, >> means append.

mail -s "Server down, oh noes!" worriedhuman@ouchserver.com < /var/log/monitor.log
This is a basic use of the mail command.  -s means subject, then the address to send to, and then an input pipe (<) that says to use /var/log/monitor for the body of the message.

The else statement is not needed, you can very safely cut that out and just have the following if you wanted

if [[ $? != 0 ]]; then
    date '+%m/%d - %H:%M : Connection Unavailable' >> /var/log/monitor.log
    mail -s "Server down, oh noes!" worriedhuman@ouchserver.com < /var/log/monitor.log
fi

fi
In shell scripting, when you end a case statement or if statement, you specify this by typing it backwards (if and fi, case and esac).

Should do it =]
0
 
LVL 3

Author Comment

by:toroblanco2002
ID: 34213428
Thank you I check this when I get home. Can I copy and paste this on the cron tab via the web interface of my router? Or I have to ssh to it?  Also do I have to make the script executable first? Chmod +x
0
 
LVL 3

Expert Comment

by:Vryali
ID: 34213643
Yes, you'll need to make it executable.  I'm not very familiar with the web interface, but you could try that first.  If it didn't it should work from the ssh prompt just fine.
0
 
LVL 3

Author Comment

by:toroblanco2002
ID: 34213718
Ok, as soon as I get out from work I will try this and post back.  Thanks again
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 3

Expert Comment

by:Vryali
ID: 34215730
I believe you may have misclosed this?  I gave you a viable solution, some useful links, and you thanked me for it :)  need to close it under my solution for me to get points.
0
 
LVL 3

Author Closing Comment

by:toroblanco2002
ID: 34217373
Ok I'm trying to close now under your solution  let me know if I did it rigth.
Thanks
0
 
LVL 3

Author Comment

by:toroblanco2002
ID: 34217402
Sorry I think I put it on the wrong one again
Can you object it again so I can accept the rigth solution
0
 
LVL 3

Expert Comment

by:Vryali
ID: 34218579
You got it right mate, cheers =]

If for some reason you run into problems with it, post up here and let me know the specific error or problem you have.  Best of luck.
0
 
LVL 3

Author Comment

by:toroblanco2002
ID: 34218643
Thank you.  I have another question open maybe you know something about it. I'm to ssh from work to my router. and getting a time out connection error.   thanks
0
 
LVL 3

Author Comment

by:toroblanco2002
ID: 34218655
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Dictionaries contain key:value pairs. Which means a collection of tuples with an attribute name and an assigned value to it. The semicolon present in between each key and values and attribute with values are delimited with a comma.  In python we can…
I recently attended Cisco Live! in Las Vegas, a conference that boasted over 28,000 techies in attendance, and a week of hands-on learning hosted by a solid partner with which Concerto goes to market.  Every year, Cisco displays cutting-edge technol…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now