Sending SMS from the command line

Quite a few years ago I started using a VOIP provider called voipcheap.  Aside from the very cheap rates they have for calling (including FREE calls to number of countries), I’ve also found them to be very reliable. Another feature they have is the ability to send SMS’s either via their website or via a direct URL that you can hit with with a few bits of info in the URL and they’ll send out your SMS for you.

So what is this post all about then?  Well, I was looking for a script or something that would interface to voipcheap so I could send out SMS’s from my automation scripts and such.  I didn’t find anything around .. so that left me with no choice but to write my own.   The first run of the script started from a 3 line bash script, as I added more logic and features it’s now around 265 lines.  Here’s the key points of what the script does;

  • Send SMS from single command line by providing the number to send to and the message (in quotes)
  • Send SMS from the command line by providing a “name” of someone in the database
  • Contact database is just a flat file with a name and number in it
  • The script allows the user to list the database, edit the database and add single entries quickly

I’ve decided that after about 6 months of fiddling the script it’s at a level I don’t mind releasing to the wild.  Attached to this post you’ll find a link to a tarball with the script as well as a sample database and config file.  (Continue reading)

The script contains some simple instructions inside so you can configure it, but in a nutshell there are really only 2 variables in the script that should be looked at and they are:

CONFIG=~/.sms-config.db

NAMESDB=~/.sms.db

You’ll see these both default to your home directory, so if you rename the included samples and save them to your home directory then you’re good to go.  You’ll only need to edit the 2 files and you’re ready to go.

The CONFIG file looks like this

VCUSER="YOUR VOIPCHEAP USERNAME"
VCPASS="YOUR VOIPCHEAP PASSWORD"
FROM="YOUR PHONE NUMBER"

Completed it will look something like

VCUSER="fredsmith"
VCPASS="myvoipischeaperthanyourvoip"
FROM="+12125551212"

Those 3 variables can also be configured in the script itself rather then a separate file.  If the file isn’t found then the script will expect to use the ones provided in the script.  I’ve made the script handle either method of getting the user data in case the script is to be used by multiple users on the same system.

The second file is NAMESDB, this is a simple database of names and numbers you might frequently use

#NAME,NUMBER
fred,+12125551212

Commented lines are ignored during parsing, however I’ve not put in logic to handle comments at the end of lines, ie:

#NAME,NUMBER
fred,+12125551212  # My best friend

It really is rather simple but I’ve tried to build in a bunch of logic into the script to make it more flexible.

Using the script

To find out the options, run the script with a  -h or –help, the output will be like:

[rmckenzi@localhost send-sms]$ ./send-sms.sh -h
** ERROR **
There is a problem, the file /home/rmckenzi/.sms.db isn’t available

Usage  : ./send-sms.sh [ options ] (<message in quote>)

options are:
–help  / -h : Display this page
<a name>     : The name of someone in the database
<a number>   : The phone number to text
–add   / -a : Add new name and number to the database
–list  / -l : List names in the names database
–edit  / -e : Calls the editor you provide to edit /home/rmckenzi/.sms.db

Or no options to be prompted for interactive mode

example     : ./send-sms.sh +447700011223 "This is a test"
example     : ./send-sms.sh rob "This is a test"
example     : ./send-sms.sh rob
example     : ./send-sms.sh [ –add | –list | –edit ]

 

You’ll notice the ** ERROR ** line, this will output when the names database is not found.

Adding a number to the database can be completed via the script if you like.  You’ll be asked for a name and number to add, then it’ll be appended to the end of your database.

Editing the database option will prompt you for your favourite editor first, just enter it’s name, ie: pico / vi / vim / etc

That’s really about it, it’s pretty simple to operate.

Things I’d like to add:

– Automatic editor detection, might even add an option to the config file $EDITOR and / or $VISUAL

– Allow users to set the required config variables in .bashrc or whatever

Anyhow, if you’re a user of VoipCheap and want to send SMS’s then check out the script.  I’d love to hear feedback from people that put it to use.  Please direct people to my link and if you modify the script and give it away I’d appreciate if you give me credit for the original work.

Download Script

Thanks for reading.


Comments are closed.