REGISTER  


Program to reboot computer if no network connection?
This thread belongs to expert.forumgeeks.net


2009-06-18 20:42 GMT   |   #1
 
Hi folks,

Have a cronjob to reboot a Linux box in the wee hours of the morning.
Strange as it may seem, sometimes the network card is blocked.

Looking for a program that will ping a collection of IPs for 5-10
minutes. If no success, the program does a "shutdown -r now"
I don't have the time to program it myself.

Any ideas on how to proceed? If there was a bounty, where
would I offer it?

Thanks for pointing me in the right direction.
2009-06-18 23:45 GMT   |   #2
 
Looking for a program that will ping a collection of IPs for 5-10
minutes. If no success, the program does a "shutdown -r now"
I don't have the time to program it myself.

Look at the manual page for your version of ping. The FreeBSD ping
has the -o option, which causes ping to exit after receiving a ping
response successfully, the -c option, which specifies how many pings
to send, and the -t option, which causes ping to stop after a given
time whether it receives replies or not. Ping's exit status is
zero if it received a response to at least one ping.

Try pinging with options to force ping to quit eventually, say,
ping -c 10 -t 30 , possibly from a shell script. Keep track of
whether each ping for each address worked (got at least one response)
or not. Retry pings for a given host if desired.

Decide what your criterion for failure is (No response from *all*
of the IPS, no response from *any one* of the IPs, no response from
the majority of the IPs?). If (enough of) the pings fail, run
"shutdown -r now" from your shell script.

2009-06-19 03:20 GMT   |   #3
 
You may be able to do this quickest with a fancy .bat file.

Google for "freelancer". There are lots of websites (www.getafreelancer.com is one)
where you can set a fee, and people will make an offer for the job.
2009-06-19 03:20 GMT   |   #4
 
Doing this is C is way overkill. Its like cracking a walnut open with a
steamroller. This is much better accomplished with a shell script or at
most a scripting language such as perl or python.

I use the following script to monitor my servers from my personal desktop
and provide and audio announcement if any of the required ports on my
server are not open. It uses diff to check the trimmed output of nmap
against a known template. My version also logs the success of the test and
if it fails it writes a traceroute to the logs that I can use for
troubleshooting.

You should be able to modify this script to shutdown your system. You will
need to find an open port that you can monitor (port 80 of google should
work fine). Then you will need to provide the template of how the trimmed
output of nmap is supposed to look. Then you can replace the commands
which play sound with a reboot command. If you like, you can also remove
the commands that handle logging.

#! /bin/bash
# checkports.sh - script to check the ports of a given host
# logs whether successful and plays SOUND_FILE if it finds
# unavailable ports

export MON_HOST=example.com
export LOGFILE=$HOME/log/checkports.log
export MON_PORTS="22,25,80"
export MON_NORM_TMPL=$HOME/tmpl/normal_mon_tmpl.txt
export SOUND_FILE=$HOME/scripts/data/down.wav
export LOGFILE_MAX_SIZE="104857600"

if (/usr/bin/nmap -P 0 -p $MON_PORTS $MON_HOST | /bin/head -7 |
/usr/bin/tail -3\
| /usr/bin/diff /dev/stdin $MON_NORM_TMPL)
then
/usr/bin/printf "%s - system normal\n" "`date +\"%m-%d-%y:
%H:%M\"`"\
>> $LOGFILE
else
/usr/bin/printf "%s - host down:\n" "`date +\"%m-%d-%y: %H:%M\"`" \
>> $LOGFILE
/usr/bin/nmap -P 0 -p $MON_PORTS $MON_HOST | sed -e "s/^/\t/" \
>> $LOGFILE
/usr/bin/play $SOUND_FILE
/usr/bin/traceroute $MON_HOST | sed -e "s/^/\t/" >> $LOGFILE
fi

# manage the log file size

export LOGFILE_SIZE=$(ls -l $LOGFILE | awk '{print $5}')

if [ $LOGFILE_SIZE -gt $LOGFILE_MAX_SIZE ] ; then
tail -10000 $LOGFILE > $LOGFILE.tmp
mv $LOGFILE.tmp $LOGFILE
fi
2009-06-19 03:20 GMT   |   #5
 
He is using Linux. The equivilent of .bat files on Linux are known as
shell scripts.

Google for "freelancer". There are lots of websites (www.getafreelancer.com is one) where you can set a fee, and people will make an offer for the job.

You mean that you can get paid for this stuff?
2009-06-19 05:20 GMT   |   #6
 
No, I mean that you can pay other people to do it.
2009-06-19 05:20 GMT   |   #7
 
Bummer, I guess I better keep my "will program for food" sign. Sad
2009-06-19 05:20 GMT   |   #8
 
So, how much are you willing to pay. Where is the requirements document.
The above requirement is pretty vague on exactly when to run
the shutdown -r now

A shell script would seem an easy way to solve your problem.

Load an array with ip address,
for loop to loop through array of ip addresses,
ping ip from array,
bump counter on failure,
if counter greater than x, shutdown -r now


You might want to read/play here
http://tldp.org/LDP/abs/html/index.html


Something like this quick untested kludge would meet your requirement
as stated.


#!/bin/bash

ip_addy=(
192.168.1.136
192.168.1.137
192.168.1.139
)

_max=2
_count=0

#*************************************
#* make 1 ping wait up to 3 seconds
#*************************************

for ip in ${ip_addy[*]} ; do
/bin/ping -c1 -w3 $ip > /dev/null
if [ $? -ne 0 ] ; then
_count=$(( $_count + 1 ))
fi
done

if [ $_count -gt $_max ] ; then
shutdown -r now
fi

But I would suggest that effort is better spent fixing that..actually!

new cards are not expensive..
2009-06-19 13:20 GMT   |   #9
 
CORRECT ANSWER!!!

It is incredible how people, when confronted with a simple problem,
will add more and more layer of "fixes" instead of correcting
the source problem!

If the network card gets "stuck" (i.e. the software in the card is
faulty or the electronic parts are burned) it is better to just
replace the card as you say, instead of putting a shell script to
fix the faulty card with software!
2009-06-19 13:51 GMT   |   #10
 
Since the op only wants to boot if the card is stuck I'd assume that
being able to ping any host means it's not stuck and should not boot.
Need to reverse the logic and only boot if all pings fail. Any one
working means packets are going in and out.

1 2nextlast