Program to reboot computer if no network connection?

Discussion in 'C Programming' started by xyz-2041, Jun 19, 2009.

  1. xyz-2041

    xyz-2041 Guest

    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.
    xyz-2041, Jun 19, 2009
    #1
    1. Advertising

  2. xyz-2041

    Tim Harig Guest

    On 2009-06-19, 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.


    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
    Tim Harig, Jun 19, 2009
    #2
    1. Advertising

  3. xyz-2041

    Tim Harig Guest

    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?
    Tim Harig, Jun 19, 2009
    #3
  4. xyz-2041

    Tim Harig Guest

    On 2009-06-19, Paul <-> wrote:
    > "Tim Harig" <> wrote in message
    > news:3rH_l.121$...
    >>>> 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. :(
    Tim Harig, Jun 19, 2009
    #4
  5. xyz-2041

    Bit Twister Guest

    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
    Bit Twister, Jun 19, 2009
    #5
  6. 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..
    The Natural Philosopher, Jun 19, 2009
    #6
  7. xyz-2041

    jacob navia Guest

    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!
    jacob navia, Jun 19, 2009
    #7
  8. xyz-2041

    Joe Beanfish Guest

    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.
    Joe Beanfish, Jun 19, 2009
    #8
  9. In article <h1gilf$lig$-september.org>,
    jacob navia <> 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.
    Kenny McCormack, Jun 19, 2009
    #9
  10. 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
    John Stumbles, Jun 19, 2009
    #10
  11. xyz-2041

    Guest

    jacob navia 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!


    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.
    , Jun 19, 2009
    #11
  12. xyz-2041

    Guest

    Kenneth Brody wrote:
    >
    > jacob navia 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!

    >
    > You can always add software+hardware for the fix:
    >
    > http://thedailywtf.com/Articles/ITAPPMONROBOT.aspx
    >
    > --
    > Kenneth Brody


    I would do this if it would work in our situation--seriously.

    I think I'll look at the shell scripts first.
    , Jun 19, 2009
    #12
  13. writes:

    > 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.
    Nate Eldredge, Jun 19, 2009
    #13
  14. Kenny McCormack wrote:
    > In article <h1gilf$lig$-september.org>,
    > jacob navia <> 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.
    >

    I haven't had a network layer fail in the last 10 years.

    Not since the old NE1000 cards..
    The Natural Philosopher, Jun 20, 2009
    #14
  15. xyz-2041

    Richard Bos Guest

    wrote:

    > jacob navia wrote:
    > >
    > > 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.


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

    Richard
    Richard Bos, Jun 20, 2009
    #15
  16. xyz-2041

    Guest

    Richard Bos wrote:
    >
    > wrote:
    >
    > > jacob navia wrote:
    > > >
    > > > 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.

    >
    > 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.
    , Jun 21, 2009
    #16
  17. xyz-2041

    Marty Guest

    wrote:
    > jacob navia 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!

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

    --
    Reverse the parts of the e-mail address to reply by mail.
    Marty, Jun 21, 2009
    #17
  18. xyz-2041

    Richard Bos Guest

    Kenneth Brody <> wrote:

    > jacob navia wrote:
    > > 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!

    >
    > You can always add software+hardware for the fix:
    >
    > http://thedailywtf.com/Articles/ITAPPMONROBOT.aspx


    One from my own practice: <http://www.xs4all.nl/~rlbos/entermachine/>.

    Richard
    Richard Bos, Jun 23, 2009
    #18
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joe Longo

    How to Reboot ?

    Joe Longo, Oct 14, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    546
    Kumar Gaurav Khanna [C# MVP]
    Oct 16, 2003
  2. Ken Tucker
    Replies:
    0
    Views:
    1,657
    Ken Tucker
    Jul 5, 2003
  3. =?Utf-8?B?VGVycnk=?=

    Want to Reboot server from ASPX page

    =?Utf-8?B?VGVycnk=?=, Apr 18, 2004, in forum: ASP .Net
    Replies:
    5
    Views:
    384
    Sharon
    Apr 19, 2004
  4. spamfurnace
    Replies:
    1
    Views:
    315
    Natty Gur
    Jun 5, 2004
  5. Jochen Friedmann

    shutdown or reboot computer

    Jochen Friedmann, Sep 15, 2003, in forum: Perl Misc
    Replies:
    5
    Views:
    148
    Thomas Kratz
    Sep 15, 2003
Loading...

Share This Page