[HOW-TO] Send commands to the router with SMS messages (Asterisk)

In a next future I will post a HOW-TO about installing and full configuring Asterisk13 on an OpenWRT router,
This time it is just a quick HOW-TO about receiving a SMS message with Asterisk (with chan_dongle) and then execute any command found (if any) in the SMS body message.

this is the asterisk dialplan:

nano /etc/asterisk/extensions.conf

[from-dongle]

;
exten => sms,1,Noop(Incoming SMS from ${CALLERID(num)} ${BASE64_DECODE(${SMS_BASE64})}) 
exten => sms,n,System(echo 'From: ${CALLERID(num)} \n${BASE64_DECODE(${SMS_BASE64})}' >> /tmp/sms.txt)
exten => sms,n,Wait(3)
exten => sms,n,System(/bin/sh /root/getsms.sh)
exten => sms,n,Wait(15)
exten => sms,n,System(/bin/rm -f /tmp/sms.txt) 
exten => sms,n,Wait(2)
exten => sms,n,Hangup() 

this is the script launched by asterisk when a SMS is received

nano /root/getsms.sh

#!/bin/sh
#################################################
# GetSMS - script to check for commands 
#################################################

# Variables
folder=/www/sms                
dbsms=$folder/dbsms.txt         # SMS received database
newcmd=$folder/newcmd.txt       # new commands to be executed
dbcmd=$folder/dbcmd.txt         # executed commands database
now=$(date)                     # current date and time

# fill newcmd file with new commands received, if any
/bin/grep XX /tmp/sms.txt > $newcmd

# count the commands received, if any
numcmd=$(grep -c "^" $newcmd)

# if no commands are found, go ahed
if [ $numcmd -eq 0 ]
then
    exit 1
fi

# check for  any new command  
for ancmd in $(cat $newcmd)
do
    case $ancmd
    in
        XXon) /root/on.sh &    # if the body of the message contains "XXon", launch the on.sh script
        echo $ancmd >> $dbcmd;;

        XXoff) /root/off.sh &    # if the body of the message contains "XXoff", launch the off.sh script
        echo $ancmd >> $dbcmd;;

   
*) ;;
    esac
done

# add new sms to database file

/bin/cat /tmp/sms.txt >> $dbsms
echo -n '<p>' >> $dbsms

# wait 2 seconds
/bin/sleep 2

# empty newcmd file
> $newcmd

chmod 755 /root/getsms.sh

nano /etc/asterisk/dongle.conf

[general]

interval=15

[defaults]

context=from-dongle
group=1
rxgain=5
txgain=-3
autodeletesms=yes
resetdongle=yes
u2diag=-1
usecallingpres=yes
callingpres=allowed_passed_screen
disablesms=no
language=en
smsaspdu=yes
mindtmfgap=15
mindtmfduration=40
mindtmfinterval=200
callwaiting=auto
disable=no
initstate=start
exten=+39xxxxxxxxx # put your mobile number here
dtmf=relax

[dongle0]
audio=/dev/ttyUSB1
data=/dev/ttyUSB2
;imei=358109024359102
;imsi=222956701469191

in this exampe, the two commands I configured in the getsms.sh script, are:
XXon and XXoff , XX is the prefix to recognize a command.
Note: do not add anything else to the SMS body, just a single command without any space before or after the command, otherwise the command will not be executed.

You can obviously add more commands to the above script at your choice.

All the SMS received and all the command executed are in two files: /www/sms/dbsms.txt and /www/sms/newcmd.tx, so you can point your browser to http://your-router-ip/sms to easily read them.

2 Likes

You might want to provide some insight on how users can authenticate the message before executing it.

there is no authentication, but you can use commands like: Sd543fgdsn-on or Sd543fgdsn-off and change the prefix "XX" to "Sd543fgdsn-" in the above script, basically the prefix is the password too.

This method permits you to launch commands an a remote router, even without any internet connection, since asterisk works locally (no sip peers in this case) and the GSM dongle does not need internet to receive or send SMS messages :grinning:
I'm also using asterisk+dongle to get SMS warnings from the router when internet connection goes down :wink:

I also tried to use the dongle voice channel to send DMTF tones to control the router, but unfortunately the DMTF recognition of chan_dongle is very buggy and unreliable.

Another system I developped (maybe I will post a How-TO about that) is to get vocal calls from the router with pre-recorded warning voice messages, using baresip + asterisk + chan_dongle + GSM USB dongle, again, even if internet connection is not available to the router.

Just a few tips for Asterisk on OpenWRT routers.

Sometime you need to use audio files .sln you have created for another linux build, this is a simple way to convert .sln files to .gsm:

opkg update
opkg install sox

converting:

sox my_custom_message.sln my_custom_message.gsm

then put your custom files in the following directory:
/usr/share/asterisk/sounds/custom (asterisk 13).
/usr/lib/asterisk/sounds/custom (asterisk 11)

The dialplan (/etc/asterisk/extensions.conf) would be:

exten => 600,1,Answer()
exten => 600,n,Set(CHANNEL(language)=en)
exten => 600,n,Ringing
exten => 600,n,Wait(1)
exten => 600,n,Playback(./custom/please_wait)
exten => 600,n,Dial(SIP/200,30)
exten => 600,n,Wait(1)
exten => 600,n,Playback(./custom/leave_a_message)
exten => 600,n,VoiceMail(240@voicemail)
exten => 600,n,Hangup()

extra: convert wav files to sln:

sox message.wav -t raw -r 8000 -s -2 -c 1 message.sln

A number to call to listen to the current time:

Install asteriskXX-app-sayunixtime and asteriskXX-sounds for your asterisk release

opkg update
opkg install zoneinfo-core
opkg install zoneinfo-europe
; exact time now - call 161
exten => 161,1,Set(CHANNEL(language)=en)
exten => 161,n,SayUnixTime(,CET,AkMbdY)
exten => 161,n,Wait(2)
exten => 161,n,Hangup()

note: you may want to change zoneinfo-europe and CET (Central European Time) to your timezone

Another interesting functionality to add to to our OpenWRT PBX:

Chan Spy : listen in on a call, or whisper into a conversation.

If you have not installed it previously, you need to install app_chanspy module:

opkg update
opkg install asteriskXX-app-chanspy

example:
opkg install asterisk13-app-chanspy
depending on what version of Asterisk you have installed on your router.

Also, you need at least "beep.gsm" audio file in the asterisk sounds directory (see above).

Then simply add the following lines at the end of the file "/etc/asterisk/extensions.conf":

[from-internal]

Chanspy Scanning
exten => 555,1,Chanspy(all,b)

save and restart Asterisk.

Now by dialing 555 from another extension you can listen in on a call.

While spying, the following actions may be performed:

Dialing # cycles the volume level.
Dialing * will stop spying and look for another channel to spy on.
Dialing a series of digits followed by # builds a channel name to append to
(e.g. run ChanSpy(Agent) and dial 1234# while spying to jump to channel Agent/1234)

Another nice feature:

Make a phone call to your router and execute a system command (Linux shell) inside OpenWRT

If you have not installed it previously, you need to install app_system module:

opkg update
opkg install asteriskXX-app-system

example:
opkg install asterisk13-app-system
depending on what version of Asterisk you have installed on your router.

example line for the diaplan:

exten => s,n,System(/path/command)

Details:
System(command) - System command alone
System(command arg1 arg2 etc) - Pass in some arguments
System(command|args) - Use the standard asterisk syntax to pass in arguments