Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Program to reboot computer if no network connection?

Reply
Thread Tools

Program to reboot computer if no network connection?

 
 
xyz-2041
Guest
Posts: n/a
 
      06-19-2009
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.
 
Reply With Quote
 
 
 
 
Tim Harig
Guest
Posts: n/a
 
      06-19-2009
On 2009-06-19, xyz-2041 <(E-Mail Removed)> wrote:
> 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\"`"\
>> $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
 
Reply With Quote
 
 
 
 
Tim Harig
Guest
Posts: n/a
 
      06-19-2009
On 2009-06-19, Paul <-> wrote:
>> Any ideas on how to proceed?

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

>> If there was a bounty, where would I offer it?

> 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?
 
Reply With Quote
 
Tim Harig
Guest
Posts: n/a
 
      06-19-2009
On 2009-06-19, Paul <-> wrote:
> "Tim Harig" <(E-Mail Removed)> wrote in message
> news:3rH_l.121$(E-Mail Removed)...
>>>> If there was a bounty, where would I offer it?
>>> 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?

> No, I mean that you can pay other people to do it.


Bummer, I guess I better keep my "will program for food" sign.
 
Reply With Quote
 
Bit Twister
Guest
Posts: n/a
 
      06-19-2009
On Thu, 18 Jun 2009 20:42:50 -0700 (PDT), xyz-2041 wrote:

> 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

 
Reply With Quote
 
The Natural Philosopher
Guest
Posts: n/a
 
      06-19-2009
xyz-2041 wrote:
> 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..
 
Reply With Quote
 
jacob navia
Guest
Posts: n/a
 
      06-19-2009
The Natural Philosopher wrote:
> xyz-2041 wrote:
>> 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..


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!
 
Reply With Quote
 
Joe Beanfish
Guest
Posts: n/a
 
      06-19-2009
Bit Twister wrote:
> On Thu, 18 Jun 2009 20:42:50 -0700 (PDT), xyz-2041 wrote:
>
>> 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.
 
Reply With Quote
 
Kenny McCormack
Guest
Posts: n/a
 
      06-19-2009
In article <h1gilf$lig$(E-Mail Removed)-september.org>,
jacob navia <(E-Mail Removed)> wrote:
>The Natural Philosopher wrote:
>> xyz-2041 wrote:
>>> 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..

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

 
Reply With Quote
 
John Stumbles
Guest
Posts: n/a
 
      06-19-2009
On Fri, 19 Jun 2009 19:42:01 +0200, jacob navia wrote:

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

--
John Stumbles

Life is nature's way of keeping meat fresh
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Reboot, reboot, reboot Lawrence D'Oliveiro NZ Computing 12 03-07-2009 11:35 PM
How to reboot my computer Kathleen Microsoft Certification 3 12-28-2007 01:45 PM
To reboot the PIX or not reboot - that is the question Darren Green Cisco 1 03-14-2006 10:59 PM
Spontaneous Reboot when no activity on computer? Nico Computer Support 3 07-07-2004 06:45 AM
computer reboot Keith Joly Computer Information 2 11-06-2003 03:44 AM



Advertisments