Ping My Thing Mr. CRON

Discussion in 'Programming/Scripts' started by danhansen@denmark, Sep 18, 2019.

  1. danhansen@denmark

    danhansen@denmark Member HowtoForge Supporter


    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)

    if result = 4 numbers with . in between
    then /username/scripts/
    else /username/scripts/

  2. Taleman

    Taleman Well-Known Member HowtoForge Supporter

    #! /bin/bash
    set -x
    echo $1
    ping -c 3 $1
    echo $RETVAL
    if [ $RETVAL -eq 0 ] ; then
        echo "Ping successful"
        echo "No answer to ping"
    Example use:
    $ ./
    + echo
    + ping -c 3
    PING ( 56(84) bytes of data.
    64 bytes from icmp_seq=1 ttl=55 time=36.1 ms
    64 bytes from icmp_seq=2 ttl=55 time=37.0 ms
    64 bytes from icmp_seq=3 ttl=55 time=33.7 ms
    --- 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 -eq 0 ']'
    + echo 'Ping successful'
    Ping successful
    ahrasis likes this.

Share This Page