ping script with email option


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting ping script with email option
# 1  
Old 01-10-2012
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

Code:
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 :

Code:
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

# 2  
Old 01-10-2012
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.
# 3  
Old 01-10-2012
# 4  
Old 01-10-2012
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:

Code:
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.
# 5  
Old 01-10-2012
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 :
Code:
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
# 6  
Old 01-10-2012
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.
# 7  
Old 01-10-2012
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 :


Code:
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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Ping test sends mail when ping fails

help with bash script! im am working on this script to make sure my server will stay online, so i made this script.. HOSTS="192.168.138.155" COUNT=4 pingtest(){ for myhost in "$@" do ping -c "$COUNT" "$myhost" &&return 1 done return 0 } if pingtest $HOSTS #100% failed... (4 Replies)
Discussion started by: mort3924
4 Replies

2. Shell Programming and Scripting

Script to call a menu script and redirect each option to a text file

Hello, I want to design a script that will call an existing menu script and select options one by one and redirict the out put to a file. For example;- In the script MENU.sh there are 10 options i want to design a script MENU2.sh that will select option 2 3 4 6 7 10 and redirict the output... (4 Replies)
Discussion started by: spradha
4 Replies

3. Shell Programming and Scripting

Ping Host Until it is up and email

Hi I am trying to write a script which runs until the host is up. i got it figured out that it needs to be in loop till it return $? = 0. Not getting it through though. I am not sure about the 6th line !!! #!/bin/sh HOSTS="host.txt" ping(){ for myhost in "$HOSTS" do ping -c -1 "$myhost"... (8 Replies)
Discussion started by: Antergen
8 Replies

4. Shell Programming and Scripting

[Solved] adding email option to KSH

Hi, I wanted to add a email option to this script. and was wondering if anyone could help me out. #!/bin/ksh echo "Finding hdisk" <DIR>/find-disk i=1 b=0 p=0 while ... (2 Replies)
Discussion started by: vpundit
2 Replies

5. Shell Programming and Scripting

Need bash script to ping the servers and rename the output file each time the script is ran

HI, I have a file serverlist in that all host names are placed. i have written a small script #./testping #! /bin/bash for i in `cat serverlist` do ping $i >> output.txt done so now it creates a file output.txt till here fine.. now each time i run this script the output file... (4 Replies)
Discussion started by: madhudeva
4 Replies

6. Shell Programming and Scripting

Email with cc and bcc option

Hi, I am able to send email from AIX using the following command. cat email.txt | mail -s "Mail Subject" venkat@gmail.com Our requirement is send email with cc and bcc option. We are getting email with from email address as our UNIX server user. We would like to change that, please let us know... (2 Replies)
Discussion started by: vfrg
2 Replies

7. Shell Programming and Scripting

perl script command line option driven script

could someone show me a sample command line option driven script? i want to see an easy way to write one and how i can execute it using command line options such as typing in read.pl -i <id> -c <cmds> -s <start> -e <end> would read out all the commands run by ID . from start time to... (7 Replies)
Discussion started by: kpddong
7 Replies

8. Shell Programming and Scripting

how do I change the email sender if I donot have the -r option?

Hey, after I check the man page for mail and mailx, I did not find the -r option. But, I have to change the email sender, how can I do it? Send Mode mailx address... Receive Mode mailx -e mailx mailx -f ... (0 Replies)
Discussion started by: freelong
0 Replies

9. Shell Programming and Scripting

recently introduced to the newer option for find...does an older option exist?

To find all the files in your home directory that have been edited in some way since the last tar file, use this command: find . -newer backup.tar.gz Is anyone familiar with an older solution? looking to identify files older then 15mins across several directories. thanks, manny (2 Replies)
Discussion started by: mr_manny
2 Replies

10. Shell Programming and Scripting

-c option in ping command

What does '-c' mean in ping command? Is this option specific to bash shell? Deepa (3 Replies)
Discussion started by: Deepa
3 Replies
Login or Register to Ask a Question