+ Post New Thread
Results 1 to 9 of 9
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
    5,527
    Thank Post
    1,339
    Thanked 470 Times in 307 Posts
    Blog Entries
    6
    Rep Power
    199

    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
    Posts
    4,006
    Thank Post
    489
    Thanked 1,343 Times in 729 Posts
    Rep Power
    429
    Have something in VB but don't think that will help you.

  3. #3
    morganw's Avatar
    Join Date
    Apr 2009
    Location
    Cambridge
    Posts
    816
    Thank Post
    46
    Thanked 132 Times in 126 Posts
    Rep Power
    40
    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
    431
    Thank Post
    60
    Thanked 38 Times in 35 Posts
    Rep Power
    24
    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
    429
    Thank Post
    69
    Thanked 90 Times in 75 Posts
    Rep Power
    61
    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 07:06 PM.

  6. Thanks to box_l from:

    RabbieBurns (21st January 2010)

  7. #6

    RabbieBurns's Avatar
    Join Date
    Apr 2008
    Location
    Sydney
    Posts
    5,527
    Thank Post
    1,339
    Thanked 470 Times in 307 Posts
    Blog Entries
    6
    Rep Power
    199
    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

  8. #7

    Join Date
    Nov 2012
    Location
    Lithuania
    Posts
    2
    Thank Post
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0
    Hello,
    I'm trying to make script with bash so I want to gain more experience with this super tool.

    I've trying to make a host script to make job more effective and faster.

    #!/bin/bash
    #By Rokas Klyvis
    echo "Iveskite adresa arba IP"
    read hostname
    host $hostname
    host www.$hostname
    host mail.$hostname
    whois $hostname | grep -E 'Domain:|Nameserver:|Status:|Registrar:'




    PACKETS=1


    for myHost in $hostname
    do
    check=$(ping -c $PACKETS $myHost | grep 'received' | awk -F',' '{ print $2 }' | awk '{ print $1 }')
    if [ $check -eq 0 ]; then
    echo "Hostname : $myHost nepingina"

    else
    echo "Hostas pingina OK"
    fi
    done


    So for example, if I put domain, that doesn't exist, this script gives error ./host.sh: line 16: [: -eq: unary operator expected

    I understand, that awk cutting doesn't help anymore, tried to make if statements with != ping: unknown host $myHost but still no luck, I just need to make, if host doesn't exists, just to show single line "Host doesn't exist" and that's it. It should be useful for many sysadmins so would be lovely to get your pros help for such newbies like me

    Regards.

  9. #8

    Steve21's Avatar
    Join Date
    Feb 2011
    Location
    Swindon
    Posts
    2,697
    Thank Post
    335
    Thanked 515 Times in 483 Posts
    Rep Power
    179
    It's been a while since I've done bash, but can't you ignore most of that and just use the return value from ping?

    Code:
    ping -c 1 $myHost > /dev/null
    if [ $? -eq 0 ]; then 
    echo "Ping Failed"
    else
    echo "Ping Ok"
    fi
    Every command in bash will always return a return code to $? so just use that to get your answer from. No need to populate/copy from output (or shouldn't be if i remember correctly)

    Should work anyway.

    - Edit: Might have my "failed" and "ok" wrong way round. But can't really test it on windows machine If it's doing it back to front, just change echos etc

    Steve
    Last edited by Steve21; 3rd November 2012 at 02:47 PM.

  10. #9

    Join Date
    Nov 2012
    Location
    Lithuania
    Posts
    2
    Thank Post
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0
    Thanks for quick reply, will try to make advanced sheet and than share, or maybe there are some good admin scripts? Will check later on

SHARE:
+ Post New Thread

Similar Threads

  1. Replies: 6
    Last Post: 27th November 2009, 09: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, 08: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
  •