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

#1

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

#2

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

0 Likes

#3

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.

0 Likes

#4

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.

0 Likes

#5

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.

0 Likes