Automate Everything w/ Bash, Linux & Command Line
  1. Fun with TextBelt - Public SMS API

    The first time I heard about TextBelt was months and months ago. I tried it out right away and it worked very well. At the time I didn’t really have an excuse to build a tool around it. In fact, I still don’t but I had a few ideas and just happen to think this kind of thing is fun.

    So, let’s first take a look at how to use it from the Linux command line.

    How to use TextBelt

    The text belt has a really clear tutorial that should get you started. All you really need to know is the following command:

    curl "http://textbelt.com/text" -d number=1234567890 -d "message=this is where you put the body of the sms"
    

    That’s it. Once you successfully send your text message you should get back the following response in your terminal:

    {"success":true}
    

    If you get that message back you should receive the sms very shortly. I did determine that sending these to Google Voice accounts does not work. I’m not sure why and I didn’t really look into it. My guess is that Google Voice silently drops the request because TextBelt reports that the message was successfully sent. You can also get error messages, though I’ve only received one and it is as follows:

    {"success":false,"message":"Exceeded quota for this phone number."}
    

    TextBelt does have some pretty tight quotas. You cannot send more than three messages to any one phone number in a three minute period. You are also limited to 75 outgoing messages from any IP address per day. It does say on their site that you can request increased limits if you have a real need for them.

    Interesting Ways to use TextBelt

    Now that you know how to use TextBelt, lets look at some of the interesting ways to use it.

    Watch a log file for changes, SMS yourself the updates

    You may or may not know about the tool called tail. Basically tail is a command line tool that writes the last set of lines to the command line. Here’s the most basic way to use it:

    tail logfile.txt
    

    When you run tail like this it will exit after printing the last ten lines. You can run it with the -f flag or run the tailf program and then it will stay open and print out each of lines added to the end of the log file as soon as they’re added.

    tail -f logfile.txt
    

    So, let’s combine our new-found skills. With some help of the program xargs and this Commandlinefu page, I was able to come up with this little gem:

    tail -f file.txt |xargs -IX curl "http://textbelt.com/text" -d number=1234567890 -d "message=X"
    

    Instead of printing the newest items out to the command line, they’ll get sent to your phone number. Pretty cool right? Depending on what type of a file you’re monitoring, you may not want to get every one of the messages sent to your phone. It’s easy enough to filter the messages from the log update by using grep.

    Here’s a real world example that would send you a SMS each time someone logs into a server using SSH:

    tail -f /var/log/auth.log | grep "sshd.*Accepted" |xargs -IX curl "http://textbelt.com/text" -d number=1234567890 -d "message=X"
    

    You could modify the grep expression to send the failed log on attempts as well.

    Send Facebook’s falling stock price every 30 minutes

    Maybe you have a similar disdain for Facebook as me and it brings you a small amount of joy each time you see something bad happen to Facebook. It’s easy enough to scrape Yahoo Finance using curl to send the current stock price for any stock through TextBelt. This is the quick example using ‘FB’:

     curl -s 'http://download.finance.yahoo.com/d/quotes.csv?s=fb&f=l1'|xargs -IX curl "http://textbelt.com/text" -d number=1234567890 -d "message=X"
    

    If you don’t enjoy watching Facebook’s stock fall then you can change it to what ever stock you want by replacing the fb in s=fb with the stock of your choosing.

    That’ll just send it once and exit. If you want it to be sent every 30 minutes then you have two options. You can set up the command as a cron job or you can execute the command inside a while true loop and ask it to sleep for 1800 seconds before re-running the command. Here’s an example of the while loop:

    while true; do curl -s 'http://download.finance.yahoo.com/d/quotes.csv?s=fb&f=l1'|xargs -IX curl "http://textbelt.com/text" -d number=1234567890 -d "message=X"; sleep 1800; done;
    

    That’s all I have for now. I’d love to see some additional uses in the comments. Happy automation!