Ping My Thing Mr. CRON

Discussion in 'Programming/Scripts' started by [email protected], Sep 18, 2019.

  1. danhansen@denmark

    danhansen[email protected] Member HowtoForge Supporter

    Hi,


    I'm running several rack-mounted linux servers all Ubuntu Server edition. I use mostly shellscripts to watch and control my systems.

    I would like to ma a script and have CRON run it for me. I know how to configure CROn so thats not it. What I very much would like, is for you guys to give me your suggestion on a little shellscript pinging a running server elsewhere. It can be locally and it can be somewhere on the internet. If you have a few tasty ideas of getting additional information along the way, please don't hesitate to show me ;)
    I want it to log it to and use sendmail to alert me about a server not running, but this I've done several times before, so thats not needed either.

    Maybe something like (Not a wizard, please don't bash me)

    #!/bin/bash
    ping 8.8.8.8
    if result = 4 numbers with . in between
    then /username/scripts/pingpong-isago.sh
    exit
    else /username/scripts/pingpong-isanogo.sh
    fi

    KR
    Dan
     
  2. Taleman

    Taleman Well-Known Member HowtoForge Supporter

    Code:
    #! /bin/bash
    
    set -x
    echo $1
    ping -c 3 $1
    RETVAL=$?
    echo $RETVAL
    if [ $RETVAL -eq 0 ] ; then
        echo "Ping successful"
    else
        echo "No answer to ping"
    fi
    
    Example use:
    Code:
    $ ./pinger.sh 8.8.8.8
    + echo 8.8.8.8
    8.8.8.8
    + ping -c 3 8.8.8.8
    PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
    64 bytes from 8.8.8.8: icmp_seq=1 ttl=55 time=36.1 ms
    64 bytes from 8.8.8.8: icmp_seq=2 ttl=55 time=37.0 ms
    64 bytes from 8.8.8.8: icmp_seq=3 ttl=55 time=33.7 ms
    
    --- 8.8.8.8 ping statistics ---
    3 packets transmitted, 3 received, 0% packet loss, time 2002ms
    rtt min/avg/max/mdev = 33.731/35.656/37.076/1.420 ms
    + RETVAL=0
    + echo 0
    0
    + '[' 0 -eq 0 ']'
    + echo 'Ping successful'
    Ping successful
    
     
    ahrasis likes this.

Share This Page