Shell Script Sends SMS Messages

Tux with a phone I wanted to teach my Linux home server to send me SMS text messages to alert me of certain events. As usual there's more than one way to skin a cat. Here are some of the ways to achieve this:

  • Use a USB modem.
  • Use an old Nokia phone connected to the server's COM port.
  • Use one of the many Email to SMS services.
  • Use an SMS service on the internet.

The first 2 options require some external hardware, an extra power supply and an active SIM card. Especially the last asset is the problem. You could opt for a full SMS subscription, but then you'll have to pay a monthly fee, even if you don't send a single SMS that month. If you send too many SMSes outside the bundle, you'll have to pay extra per SMS. A pre-paid SIM is also an option, but those tend to expire after half a year. So you'll have to monitor the credit which is still left on the card and the expiring date regularly. For both actions you'll probably have to remove the card from the modem and put it in a normal phone. Also upgrading the credit has to be done with the SIM card inserted in a real phone.

Email2SMS services come and go. Especially the free services which existed a couple of years ago have all gone. And if some still exist they usally limit the number of messages sent per day/week/month, and they usually don't support the full 160 character message size.
Paid Email2SMS services are often relatively expensive or have a very obscure payment system. Especially if you only have to send a couple of text message per month.

Obviously the last option is the one to go for. The only thing needed is a small shell script and a subscription with Voipbuster. This is also a pre-paid system, but your credit won't expire after half a year. And you can simply check and upgrade your current credit from all over the world using a simple web interface.

Sign Up With Voipbuster

Voipbuster signup

In order to send a SMS text message you need a subscription with Voipbuster. After signing up there you'll have to top up your credit, don't worry it will cost you next to nothing. You've got plenty of options to pay them, there's probably at least one of them which will work for you. With this credit you can make phone calls and send SMS text messages almost all over the world, which is just about what we want to do. Text messages to Dutch mobile phones cost about €0.06. Messages to most other countries may vary but are usually also quite affordable.

Windows users can download the Voipbuster soft phone and sign up to get an account using that soft phone. Linux and Mac users can use a special link on the Voipbuster download page to sign-up using the web site only.

The Shell Script

I'm quite new to shell scripting, so probably some things I've done below could be done easier. It works though, and that's what counts doesn't it? You can simply copy/paste the code below, or you can download this tgz file and unpack it to your system.
All you have to do is change the first three variables in the program to match your Voipbuster account. Don't forget to make the file executable using chmod u+x sendsms .

#!/bin/sh

# sendsms
#
# Author: San Bergmans
# www.sbprojects.net / www.oeioei.nl
#
# Sends an SMS to a phone number through Voipbuster.
# Obviously you'll need a Voipbuster account for this to wrok.
# use: sendsms +31612345678 Message to be sent
# Messages longer than 160 are refused by the service. The return message
# will be failure. Therefore the script will truncate your message to 160
# characters for you.
#
# Sending to more than one phone number is not reliable, you'll get a
# success response, you pay for the sent SMSes, but nothing is sent.
# Therefore stick to one phone number at a time.
#
# If you want a Fonera to send an SMS add the option --no-check-certificate
# to the wget command. The fonera doesn't kill the leading tab in the
# result string.

# Change these settings to match your account with www.voipbuster.com

USER="username"
PASS="password"
FROM="from"

guide ()
{
    echo "Usage:   $0 phone_number sms text"
    echo "Example: $0 +31612345678 This text is sent\n"
}

# Parameter $1 is the destination number in international notation
if [ $# -eq 0 ]; then
    echo "No parameters given"
    guide
    exit 1
else
    TO=$1
fi

# The rest of the parameter string is used as SMS text
shift
SMS_TEXT="$*"
SMS_TEXT=`echo $SMS_TEXT|cut -b -160`

if [ "$SMS_TEXT" = "" ]; then
    echo "No text message given"
    guide
    exit 1
fi

# Form the URL, including constants and parameters
URL="https://www.voipbuster.com/myaccount/sendsms.php?username=$USER&password=$PASS&from=$FROM&to=\"$TO\"&text=$SMS_TEXT"

# Send SMS and print the result string from the XML file returned by voipbuster
wget -q "$URL" -O - | grep resultstring | sed -e 's,<resultstring>,,' -e 's,</resultstring>,,' -e 's," ",a,' -e 's,\t,,'

# Example result file from voipbuster
# Indented lines are preseded by a tab

# <?phpxml version "1.0" encoding="utf-8"?>
# <SmsResponse>
# 	<version>1</version>
#	<result>1</result>
#	<resultstring>success</resultstring>
#	<description></description>
#	<endcause></endcause>
# </SmsResponse>

Now you can start sending SMS messages by typing ./sendsms +31612345678 Your message. You may have to replace ./ by the path where sendsms can be found. The phone number must be in international format, after all you can send text messages around the entire world. Following the phone number comes the text message.
A few simple rules apply to this message: The message can't be longer than 160 characters, longer text is truncated to 160 characters for you. And you can't use the characters *, " or \ in your text. Well, you can if you understand how Linux handles these characters. The * for instance will send all file names in the current directory as text, which is probably not what you've intended.

As far as I know there are two possible result messages success and failure. I haven't seen any others being generated so far.