0

I have a script that I'm going to use for our FreePBX with Asterisk phone system. I would like to know if any of our extensions/endpoints are offline/unavailable, I would like to be sent that info in an email.

#!/bin/bash

email="my-email@my-domain.com"

offlineExtensions=$(asterisk -rx 'pjsip show contacts' | grep -i "unavail")

if [ "$offlineExtensions" ]
then
echo $offlineExtensions | mail -s "Extensions Offline" $email
fi

My email looks like...

Contact: 7101/sip:7101@IP_ADDRESS:5060 d20a378e88 Unavail 55.829 Contact: 7770/sip:7770@IP_ADDRESS:5060 174a796f10 Unavail 12.275

I would like the contacts info to be on their own separate line, not concatenated together. I've used the next line symbol(\n) in the script but it doesn't seem to work.

Any ideas, please?

2

Your contacts get concatenated because you didn't quote $offlineExtensions in echo $offlineExtensions | .... The variable has the newlines intact, but then the shell splits it into words, then joins the words separated by spaces. To prevent that you need to enclose $offlineExtensions in double quotes. For the same reason it's good hygiene to also quote $email (although leaving it unquoted is harmless in this particular situation).

An unrelated problem lurking in your script is that you're using echo to print a variable with unknown contents. If $offlineExtensions happens to start with a dash -, echo will take that as an option. Some implementations of echo allow you to avoid that problem by using echo -- instead of just echo, others don't. The portable way to deal with it is to use printf instead of echo.

Summarizing, replace the echo ... line by:

printf '%s\n' "$offlineExtensions" | mail -s 'Extensions Offline' "$email"
0
0

You should try use

pjsip show contact concise

But that is not best option anyway.

Best practice is parse /var/log/asterisk/messages or full file(it can be setuped to put record when someone go unreachable/reachable).

Best practice is use nagios or other monitoring soft, not write your own.

-1

My final script that works. Thanks.

#!/bin/bash

email="my-email@my-domain.com"

offlineExtensions=$(asterisk -rx 'pjsip show contacts' | grep -i "unavail")

if [ "$offlineExtensions" ]
then
printf "$offlineExtensions" | mail -s 'Extensions Offline' "$email"
fi
3
  • 1
    No, that's definitely not how to use printf! If the variable contains a % character, this will be interpreted as a formatting string. See Satō Katsura's answer again. And accept that answer if it solves your issue.
    – Kusalananda
    Oct 26 '17 at 11:00
  • I've tested this script and it works the way I want it too. Could you tell me please what you think is wrong with it? Thank you.
    – Marc
    Oct 26 '17 at 12:56
  • I believe I did. printf should always be used with a formatting string, as in Sato's answer.
    – Kusalananda
    Oct 26 '17 at 13:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.