Email not working with bash script

0

I am trying to monitor our voip sip trunk carrier if it becomes unavailable i get an email from script below, the command in the script is working fine create log file in /tmp/checkpeers but script is not sending out email i have tried many options as they can be seen in the script below, i have also tested mail delivery agent is enable and i have sent out some test emails using email -s "test" me@me.com it works, can someone please help out with this.

#!/bin/sh

# Check for Offline SIP Peers
#peername=vitel-inbound2/kdc_gatine
rm -f /tmp/checkPeers
#/usr/sbin/asterisk -rx 'sip show peers' | grep UNKNOWN >/tmp/checkPeers
#asterisk -rx "sip show peers" | grep vitel-inbound2/kdc_gatine | grep -v OK

asterisk -rx "sip show peers" | grep vitel-inbound2/kdc_gatine | grep -v OK >/tmp/checkpeers

if [ -s "/tmp/checkPeers" ]; then
mail -s "Vitelity Inbound SIP Connection OffLine please Check" me@me.com < /tmp/checkpeers
#[EMAIL="me@me.com"]me@me.com[/EMAIL] </tmp/checkpeers

#SUBJECT="Vitelity Inbound SIP Connection OffLine please Check"
#EMAILID="me@me.com" </tmp/checkPeers
#$SUBJECT
#$EMAILID

fi

user313162

Posted 2018-02-22T18:29:43.687

Reputation: 25

Are you able to send an email from the command line using 'mail -s'? – Tyson – 2018-02-22T18:34:54.550

Yes it works with mail -s no problem – user313162 – 2018-02-22T18:37:09.063

How is the script being run? Note that the asterisk line will create /tmp/checkpeers (which may be empty and will fail a -s test), but you are removing and later testing for /tmp/checkPeers, which is a different file, though the mail command takes input from the lower-case file. – AFH – 2018-02-22T19:25:40.457

even if i comment rm -f /tmp/check.. it still does't sent out email, and i am running script manually sh monitorsip.sh, also the script has 755 permissions – user313162 – 2018-02-22T19:28:04.517

Answers

1

My guess is that the file you are creating doesn't match the file you're testing. These are the lines I'm referring to.

asterisk -rx "sip show peers" | grep vitel-inbound2/kdc_gatine | grep -v OK >/tmp/checkpeers
if [ -s "/tmp/checkPeers" ]; then

in the first you are creating the file as /tmp/checkpeers, but in the second, you are testing against /tmp/checkPeers. Notice the difference in the lowercase p and uppercase P? Since files in Linux are case-sensitive, these need to be the same. Make sure the filenames match here and in other places in your script.

Good luck.

virtex

Posted 2018-02-22T18:29:43.687

Reputation: 1 129

That was it boss you got it thank you very much... – user313162 – 2018-02-22T20:19:06.773

0

Further to @virtex's observation, I would avoid temp files altogether

data=$(asterisk -rx "sip show peers" | grep vitel-inbound2/kdc_gatine | grep -v OK)

if [ -n "$data" ]; then
    subject="Vitelity Inbound SIP Connection OffLine please Check"
    recipients="me@me.com"

    echo "$data" | mail -s "$subject" "$recipients"
fi

If you're OK with temp files, then DRY and use a variable to hold the filename:

tmp=$(mktemp -t checkPeers.XXXX)
trap "rm -f $tmp" EXIT            # remove when script exits

asterisk -rx "sip show peers" | grep vitel-inbound2/kdc_gatine | grep -v OK >"$tmp"

if [ -s "$tmp" ]; then
    mail -s "Vitelity Inbound SIP Connection OffLine please Check" me@me.com <"$tmp"
fi

glenn jackman

Posted 2018-02-22T18:29:43.687

Reputation: 18 546