Skip navigation

Thanks to the good people over at stackoverflow, I have discovered how to tweet from my linux server command line!  I know there are probably lots of ways to do this by installing utilities etc. but I did it manually so I could understand how it worked, not just so that I could do it!  I already have TweetDeck on my iPhone so I don’t particularly need another way to tweet, but this is an intermediary step towards getting my server to tweet alerts.  I’ll write another post when I’ve figured that one out but here’s the basics of tweeting from the bash prompt.  Enjoy!

So, first things first, you need cURL which formats the http request to the Twitter API.  To install on Ubuntu (will probably work for most Debian systems) type the following into the command prompt:

sudo apt-get install curl

Once the installation is complete, you need to create a shell function, I won’t cover the how’s and whys here but you can find out for yourself.  Below are the contents of a file I named tweet.sh and placed in the directory /home/conor/shell on my linux server.

#!/bin/bash
function tweet {
curl -o /dev/null -s -u username:password -d status="$*" http://twitter.com/statuses/update.xml
}

The twitter URL should be on the same line as the cURL command above, username and password are obviously your twitter username and password. The switches -o and -s redirect the response and hide the status meter, respectively. You can leave them out to see their effect if you like.

So, now you need to be able to trigger the function. Edit your .bashrc file in your home directory and add a line near the end like the one below

source shell/tweet.sh

You should already have other ‘source’ lines in there so you can add it under these. This loads the ‘tweet’ command at login time, to load it now you can just type source ~shell/tweet.sh and that’s it, you’re done! Now to tweet you can just type the following at the bash command line:

tweet I'm tweeting from the command line!

Check your twitter page and your status should be updated, if it isn’t there immediately, wait a moment. Sometimes Twitter is slooooow!

Hope you enjoyed this little tutorial, and I’ll be back when I’ve figured out how to make my server tweet by itself, till next time…

Advertisement

One Trackback/Pingback

  1. [...] , será lo que intentará publicar curl (se truncará si pasa de los 140 caracteres). Sería interesante reescribir la función de modo que podamos especificar más opciones como antes, como el dent y usuario al que estamos respondiendo pero eso rebasa la Idea original de la función de bash [...]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.