0

I have this script I am working but cant seem to get it to send an email. I have a bash debug going but the script doesn't seem to be executing the mail command. Any help would be greatly appreciated. I have the email variable set as such

echo "Please enter email address"
read -r var2

and I have the email variable set as

EMAIL="$var2"

The script creates the log file that cat uses, and it seems to do everything I need "except" send the email. The part that does the work is below.

while true;
do
cat ip_monitor_list.txt | while read IP
do
fping -c 1 -q "$IP" > /dev/null
if [ $? -eq 0 ]; then
STATUS=$(cat $LOG."$IP")
if [ "$STATUS" != "$IP-UP!" ]; then
echo "$(date): ping OK, $IP host is UP!" |
mail -s "$IP host is UP!" "$EMAIL"
fi
echo "$IP-UP!" > $LOG.$IP

I also have an "else statement very similar to the above but to do the same thing if it is down. I noticed if I try the mail command by itself just typing it out in the terminal linux$mail -s "subject" myemail@blah.com, it works. But if I paste the below directly in the terminal like below

echo "$(date): ping Failed, $IP host is DOWN!" | mail -s "$IP host is DOWN!" myemail@blah.com

I get -bash: !": event not found

Thanks again for any help I really appreciate it.

Here is the updated version in its entirety.

#!/bin/bash
exec 5> debug_output.txt
BASH_XTRACEFD="5"
PS4='$line0: '
set -x
trap "exit" INT
echo "Please enter time in seconds:"
read -r var1
echo "Please enter email address"
read -r var2
LOG=mylog.log
SECONDS="$var1"
EMAIL="$var2"
#Array to write IP's pasted to ip_monitor_list.txt file.
arrIP=()
echo "Enter the IP addresses here enter and Ctrl-d when finished  :"
while read IP
do
arrIPAddress+=($IP)
echo "$IP" >> ip_monitor_list.txt
echo "$IP-UP\!" > $LOG.$IP
done
while true;
do
cat ip_monitor_list.txt | while read IP
do
fping -c 1 -q "$IP" > /dev/null
if [ $? -eq 0 ]; then
STATUS=$(cat $LOG."$IP")
if [ "$STATUS" != "$IP-UP\!" ]; then
echo "$(date): ping OK, $IP host is UP\!" |
mail -s "$IP host is UP\!" "$EMAIL"
fi
echo "$IP-UP\!" > $LOG.$IP
else
STATUS=$(cat $LOG."$IP")
if [ "$STATUS" != "$IP-DOWN\!" ]; then
echo "$(date): ping Failed, $IP host is DOWN\!" |
mail -s "$IP host is DOWN\!" "$EMAIL"
fi
echo "$IP-DOWN\!" > $LOG.$IP
fi
done
sleep $SECONDS
DONE
#File cleanup
rm ip_monitor_list.txt
Rchammond
  • 5
  • 4
  • Be extra careful when using exclamation marks in Bash scripts. The "!" is interpreted as a history reference. Try your script without exclamation marks and then put them back carefully as "\!". – Dima Chubarov Jan 13 '18 at 06:54
  • So I figured out the bash "even not found" error that was because I had ! bash treats that as a special character. So like this it works from the command line – Rchammond Jan 13 '18 at 06:57
  • Dmitri , thanks so much yea I just figured that bit out. so – Rchammond Jan 13 '18 at 06:58
  • This works echo "$(date): ping Failed, $IP host is DOWN" | mail -s "$IP host is DOWN" myemail@blah.com now, but the script still does not send the email – Rchammond Jan 13 '18 at 06:59
  • Now mail waits for the body of the email message on its standard input stream. Try `echo "$IP host is UP\!" | mail -s "$IP host is UP\!" "$EMAIL"` – Dima Chubarov Jan 13 '18 at 07:20
  • well I tried removing the ! all together , and I put a test ip in my list of 10.10.10.10 that I knew would show down, and it , successfully sent 1 email stating it was down , but thats it , and for the ip's that show UP it did not send and email. I tried the same by adding the ! like \! and it did the same thing, only sends one email of the one that is reporting down, but no other emails are sent , I would like to get an email for UP or DOWN each time fping runs in the script so still not sure why its not working – Rchammond Jan 13 '18 at 07:53
  • You probably changed the script already quite a bit. Would you post the updated version below the original post? – Dima Chubarov Jan 13 '18 at 08:04
  • Sorry if thats too much info , but figured I would post the whole thing as its not too long. Thats the most recent. – Rchammond Jan 13 '18 at 08:31
  • `if [ "$STATUS" != "$IP-DOWN\!" ];` says if `$STATUS` is not equal `$IP-DOWN` then do something. You probably intended it to be `if [ "$STATUS" == "$IP-DOWN\!" ];`. – Dima Chubarov Jan 13 '18 at 08:44
  • Dmitri !!!!! your so awesome!!! If I was in Russia would buy you a beer! my friend :-) man thanks soo much !!!! – Rchammond Jan 13 '18 at 08:53

0 Answers0