Remote UNIX Execution via a Cell Phone

by Muskrat

Last night I was sending myself picture messages from my cell phone.  I never cease to be impressed by the speed at which information travels, even when sent from a tiny cellular phone.

I was also home on spring break and I left my computer at my dorm, confident that remote access would be sufficient.  So I've been playing with the notion of remotely running commands in an unconventional way.

Naturally I wondered if it would be possible to run commands on my Linux box via my cell phone (from 100 miles away).  I fumbled this idea around in my head and decided that the only way to do it would be using text messaging.

Up until this point I had been sending photo messages to my Gmail account where I could retrieve them and save them manually.  I needed a way to either:

  1. Retrieve messages from Gmail automatically.
  2. Send messages "directly" to my machine.

Since Gmail requires authentication, I decided to go with No. 2 (because setting up an automated authentication procedure would be much more complicated than the alternative).

I didn't have my system set up as a mail server, so naturally I needed to do that first.

installed the packages for Sendmail, Procmail, Pine, etc. to make sure I had everything I needed.

I also read a little bit about the mail delivery process to understand the basics of what was happening.

After everything was installed (which was trivial) and activated (i.e., editing inetd.conf, starting up Sendmail), I tested to see if my system could actually send/receive mail.

I sent a message from Gmail to my domain and then checked Pine.  Sure enough, the message had arrived.

At that point I needed a way to test for a certain string in the new message and perform an action based on that string.

Email (at least for me) was stored in the file /var/spool/mail/muskrat.  The file contains the message headers (which contains information such as date, sender, subject, status, and so on) and then obviously the bodies of the messages.  So I knew where the information was stored; I just needed a way to pull the desired information from it.

This is where three standard UNIX tools come into play: cat, grep, and awk.  Hopefully, everyone is familiar with these tools.

After a little bit of playing around with various possibilities, I decided the best way to execute would be the following string:

$ `cat /var/spool/mail/muskrat | grep command | awk '{print $3}'`

The breakdown of the command is this: cat will read the mail file and pipe it to grep.

grep searches for the string command and pipes the line containing it to awk.

awk takes the string and prints only the third ($3) field in the line.

So if the line was command test who, awk would only return: who

Finally, the ` (back tics) and ` around the command indicate to the shell to execute the command resulting from whatever is within `` .

So in this case the shell would execute: who

The most efficient way to avoid screwups I found was to use the Subject: part of the header to specify the command.  When using the body of the message, I ran into problems because the phone automatically uses HTML messages.

Now that we can send a command to our machine, we have to be waiting for it.

The best way to do this would be a shell script which executes that command we came up with in a loop.

#!/bin/sh
# checkmail.sh
until [ 1 -eq 2 ]; do
  `cat /var/spool/mail/muskrat | grep command | awk '{print $3}'`
  sleep 2
done

So, "until 1 equals 2," execute the command, sleep for two seconds, and then execute again.

This is an infinite loop because 1 never equals 2.

So fire up this shell command using sh checkmail.sh as root (so you can execute commands like reboot), and then go to your cell phone.

Send a picture message (or a text message if you are able to), and specify the Subject as: command reboot

Send the message.

If you set up everything properly, your system will broadcast a message and reboot.

# sh checkmail.sh   <-- system waits until the message arrives
Broadcast message from root (pts/n)
(Thu Mar 17 17:48:03 2005):

The system is going down for reboot NOW!

I recommend being careful with this for obvious reasons!  Hopefully you learned something new like me.

Thanks to peopLe in ##Linux and ##SLackware for some suggestions.  Shoutouts to my boys.

Code: checkmail.sh

Return to $2600 Index