NSLU2 & SMS With the Orange developer APIs

I was hunting around the internet for a way to get my slug to text me via SMS when things happen and stumbled across the orange developer APIs and was pleasantly surprised!  I was hoping for a simple gateway to send SMS messages from so that I could get a text alerts if/when things go wrong and I found a whole lot more

“The orange SMS API allows you to send and receive text messages within your web application.” is the description from the orange website

The API not only allows you to send texts, but also to receive them.  This sounds a little wierd but it’s really simply, so I’ll explain with the example I built!

First you need to be an orange customer and you have to register for the developer website.   Once you’ve done that, you need to get your access code – a unique number much like the google api codes.  Once you’ve got it, the next thing to do is add a prefix.  This is what your message should start with.  The prefix you choose might have gone, so you may have to try a few.
To send a text, basically, you issue a call to the following, passing your access key, the destination mobile, the text, and which number its coming from.


http://sms.alpha.orange-api.net/sms/sendSMS.xml?id=[access key]&to=>[mobile number]&content=[your text message]&from=[mobile number]


To receive a text, when you register your prefix, you get the option to select either an email address or a URL.  If you pick an email address, then that email address will receive a copy of the text.  If it’s a URL, then the message will be delivered to the specified URL with information added as URL parameters.  The parameters are:

Name Description
api This is the name of the API function – receivesms.
content This is the content of the text message, limited to 160 characters.
from This is the origin of the text message in international format. This is either a phone number or an alias.
to This is the destination number of the SMS

So an example URL would be something like:

http://www.mydomain.com/mypage.php?api=receivesms&content=hi+how+are+you&from=33123456789&to=20345


This is all documented in great depth on the orange developer website so I won’t go into this too much.
The next step was making this work on my NSLU2

I have the lighttp  webserver with FCGI installed via IPKG running on my NSLU.  This runs on port 8081 by default.  In my router, I’ve port forwarded port 80 to 8081 on my NSLU2 to open lighttp up to the internet.

The next thing to do was to create a PHP page to recieve a text and do something with it.  My first idea to test this was to use a shell script I have on the NSLU2.  This shell script simply scans the var/log/messages file to find when the last successful login was today and how many unsuccessful logins there have been today.

The shell script looks like this:

lastLogin=`cat /var/log/messages | grep "sshd" | grep "Accepted publickey for" | tail -2 | head -1`<br />

numberInvalids=`cat /var/log/messages | grep "sshd" | grep "Invalid user" | wc -l`
dateStr=${lastLogin:0:16}
echo 'Last successful login occurred on:' $dateStr' - Number of invalid user attempts: '$numberInvalids''

So my aim – to send a text to the NSLU2, and get back this message from my shell script.  I created a PHP file like the following:

$content = $_GET['content'];
$from = $_GET['from'];
$fid = fopen('~xxxxxxxxxxxxxxxxxxxx/SMSdatabase.txt','a');
fwrite($fid,"$from $contentn");
fclose($fid);
$toexec="~xxxxxxxxxxxxxxxxxxxx/commands/lastloginsms.sh" . $program;
$exec=exec($toexec);


$returnmessage = $exec;

$phone_number = urlencode($from);
$text_message = urlencode($returnmessage);
$url = "http://sms.alpha.orange-api.net/sms/sendSMS.xml?id=XXXXXXXXXXX&amp;to=$phone_number&amp;content=$text_message";
$response = file_get_contents($url);

$xml = simplexml_load_string($response);
echo "Status: ",$xml-&gt;status-&gt;status_msg;

This does a few things – firstly it gets the URL parameters, then writes out the message to a text file so I can keep a track of what messages were received.

Next, it calls the shell script lasloginsms.sh.

Finally, it takes the output of the shell script, and sends that as an SMS back to the sender!

So, to make it all work now, I simply text (from my orange phone)

prefix test

to: 967482

And a few seconds later I get a text back with a response similar to:

Last successful login occurred on: Sep 5 09:46:01  - Number of invalid user attempts: 7

Pretty cool.

You can have 5 prefixs on the devloper website, so the next step is to create a few different PHP scripts that work with the prefixs to stop/start services on the NSLU2 via SMS and provide status reports.  A good example of use would be, should I ever lock myself out of the SSH server (by denyhosts), then I could SMS my slug to enable telnet, log in, and remove the entry from the hosts.deny file!

I’ll post when I get this working!

Java Compiler & Java Virtual Machine (JVM) on the NSLU2

Being a java developer I was intrigued with the idea of getting a JVM running on my NSLU2, so I looked into how to do this.

Surprisingly it was pretty easy.

To start with you need to install the following packages with ipkg:

ipkg install classpath
ipkg install jamvm
ipkg install jikes
ipkg install zlib
ipkg install file

Once these have installed then you are almost ready to go.  Being a java developer, I was used to using javac and java to compile and execute programs, so jikes and jamvm didn’t sit well with me!  To get around this I created a symbolic link and a shell script in /opt/bin

These are as follows:

/opt/bin/java

 ln -s /opt/bin/jamvm /opt/bin/java

/opt/bin/javac

/opt/bin/jikes -classpath /opt/share/jamvm/classes.zip:/opt/share/classpath/glibj.zip $*

Remember to change the permissions on the shells scripts to add the execute flag!

Having  done all that, I can now create a java file:

HelloWorld.java

class HelloWorld
{
     public static void main(String[] args)
     {
          System.out.println("Pembo's Hello World!");
          System.out.println("Args Length: " + args.length);
          for(int i=0;i<args.length;i++)
          {
               System.out.println("[" + i + "] - " + args[i]);
          }
     }
}

compile this using the command

javac HelloWorld.java

and then run this using the command

java HelloWord test1 test2 test3

And I see the following output:

Pembo's Hello World!
Args Length: 4
[0] - test1
[1] - test2
[2] - test3

JamVM is excellent from what I’ve seen so far and is perfect for Java NSLU2 development.
I’ve yet to run anything overly complex on the Slug so I can’t comment on the memory utilisation.

It takes around a second to compile the above HelloWorld program on the NSLU2 and under a second to run it.

IPKG Web

IPKG Web isan installable package for the NSLU2 that can be installed through ipkg using the following command (as root)

ipkg install ipkg-web

This is a web front end for IPKG to make it easier to install new packages, upgrade packages and delete a package which you might have installed.

It makes it so much simpler to see exactly what packages you have installed with IPKG and to see what else is available!

Once installed, simply connect to: http://<ip address of slug>/Management/package.cgi  and from there it is very straight forward!

Click to see Full sized

Click the image to see it actual sized.

NSLU2 Ftp Client – Yet Another FTP Client (YAFC)

So – I’ve been using FTP on my NSLU2 for a while, however when the connection drops, the FTP application seems to hang, plus its a real pain to resume a file once it has hung!

To work around this, I’ve downloaded YAFC (http://yafc.sourceforge.net/ ) and compiled this on the NSLU2.
It works a treat – simple download the source, use the configure, and then make and it works as you’d hope.

Resuming is really easy – just get the file and press R to resume when it realises the file exists.

htop – An interactive process viewer on the NSLU2

I recommend htop for any Linksys NSLU2 owner whos ‘unlsung their slug’.

htop is a package that can be installed through ipkg using the following command (as root)

ipkg install htop

Once installed, just type ‘htop’ to run the process viewer.

It’s a curses based process viewer that lets you see, sort and kill processes running on the NSLU2.

Click to see Full sized

Click the image to see it actual sized.