Irrational Exuberance!

How To Remotely Admin Servers Via Twitter

March 22, 2009. Filed under python

It's been something of a reoccurring dream of mine to control my server remotely from my cellphone. Sure, I could grab an SSH application and do it the right way, but I decided it would be a bit more fun to send direct messages to the server via Twitter.

Without pretense about the value of doing so, here is a quick overview of how you too can control your servers remotely via Twitter.

Installation

Installation relies on the Python-Twitter library and the barebones python-twitter-remote library.

svn checkout http://python-twitter.googlecode.com/svn/trunk/ python-twitter
cd python-twitter
python setup.py install
cd ../
git clone git://github.com/lethain/python-twitter-remote.git
cd python-twitter-remote

Setup & Configuration

The first step is to create a folder where you store your remote setup.

mkdir ~/remote
cd ~/remote

The next step is to create a rules.json file that specifies your rules. The format of the file is as follows:

[
  {
    "users":   ["lethain","lukeman"],
    "pattern": "update monocle studios",
    "rewrite": "/home/will/scripts/update_monocle_studios.sh"
  },
  {
    "users": ["lethain"],
    "pattern": "reboot apache",
    "rewrite": "/usr/sbin/apache2ctl graceful"
  }
]

users is the list of users who are allowed to perform a given command, pattern is the format the direct message must take (at the moment this is a literal match, but it would be pretty quick to fire it up to match via regular expressions), and rewrite is the command executed when pattern matches.

The final step is to setup a cronjob to periodically run the script. Twitter historically doesn't deal well with overly frequent polling, so I'd recommend setting up the script to poll two or three times an hour.

15,45 * * * * /usr/bin/python /home/USERNAME/libs/python-twitter-remote/remote.py TWITTER-UN TWITTER-PWD /home/USERNAME/remote

Each time a command is executed by the script it will record the username, input text and output command into remote/remote.log.

Sending Commands

Finally you just need to start direct messaging commands to the listening account.

d my_account reboot apache

And, whenever your cronjob runs next, your commands will be executed.

Why?

There are two usecases where I'm actually using this. Both could be better done with different technology (like, ya know, email), but I enjoyed taking a slightly round-a-bout route.

The first is updating sites and rebooting Apache from my cellphone without actually needing to SSH into the box (authentication is by public key only, not password), and the second is allowing friends to update projects hosted on my server (usually by pulling from a Git repository and running a build script) without needing to setup an account and public keys for them.

Like I mentioned above, I certainly think that using email would be a superior approach to depending on Twitter, and I certainly consider it to be a novelty at best.