To start with, I bought a Huawei USB (E160G) stick through Ebay. Doesn't cost much, especially when they come from China. Keep in mind some of them can be simlocked. Not a real big problem, you can get them unlocked yourself, but it will cost extra.
Next I bought a cheap prepaid SIM from Vodafone. They're usually pretty cheap, a few euro's. Sending sms's is something like 9ct's per SMS, and you usually get some extra credits when registering the card on the Vodafone website :). Of course any other provider will do.
When inserting the dongle into the computer you get 3 devices:
Depending on which other USB tty's are present, the numbering can change. So I added rules to udev to make sure I can reference the Huawei on the same devices names. I added the following lines to /etc/udev/rules.d/local.rules:
Next, I installed smstools on my server.
Configuration is done through /etc/smsd.conf. After some testing, this setup seems to work nicely with my Huawei USB stick.
[huawei] init = AT+CPMS="MT","MT","MT" device = /dev/huawei0 incoming = yes pin = < pincode > baudrate = 19200 eventhandler = /usr/local/bin/sms.sh memory_start = 0 mode = new
The important bit in the configuration is the eventhandler. This hook gives you the option to process events from smsd. When an SMS is received, it can be processed by a script. I made a first initial version of the script to be able to run commands remotely. Currently authentication is a bit too open, but can easily be improved by adding an extra password or using a token. The current script looks like this.
AUTHGSM=< authorized GSM number, eg. 3112345678 > EMAIL=< email address > TMPFILE=/tmp/sms.$$ TMPSCRIPT=/tmp/sms.sh
action=$1 filename=$2 messageid=$3
source /etc/profile
case $action in "RECEIVED") if test "`grep \"^From: $AUTHGSM$\" $filename`" != "" then eval `cat $filename | awk 'BEGIN { found=0 } { if (found == 2 ) { printf("ARGUMENTS=\"%s\"\n",tolower($0)); found++ } if (found == 1 ) { printf("CATEGORY=\"%s\"\n", tolower($0)); found++ } } /^$/ { found=1; }'`
case $CATEGORY in "cmd") echo "To: $AUTHGSM" > $TMPFILE echo >> $TMPFILE echo "$ARGUMENTS" >> $TMPFILE echo "#!/bin/bash" > $TMPSCRIPT echo "source /etc/profile" >> $TMPSCRIPT echo "$ARGUMENTS" >> $TMPSCRIPT chmod 755 $TMPSCRIPT sudo $TMPSCRIPT >> $TMPFILE 2>&1 rm -f $TMPSCRIPT #( eval "source /etc/profile ; $ARGUMENTS" ) >> $TMPFILE 2>&1 mv $TMPFILE /var/spool/sms/outgoing ;; esac fi mail -s "Incoming sms" $EMAIL < $filename ;; "SENT") mail -s "Outgoing sms" $EMAIL < $filename ;; "FAILED") mail -s "Failed sms" $EMAIL < $filename ;; "REPORT") mail -s "Report sms" $EMAIL < $filename ;; esac
For all events an email is sent to the defined email address, so when something happens you automatically get an email.
When an SMS is received from the mobile number defined in AUTHGSM, the message is inspected for a command. If the first line of the message is the word "cmd", the next line is interpreted as the command that needs to be run. The command is put in a shellscript and exectued as root through sudo. If you don't feel comfortable doing this, or it isn't nessacary you can remove the sudo part. Adding a password or other authentication is relativly simple, just add an extra option to the case.
So a practical example looks like this. Send the following sms:
The SMS is recieved and processed:
As you can see a reply SMS is sent back to the same number. And the content looks like:
As you can see the first line of the SMS had the executed command repeated. This takes op some room in the SMS, so it can also be removed.
For added robustness I added smstools the my monit configuration. A usefull way to check if smstools is still functioning seems to be to check the file:
It is continuously updated by smstools. I added the following to my monit configuration:
So if there have been no updates for 2 minutes, smstools gets restarted.