Program to reboot computer if no network connection?

X

xyz-2041

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.
 
T

Tim Harig

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.

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\"`"\else
/usr/bin/printf "%s - host down:\n" "`date +\"%m-%d-%y: %H:%M\"`" \ /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
 
T

Tim Harig

You may be able to do this quickest with a fancy .bat file.

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

Bit Twister

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.

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
Any ideas on how to proceed?

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
 
T

The Natural Philosopher

xyz-2041 said:
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.
I would suggest that effort is better spent fixing that..actually!

new cards are not expensive..
 
J

jacob navia

The said:
I would suggest that effort is better spent fixing that..actually!

new cards are not expensive..

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!
 
J

Joe Beanfish

Bit said:
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.

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
Any ideas on how to proceed?

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

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.
 
K

Kenny McCormack

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!

The correct answer, of course, is to do both. Replace the card *and*
put an alarm system in place to warn you (presumably, in time to do
something about it) the next time it fails.
 
J

John Stumbles

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!

Heh! back in a previous lifetime when I was a BOFH at a local seat of
higher edukashun we for a time had a network connection provided by a well
known operator we referred to affectionately[1] as 'Clueless and
Witless'[2]. For a period of days or even weeks (my memory's a bit hazy
now) the link on their kit would hang, but could be got running again by
dropping the corresponding interface on our Cisco. Rather than station a
PFY on link-toggling duty we programmed a Camel to do it :)


[1] not very ...

[2] one of their engineers turned up to look at their WAN kit installed on
our premises which was presented to us on a pair of 75 ohm coax cables
terminated in BNC connectors, apparently having stopped off on the way at
PC World to buy some BNC barrels and terminators since he didn't carry
such exotic items in his toolkit. And of course the ones he bought were 50
ohm.
 
D

Doug

jacob said:
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!

Right. Brand new Intel NIC, brand new motherboard, memory, etc.

I think it's a problem with the OS, and/or drivers. Once the
server boots, it works fine all day.

I'll take a look at these shell scripts and see if I can get
them to work on boot up.
 
N

Nate Eldredge

Right. Brand new Intel NIC, brand new motherboard, memory, etc.

Of course, just because it's new doesn't mean it isn't broken. Quite
the contrary, in fact; it could be defective, or suffering from infant
mortality, which would not apply to old hardware that used to work.
 
T

The Natural Philosopher

Kenny said:
The correct answer, of course, is to do both. Replace the card *and*
put an alarm system in place to warn you (presumably, in time to do
something about it) the next time it fails.
I haven't had a network layer fail in the last 10 years.

Not since the old NE1000 cards..
 
R

Richard Bos

Right. Brand new Intel NIC, brand new motherboard, memory, etc.

You _have_ tried reseating it, I presume? And swapping it with the one
in another server? Checked all cables?

Richard
 
D

Doug

Richard said:
You _have_ tried reseating it, I presume? And swapping it with the one
in another server? Checked all cables?

Richard

Yeppers on all that. Only happens on boot up. I figure it's some
weird incompatibility between the OS and NIC driver. Once it
boots and is working, all is well.

Will try to get a shell script working and report back.
 
M

Marty

Right. Brand new Intel NIC, brand new motherboard, memory, etc.

I think it's a problem with the OS, and/or drivers. Once the
server boots, it works fine all day.

I'll take a look at these shell scripts and see if I can get
them to work on boot up.

I had a network chip on my motherboard crap out on me. Same symptoms...
works fine for X hours, or all day long, then doesn't seem to exist
anymore. After some investigation, I found a fan on my motherboard
wasn't spinning. So it was a thermal hardware failure.

If it's really a software problem, then a reboot shouldn't be necessary
to get it back on its feet. You should be able to down the interface
and "rmmod" the driver, then re-"modprobe" it to get things back in
action. It's REALLY rare that a reboot is required for anything these
days (unless you're running Windows).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top