[K12OSN] bash script help

Rob Asher rasher at paragould.k12.ar.us
Tue Oct 30 20:55:22 UTC 2007


Had a couple people ask how the results could be emailed to them so here's a revised version of my earlier script:

#!/bin/bash
# add IP or hostname separated by white space
HOSTS="10.40.12.1 10.40.12.2 10.40.12.10 10.40.12.26 10.40.12.56"

# Optional email report variables
SUBJECT="Host(s) Down!"
EMAIL="you at whereever.com" 

# number of ping requests
COUNT=3

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
    HOSTSDOWN=$(printf "%s\n%s\n" "$myHost" "$HOSTSDOWN")
    echo "Host : $myHost is down (ping failed) at $(date)" >> /usr/local/serverdown.txt
  fi
done

# uncomment to email results when done
#if [ -n "$HOSTSDOWN" ]; then
#mail -s "$SUBJECT" $EMAIL <<EOF
#Host(s) Not Responding:
#$HOSTSDOWN
#EOF
#fi

exit 0

Be aware though that if you're running this as a cron job, when a host stays down or you don't remove it from the HOSTS variable, you'll get repeated emails at whatever interval you have your cron job running.  :-/

Enjoy!
Rob


-------------------------------------
Rob Asher
Network Systems Technician
Paragould School District
(870)236-7744 Ext. 169


>>> "Doug Simpson" <simpsond at leopards.k12.ar.us> 10/30/2007 1:09 pm >>>
Hi all!

I have a script that I am trying to get to work and I can't get it right. It is a script that will write the names of offline servers to a textfile.

As it is, it only does the first server and repeats, *unless* the first server is not online, in which it writes to the file and then goes on to the next one.
How can I make it continue through the script regardless of whether or not each server is online, but still echo the text to the file if it finds one offline and then restart at the top of the list again?

The script follows.

******************************************
#!/bin/bash
while (true)
do
#sleep 30
if ping -i 3 -c 3 10.40.12.56
then
continue
else
echo "Doug's workstation is offline. Please check it!" >>serverdown.txt
fi
if ping -i 3 -c 3 10.40.12.1
then
continue
else
echo "Please check on Leopards Server. It appears to be offline!" >>serverdown.txt
fi
if ping -i 3 -c 3 10.40.12.26
then
continue
else
echo "Please check on Students server. It appears to be offline!" >>serverdown.txt
fi
if ping -i 3 -c 3 10.40.12.2
then
continue
else
echo "Please check on elementary lab server. It appears to be offline!" >>serverdown.txt
fi
if ping -i 3 -c 3 10.40.12.10
then
continue
else
echo "Please check on elementary primary server. It appears to be offline!" >>serverdown.txt
fi
done

***************************************************

Thanks for any assistance.

Doug

Doug Simpson
Technology Specialist
De Queen Public Schools
De Queen, AR
simpsond at leopards.k12.ar.us 


_______________________________________________
K12OSN mailing list
K12OSN at redhat.com 
https://www.redhat.com/mailman/listinfo/k12osn 
For more info see <http://www.k12os.org>

-- 

This message has been scanned for viruses and dangerous content by The MailScanner at the Paragould School District, http://paragould.k12.ar.us, and is believed to be clean.









More information about the K12OSN mailing list