ping script with email option

Hi

I have already posted about this issue here and I got reply from some members. Now I need few modifications to be done. Can someone assist ?

I need a script to be put in cron and this script should run everyday morning at 06:00 AM.

I have a filed named "network_hosts" and the below are the contents of the file

root@[/] #cat network_hosts
frssit02.standardchartered.co.in
frssit01.standardchartered.co.in
cbspssit.standardchartered.co.in
cbspsuat.standardchartered.co.in
cbsappfs1.standardchartered.co.in
cbsappfs2.standardchartered.co.in
cbdodbfs2.standardchartered.co.in

All the servers are live except cbdodbfs2.standardchartered.co.in as we have shutdown this lpar.

The script should ping all the servers at 06:00 AM

Once the script has been run at 06:00 AM it should send me an email to me mentioning which servers are live and which one are dead.

It would be better if the email I get looks like the below :

frssit02.standardchartered.co.in - this server is alive
frssit01.standardchartered.co.in - this server is alive
cbspssit.standardchartered.co.in - this server is alive
cbspsuat.standardchartered.co.in - this server is alive
cbsappfs1.standardchartered.co.in - this server is alive
cbsappfs2.standardchartered.co.in - this server is alive
cbdodbfs2.standardchartered.co.in - this server is not alive, please contact the unix support team

What have you tried so far? Where is the particular problem? There is often being asked for a script to ping hosts and also questions on how to send mails. Show efforts please and be it at least to use the search function of the forum first, thanks.

I moderate any answers with complete solutions before the OP didn't answer - thanks for your understanding.

Google Search Results for ping script email | The UNIX and Linux Forums

You'd really just want to output the results to a file and then mail the entire file to your designated recipients. Such as the much maligned:

cat output.file |mailx -s "Ping results..." users@domain.com 

Just as an aside, however, you might want to think about listing internal domain contents when posting on a forum like this. Just as a matter of security...it's generally best if you don't broadcast the contents of your pockets.

Hi Zaxxon

The scripts I got are very good and they worked fine.
But they just send the email with the server names which are down.
But my management is expecting an email something like the below one :

frssit02.standardchartered.co.in - this server is alive
frssit01.standardchartered.co.in - this server is alive
cbspssit.standardchartered.co.in - this server is alive
cbspsuat.standardchartered.co.in - this server is alive
cbsappfs1.standardchartered.co.in - this server is alive
cbsappfs2.standardchartered.co.in - this server is alive
cbdodbfs2.standardchartered.co.in - this server is not alive, please contact the unix support team

Please assist

There should be a line where you test the output of ping, maybe rest the return code or parse it's output, whatever. At this place just put another if condition or something an pass those that are positiv ie. alive to the same file, concatenating it.

Hi Zaxxon

Im just trained in AIX and I dont know anything about the scripts.
Can you please do the modifications in this script and post the new script.

The below is the script I use :

root # cat pingnodes.ksh  | pg
#!/usr/bin/ksh
#
# SCRIPT: pingnodes.ksh
#
# PURPOSE:  This script is used to ping a list of nodes and
# send e-mail notification (or alpha-numeric page) of any unreachable
# nodes.
#
# set -x # Uncomment to debug this script
# set -n # Uncomment to check command syntax without any execution
#
#######################################################

# Set a trap and clean up before a trapped exit...
# REMEMBER: you CANNOT trap "kill -9"

trap 'echo "\n\nExiting on trapped signal...\n" \
        ;exit 1' 1 2 3 15

#######################################################
# Define and initialize variables here...

PING_COUNT="3"          # The number of times to ping each node
PACKET_SIZE="56"  # Packet size of each ping

typeset -u PINGNODES    # Always use the UPPERCASE value for $PINGNODES
PINGNODES="TRUE"        # To enable or disable pinging FROM this node - "TRUE"

typeset -u MAILOUT      # Always use the UPPERCASE value for $MAILOUT
MAILOUT="TRUE"          # TRUE enables outbound mail notification of events

UNAME=$(uname)          # Get the UNIXUnix flavor of this machine

PINGFILE="/usr/local/bin/ping.list" # List of nodes to ping

if [ -s $PINGFILE ]
then
      # Ping all nodes in the list that are not commented out and blank
      PINGLIST=$(cat $PINGFILE | grep -v '^#')
else
echo "\nERROR: Missing file - $PINGFILE"
      echo "\nList of nodes to ping is unknown...EXITING...\n"
      exit 2
fi

MAILFILE="/usr/local/bin/mail.list" # List of persons to notify

if [ -s $MAILFILE ]
then
        # Ping all nodes in the list that are not commented out and blank
        MAILLIST=$(cat $MAILFILE | egrep -v '^#')
else
        echo "\nERROR: Missing file - $MAILFILE"
        echo "\nList of persons to notify is unknown...\n"
        echo "No one will be notified of unreachable nodes...\n"
fi

PING_OUTFILE="/tmp/pingfile.out" # File for e-mailed notification
>$PING_OUTFILE  # Initialize to an empty file

integer INTERVAL="3" # Number of seconds to sleep between retries

# Initialize the next two variables to NULL

PINGSTAT=    # Number of pings received back from pinging a node
PINGSTAT2=   # Number of pings received back on the secopnd try

THISHOST=`hostname`     # The hostname of this machine

########################################################
############ DEFINE FUNCTIONS HERE #####################
########################################################

function ping_host
{
# This function pings a single node based on the UNIXUnix flavor

# set -x # Uncomment to debug this function
# set -n # Uncomment to check the syntax without any execution

# Look for exactly one argument, the host to ping

if (( $# != 1 ))
then
     echo "\nERROR: Incorrect number of arguments - $#"
     echo "       Expecting exactly one argument\n"
     echo "\t...EXITING...\n"
     exit 1
fi

HOST=$1 # Grab the host to ping from ARG1.

# This next case statement executes the correct ping
# command based on the UNIXUnix flavor
case $UNAME in

AIX|Linux)
           ping -c${PING_COUNT} $HOST 2>/dev/null
           ;;
HP-UX)
           ping $HOST $PACKET_SIZE $PING_COUNT 2>/dev/null
           ;;
SunOS)
           ping -s $HOST $PACKET_SIZE $PING_COUNT 2>/dev/null
           ;;
*)
           echo "\nERROR: Unsupported Operating System - $(uname)"
           echo "\n\t...EXITING...\n"
           exit 1
esac
}

#######################################################

function ping_nodes
{
#######################################################
#
# Ping the other systems check
#
# This can be disabled if you do not want every node to be pinging all
# of the other nodes.  It is not necessary for all nodes to ping all
# other nodes .aAlthough, you do want more than one node doing the pinging
# just in case the pinging node is down.  To activate pinging the
# "$PINGNODES" variable must be set to "TRUE".  Any other value will
#  disable pinging from this node.
#

# set -x # Uncomment to debug this function
# set -n # Uncomment to check command syntax without any execution

if [[ $PINGNODES = "TRUE" ]]
then
     echo     # Add a single line to the output

     # Loop through each node in the $PINGLIST

     for HOSTPINGING in $(echo $PINGLIST) # Spaces between nodes in the
                                          # list is are assumed
     do
          # Inform the uUser what is gGoing oOn

          echo "Pinging --> ${HOSTPINGING}...\c"

          # If the pings received back is equal to "0" then you have a
          # problem.
# Ping $PING_COUNT times, extract the value for the pings
          # received back.


          PINGSTAT=$(ping_host $HOSTPINGING | grep transmitted \
                     | awk '{print $4}')

          # If the value of $PINGSTAT is NULL, then the node is
          # unknown to this host

          if [[ -z "$PINGSTAT" && "$PINGSTAT" = '' ]]
          then
               echo "Unknown host"
               continue
          fi
          if (( PINGSTAT == 0 ))
          then    # Let's do it again to make sure it really is reachable

               echo "Unreachable...Trying one more time...\c"
               sleep $INTERVAL
               PINGSTAT2=$(ping_host $HOSTPINGING | grep transmitted \
                         | awk '{print $4}')

               if (( PINGSTAT2 == 0 ))
               then # It REALLY IS unreachable...Notify!!
                    echo "Unreachable"
                    echo "Unable to ping $HOSTPINGING from $THISHOST" \
                          | tee -a $PING_OUTFILE
               else
                    echo "OK"
               fi
          else
               echo "OK"
          fi

     done
fi
}

######################################################

function send_notification
{
if [ -s $PING_OUTFILE -a  "$MAILOUT" = "TRUE" ];
then
        case $UNAME in
        AIX|HP-UX|Linux) SENDMAIL="/usr/sbin/sendmail"
        ;;
        SunOS) SENDMAIL="/usr/lib/sendmail"
        ;;
        esac

        echo "\nSending e-mail notification"
$SENDMAIL -f root@$THISHOST $MAILLIST < $PING_OUTFILE
fi
}

##################################################
############ START of MAIN #######################
##################################################


ping_nodes
send_notification


# End of script

At the end of the function ping_nodes you should add an else with instructions what to do, if the test turns out to be not "TRUE".