Alright, I have being checking out various posting here trying to hack together something for a friend
Needed:
A script that can run in cron doing:
ping of several hosts and notifying via email when they are unavailable.
I am not going to post my non working messes (I am a basic scripter), but I have tried several approaches and just dont have the strong knowledge base to push variables around in a script.
Suggestions Please..
using: bash on redhat linux box
actually here is the latest script:
#!/bin/bsh
#################################
for file in `cat iplist.txt`
do
ping -c 1 -A $file | grep errors | wc -l
read tom
echo $tom
if [ "$tom" != 0 ]
then
mail -s"Test" exx@xx.com
else
mail -s"Worked" exx@xx.com
fi
done
OH how the heck do I get the mail program to just fire out the email? I know in solaris mailx will nicely shoot out the email...
Also my variations of this produce nice frustrating results...like the if then aint working right. Figure read is seen it as a string? At least that is my guess...
For starters, make sure that iplist.txt is in the correct directory. I assume it will always be in the same directory as the script? If not, add a path to the file name. Second, for the mail portion, try using sendmail as this is commonly installed on RedHat. To check the status of the ping command, use a variable to capture the status. Not sure what the return value is of that ping command, adjust the IF statement to match.
for file in `cat iplist.txt`
do
PING_TEST=0
PING_TEST=`ping -c 1 -A $file | grep errors | wc -l` #read tom #echo $tom
if [ $PING_TEST -ne 0 ] #Use -ne (not equal) for integer comparison
then
sendmail -s"Test" exx@xx.com
else
sendmail -s"Worked" exx@xx.com
fi
done
Thanks, that worked much better. Getting both the positive and negative results I originally wanted. I have to work on the sendmail part (get the right switches).
Now here is another question
Lets say in my iplist.txt file I want to include a name for the ip address
ex: 192.168.60.5,router1
and I want to have the email subject to say
Router1 is down!
I am thinking that any of the text editors will help with this...(sed awk or what have you)
Using Oombera's suggestion on another thread....
echo "1024MB" | awk '{print substr($1,0,4)}' # print first four numbers / chars
echo "1024MB" | sed 's/[A-Za-z]//g' # removes all letters
x=${x%%[A-Za-z]} # remove everything after (and including) any chars - Note this is specific to Korn Shell
setup your variables in you iplist.txt so that they are in a consistent format. Then, instead of using $FILE, use one of the above suggestions to get either the IP address or the actual name. You will of course have to make some modifications. The code above will remove the characters from the variables. You will need to remove numbers and dots to reverse this.
Do a google search on SendMail Flags to get correct formatting.
what ends up happening is mail goes into an interactive mode looking for text, I end up having to ctrl-d to get it to kick the mail up and out. When I figure it out, I will post it...