Social Enterprise

Tweeting from the command line

Vincent Danen shows you a simple script that will allow you to send tweets from the command line. Whether you're sharing your personal thoughts or project announcements, the script gives you the convenience of automation.

Twitter is one of the biggest and fastest-growing social media sites currently online. If you're not already "tweeting," you should know that Twitter is a micro-blog: short little posts, called tweets, allow you to share what you are doing or thinking with others. The posts are 140 characters at a maximum, and you can follow other people on Twitter, as well as have other people follow you.

A number of companies and projects are starting to use Twitter as a means of making announcements, which means that you can follow projects or organizations that interest you to see what is going on with them. Twitter is a Web-based service, so you log in to tweet and to look at the folks you are following. Through its available API, however, you can use other clients to send your tweets and read Twitter.

If you are more interested in sharing your thoughts or project announcements than in reading other people's tweets, there is a very easy way to accomplish this: using the command-line.

The script you will see takes exactly one argument: your tweet. You use it like this:

$ ~/bin/tweet "Writing my TechMails"
Successful tweet!

Once you run the script and look on Twitter, your post is there for all to see. The script itself uses nothing fancier than cURL to post the text provided to the script. The text must observe shell constraints: it must be properly escaped for special characters (such as "!" and "?"). The script doesn't require the tweet to be in quotes; you could use:

$ ~/bin/tweet Does my tweeter need quotes\?
Successful tweet!

and it would work just as well.

The script itself:

#!/bin/sh
tweet="${@}"
user="username"
pass="sekret"
if [ $(echo "${tweet}" | wc -c) -gt 140 ]; then
    echo "FATAL: The tweet is longer than 140 characters!"
    exit 1
fi
curl -k -u ${user}:${pass} -d status="${tweet}" https://twitter.com/statuses/update.xml >/dev/null 2>&1
if [ "$?" == "0" ]; then
    echo "Successful tweet!"
fi

Output of the cURL command is directed to /dev/null because it returns some XML that we don't need to care about. The script also makes sure that the tweet is 140 characters or less, and exits with an error if it is longer.

The applications for this very simple script can be quite interesting. You could hook this into a subversion repository to notify of new commits to your project or you could put together a script to take the title of a new regular blog post and turn it into a tweet using a URL shortening service to generate a link -- there are a number of ways to automate interesting things with this script.

Get the PDF version of this tip here.

About

Vincent Danen works on the Red Hat Security Response Team and lives in Canada. He has been writing about and developing on Linux for over 10 years and is a veteran Mac user.

35 comments
dennyboy
dennyboy

Worked great from an OpenBSD 4.4 box!

morourke
morourke

As Bourne scripts go this one has woopsies, errors and just absurd uses of quotes. And yes, cygwin would be your only hope of running this on Windows

dshepherd
dshepherd

I've been using a similar script for a while. I set up a twitter account and protected the tweets (so I control who can see it). I have an hourly cron job that tweets uptime and mail queue counts from a bunch of linux boxes. Very helpful.

csmith.kaze
csmith.kaze

I swear. I have like five emails from Tech Rep and four of them are about Twitter. I, for one, don't use twitter. I don't see the point in it and I don't really want you to know what i am doing at all times, nor vice versa. And it seems most people in my age group (early twenties) and younger don't seem to get it either. It may be that it is just me.

b4d93r
b4d93r

Very cool info! Thank you for sharing this. However I was wondering what about the Windows users, is there a similar option for them as well?

corePro
corePro

That's pretty cool. you don't have that script opensource do you?

The 'G-Man.'
The 'G-Man.'

What about all those twitter admins that can see your data. Once it is out the door.....well you know.

csmith.kaze
csmith.kaze

why not just use email or something? I mean it is cool that you set this up, it just seems that it would have been easier to have it send to an email address, then you don't even have to worry about who has rights to it since (hopefully) only you have the password.

DT2
DT2

I never thought of using it as a sysstat notification tool. The possibilities in this vein are endless. Very cool!

pkvelins
pkvelins

I am fine with tweeter as long as people do not attempt to convince me that I must spend time tweeting and ?following? in order to remain in 21-st century. I think it might be useful for PR or for people that do not know other means of communicating their agenda.

CharlieSpencer
CharlieSpencer

"And it seems most people in my age group (early twenties) and younger don't seem to get it either." Your generation is the one all hot for Twitter, according to what the media keeps telling us old fogeys (50 and over). If we're not using it, and you're not using, then where are all these Twits coming from???

Thrash Cardiom
Thrash Cardiom

So if you don't see the point, don't use it; and even if you do, you aren't required to constantly tell people what you are up to. However others find it useful. I get quite a few links to interesting tech and other articles through twitter. My main employer uses it to inform its clientbase about various subjects. It can be a good and useful tool even if 50% of it is dross - but you don't have to read those either.

amabilis
amabilis

Think a little bit more abstract, not only persons tweet (now). 6 backup programs from 6 branch offices across europe tweet if they have finished succesful (meaning the server is still up and the substitute has fed the tape drive) - while the admin (me) is sunbathing and "dolce vita"-ing on an italian beach. Quite cool...

altax
altax

perphaps its a good piece of information...

csmith.kaze
csmith.kaze

most windows users I know are terrified of the command line, why would they even use this? Put like a previous poster said, cygwin might cut it.

vdanen
vdanen

I suppose you could install Cygwin and do the same thing on Windows... or convert that into a python script to run on Windows.

CharlieSpencer
CharlieSpencer

but Veritas Back-up Exec has e-mail capability built in. I realize not everyone runs an enterprise-grade backup solution, but since e-mail's been around in some form for most of two decades, I can't believe most utilities don't already have e-mail capability. Nothing wrong with using Twitter to do this, but you may be ignoring a capability you already have.

The 'G-Man.'
The 'G-Man.'

thats the issue. Little do they know that it is just a glorified text messaging system.

csmith.kaze
csmith.kaze

is that if i don't like it should just sit down and shut up? nothing like promoting diverse opinions, bucko.

csmith.kaze
csmith.kaze

Ours sends an email to a group account that everyone who needs it can read it. How is my way any worse? At least this way noone but those who need to know it is done will know. And all the while I am sitting on a (non-disclosed) beach sipping a strong drink with my iphone. Not only that, but my SNMP monitor sends a text message to me with the info once the event has tripped it. Same principle, minus the telling the world that your server just backed up something properly (unless they have come out with a federation style Twitter, and even then I still have an "as easy" solution) I just don't see the point of twitter at all, for pretty much any reason (now I have seen bands use it to post show times and stuff, which is cool, but other than that? not really.)

CharlieSpencer
CharlieSpencer

"most windows users I know are terrified of the command line, ..." Most Windows users don't know the command line exists. Regardless, what does the number of potential users have to do with the usefulness of the script? By that logic, most computer users run Windows so there's no reason for developers to write for Linux. Oh, wait...

david.goodwin
david.goodwin

Email has been around in one form or another for well over 40 years now. Twitter has its uses, when you think of it as a mashup between SMS messages and Usenet. If you're using SMS or email for notifications, it's fine for a few people, but if you have 200 people that might wanna know about your notifications, twitter is superior. It's also device independent, and persistent, so if I receive an SMS on my phone that I left at work, I'm out of luck until I go get it, but if I get notified via a twitter account, I can see it from a variety of places.

CharlieSpencer
CharlieSpencer

I've decided I don't have any use for it either. I've read web logs and posts by many people whose opinions I respect, people who think Twitter is a good thing. However, I now think their information needs have nothing in common with mine. I have no desire to post content, and my inability to locate content of interest with it is both frustrating and disappointing. I also don't feel any need to know 'breaking news' the second it happens.

Thrash Cardiom
Thrash Cardiom

Of course I'm not saying you should shut up - but then you didn't really say anything in the first place apart from "I don't understand it". That isn't a diverse opinion.

rmerchberger
rmerchberger

I would think one reason to use twitter instead of email/SNMP is cost. In many outlying areas (including the area I reside), SMS messages can get through with "less than optimal" signal strength, but trying to get on the Interweb is problematic (put mildly). Not everyone has an iPhone. ;-) I don't use Twitter either, but I'd set up an account to receive alerts via SMS before paying for fullbore Interweb access that doesn't work most of the time in my area anyhow... but that's just me. Laterz, "Merch"

Ralph.Stoos
Ralph.Stoos

I think you are an optimist! The numbers of people that I see that don''t know is astounding. The number of people that stumble through life not caring is depressing. My 86 year old father who has been retired for 24+ years still reads up on new technology. A lot of folks are just sheep. BAAAAAAAH! Doctor Digital

csmith.kaze
csmith.kaze

My problem is not that people should have use their computers for more than just tools, just that they are willingly being shafted by big corporations with more marketing budgets. If someone was driving a POS car and paid ten times what I did for the same spec'd car, I would say something. People using Windows is buying the exact same computer for more (maybe not $$$ per se, but in time lost due to any number of failures that are so easy to accomplish on Windows. For all the "hardness" of Linux, you can't crash your system and lose all your data solely through surfing the 'net (you can with windows without adequate protection). I know most of that is education, but since it is just a "tool" people feel that learning the least amount possible about is good and techs egg them on. At least with a car you have to take a few tests)

CharlieSpencer
CharlieSpencer

but many of these Windows users don't know there's an alternative. Is it their fault they don't know something exists? As to their not caring, you may be right, but not everybody cares about everything. To most people, a computer is only a tool, like a car or a vacuum cleaner. You don't get upset if people drive factory standard cars and haven't overclocked there cleaner, do you? Why get upset because people simply use their systems to accomplish only what they're interested in, not what you or I think they should be doing? Why worry about something you can't effect?

csmith.kaze
csmith.kaze

I have absolutely no sympathy for ignorance. I can't. I would literal go insane if i thought for even an instant that these people may just be a breakthrough away from understanding. I would go insane from never seeing this realization come to fruition. Ignorance is bliss they say. Purposeful ignorance is not worth my sympathy. People don't know any better because they don't "care." Doesn't matter in the long run. Sooner or later Big Brother and Big Business will eat the people and we all be proles on the dole. (wow, I read that after posting. I am a very jaded person. I may need to talk someone....)

CharlieSpencer
CharlieSpencer

There's a difference. It's not that they're too stupid to know, they've just never been introduced to it. If all you've ever seen is an automatic transmission, it never occurs to you there might be a standard drive tranny. Not stupid, ignorant.

csmith.kaze
csmith.kaze

most users are idiots, what is your point? All you are telling me is that all windows users are too stupid to know better.

Editor's Picks