+ Post New Thread
Results 1 to 6 of 6
Scripts Thread, script to only run if it cant ping a host in Coding and Web Development; Ive got a server that runs a runs a script on the hour. Trouble is said server is a bit ...
  1. #1

    RabbieBurns's Avatar
    Join Date
    Apr 2008
    Location
    Sydney
    Posts
    4,510
    Blog Entries
    6
    Thank Post
    930
    Thanked 402 Times in 257 Posts
    Rep Power
    141

    script to only run if it cant ping a host

    Ive got a server that runs a runs a script on the hour. Trouble is said server is a bit prone to the occasional lockup or crash etc and sometimes the script wont run untill I can get someone to reboot the server..

    Got a backup server that runs, that isnt really capable of running the script regularly but could cope with it for a couple of days.. I was wondernig if there was a wayh of running a script on the backup server to do the following

    Check main server is replying to ping
    if OK do nothing
    if no ping, run the script locally

    Both machines are gentoo linux so a perl / bash / python or whatever would be fine to run

    Any ideas would be appreciated.. cheers..

  2. #2

    SYSMAN_MK's Avatar
    Join Date
    Sep 2005
    Location
    Milton Keynes
    Posts
    3,770
    Blog Entries
    2
    Thank Post
    418
    Thanked 1,133 Times in 647 Posts
    Rep Power
    321
    Have something in VB but don't think that will help you.

  3. #3
    morganw's Avatar
    Join Date
    Apr 2009
    Location
    Cambridge
    Posts
    625
    Thank Post
    43
    Thanked 84 Times in 79 Posts
    Rep Power
    26
    This looks like a BASH script that would do it.

    Code:
    #!/bin/bash
    # Simple SHELL script for Linux and UNIX system monitoring with
    # ping command
    # -------------------------------------------------------------------------
    # Copyright (c) 2006 nixCraft project <http://www.cyberciti.biz/fb/>
    # This script is licensed under GNU GPL version 2.0 or above
    # -------------------------------------------------------------------------
    # This script is part of nixCraft shell script collection (NSSC)
    # Visit http://bash.cyberciti.biz/ for more information.
    # -------------------------------------------------------------------------
    # Setup email ID below
    # See URL for more info:
    # http://www.cyberciti.biz/tips/simple-linux-and-unix-system-monitoring-with-ping-command-and-scripts.html
    # -------------------------------------------------------------------------
     
    # add ip / hostname separated by white space
    HOSTS="cyberciti.biz theos.in router"
     
    # no ping request
    COUNT=1
     
    # email report when
    SUBJECT="Ping failed"
    EMAILID="me@mydomain.com"
    for myHost in $HOSTS
    do
      count=$(ping -c $COUNT $myHost | grep 'received' | awk -F',' '{ print $2 }' | awk '{ print $1 }')
      if [ $count -eq 0 ]; then
        # 100% failed
        echo "Host : $myHost is down (ping failed) at $(date)" | mail -s "$SUBJECT" $EMAILID
      fi
    done
    Got it from here
    UNIX / Linux Shell Script For Monitoring System network with ping command

  4. #4
    Gerry's Avatar
    Join Date
    Jun 2007
    Location
    North Wales
    Posts
    411
    Thank Post
    53
    Thanked 35 Times in 32 Posts
    Rep Power
    18
    I Googled perl ping and got this:
    Net::Ping - perldoc.perl.org
    The example script shows how to use it.

    Then, to launch a script if there's no ping, try:
    exec - perldoc.perl.org

    Then you could cron your "pinging" script to run it every minute, hour, day, specific time etc...

  5. #5
    box_l's Avatar
    Join Date
    May 2007
    Location
    Herefordshire
    Posts
    337
    Thank Post
    50
    Thanked 68 Times in 56 Posts
    Rep Power
    21
    how about these?

    Code:
    #!/bin/bash
    
    if ping -c 1 4.2.2.2
    then
      : # colon is a null and is required
    else
       **** your command here ****
    fi
    and found this

    in an old script of mine, cant remember where it came from tho.

    this one would detect if the link was dropping some of the packets

    Code:
    #!/bin/bash
    while [ -n "$(ping -c 1 192.168.0.1|grep 100%)" ]
    do
    : # colon is a null and is required
    else
    **** your command here ****
    BoX
    Last edited by box_l; 21st January 2010 at 08:06 PM.

  6. Thanks to box_l from:

    RabbieBurns (21st January 2010)

  7. #6

    RabbieBurns's Avatar
    Join Date
    Apr 2008
    Location
    Sydney
    Posts
    4,510
    Blog Entries
    6
    Thank Post
    930
    Thanked 402 Times in 257 Posts
    Rep Power
    141
    Quote Originally Posted by box_l View Post
    how about these?

    Code:
    #!/bin/bash
    
    if ping -c 1 4.2.2.2
    then
      : # colon is a null and is required
    else
       **** your command here ****
    fi
    Thats perfect. Simple and effective. I added a > /dev/null to the first line to supress the output of the ping to the screen. Thanks

SHARE:
+ Post New Thread

Similar Threads

  1. Replies: 6
    Last Post: 27th November 2009, 10:53 PM
  2. [Arch] Run a command on the host
    By Arcath in forum *nix
    Replies: 1
    Last Post: 6th July 2009, 11:43 AM
  3. Ping Script
    By FN-GM in forum Scripts
    Replies: 5
    Last Post: 22nd April 2009, 10:49 AM
  4. Script to check various ping results
    By localzuk in forum Scripts
    Replies: 6
    Last Post: 30th March 2009, 10:15 PM
  5. No Ping Script
    By mattx in forum Scripts
    Replies: 16
    Last Post: 22nd February 2008, 09:03 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •