RSS

Tag Archives: script

So alike, yet so different

I’ve recently fallen into the world of Mac OS X. (Fallen, pushed; what is difference?)

I keep getting caught out by the little things that are just slightly different to what I’m used to in Linux.

Take the most recent example: the mail command. I had a little anacron script set up on my Ubuntu box that would generate a summary of a previously-unseen messages in a user’s Gmail spam folder, and send it to that user. (Mostly because Mrs E kept forgetting to check hers.)

While I was perusing the mail man page on Mac OS X, and reading up on various options on Google, I found that the Mac version had a useful -E switch that would allow it to quietly abort if there was no message body, saving me the hassle of having to find a recent port of moreutils so that I could use the ifne command.

The thing that got me stuck for ages was trying to find a replacement for the -a switch in the GNU/Linux version of mail, which allowed the insertion of arbitrary headers, and was useful for setting the sender to something appropriate.

Eventually, after much reading of man pages and search results, I realised that the answer had been staring me in the face for ages, I just wasn’t doing it correctly because the order of the parameters became more important:

echo Wibble | mail -s "Subject goes here" recipient-address@example.com -r "<mail-sender@example.com>" -F "Mail Sender"

That was harder than expected…

(Note to self: WordPress doesn’t tell you that it’s stripping out the content in angle brackets that you’ve just retyped three times…)

 
Leave a comment

Posted by on Tuesday 13 March 2012 in Geeky stuff

 

Tags: , , , ,

O you are Auth-ful…

… or, how Twitter’s changes broke my nice little perl script.

The script for queuing tweets still works, but they all disappear into the void rather than getting tweeted, now that Twitter only allows external applications to connect using OAuth.

I’ve found this handy little article here that seems to suggest that it’s easy enough to do using existing modules from CPAN.

Now all I need is a set of round tuits, and we should be back in business again. I’ve got some ideas for other stuff to tweet, once I get it all working again…

 
Leave a comment

Posted by on Monday 13 September 2010 in Geeky stuff

 

Tags: , , ,

Knit your own Tweets

The Perl scripts that I was fiddling about with seem to be working the way I wanted them to (even if what’s calling them isn’t always), so here they are for anybody else who wants to use them.

queue_tweet.pl


#!/usr/bin/perl

use DateTime;
use Sys::Hostname;

my $time = DateTime->now()->strftime("%F %T");
my $tweet = "$time\n";
my $hostname = hostname;

$tweet .= "$hostname\n";

while(defined($line=<STDIN>)){
    $tweet .= $line;
}

$tweet .= "%%\n";

#print $tweet

$tweetqueuefile="/var/local/tweetqueue";

open(QUEUE,">>$tweetqueuefile") || die("Cannot open tweet queue");
print QUEUE $tweet;
close(QUEUE);

send_tweet.pl


#!/usr/bin/perl

use File::Copy;
use Net::Netrc;
use LWP::UserAgent;

$tweetqueuefile="/var/local/tweetqueue";

open(QUEUE,"$tweetqueuefile") || die("Cannot open tweet queue");

# Read lines of first tweet

my $tweet;

while(defined($line=<QUEUE>)){
    if($line eq "%%\n"){
        # Exit loop
        last;
    }
    $tweet .= $line;
}

if($tweet != ""){
#	print "Sending tweet:\n$tweet";
    $machine="twitter.com";
    $netrc = Net::Netrc->lookup($machine);
    my $ua = LWP::UserAgent->new;
    my $req = HTTP::Request->new(POST => 'https://twitter.com/statuses/update.xml');
    $req->authorization_basic($netrc->login, $netrc->password);
    $req->content('status='.$tweet);
    my $result = $ua->request($req)->as_string;
#	print $result;
}

$tempfile="/tmp/tweettmp";

open(TEMP,">$tempfile") || die("Cannot open temporary file");

while(defined($line=<QUEUE>)){
    print TEMP $line;
}

close(TEMP);
close(QUEUE);

copy("$tempfile", "$tweetqueuefile") || die("Cannot overwrite tweet queue");

(As I’ve mentioned before, it’s a long time since I’ve written any Perl, so I’ve probably made any number of schoolboy errors. Still, they seem to do the trick.)

queue_tweet.pl reads from stdin to queue a tweet, and send_tweet.pl reads and sends the first tweet from the queue each time it’s called. You’ll need your credentials for twitter.com in your .netrc file.

For example, I’ve got the following entries in my crontab:

# Send any pending tweets
*/5 * * * * /usr/local/bin/send_tweet.pl

# Log disk temperatures
30 */4 * * * /usr/sbin/hddtemp /dev/sda /dev/sdb | /usr/local/bin/queue_tweet.pl

# Motherboard temperatures
35 */4 * * * /usr/bin/sensors | /bin/grep Temp | /usr/bin/cut -c 1-21 | /usr/local/bin/queue_tweet.pl

Let me know if you use them for anything more interesting than tweeting your system temperatures, or if you’ve got any (constructive) criticisms of my Perl code…

 
1 Comment

Posted by on Tuesday 16 February 2010 in Geeky stuff

 

Tags: , ,

Perl before swine

It seems that I’ve forgotten everything I ever knew about Perl.

All I wanted to do was write a little Perl script that could be called to queue up informational tweets, and another to send queued tweets which would be called from a crontab every five minutes or so, but I’m having to look up almost every statement in the trusty Camel book. Programming, the slow way.

Update: Finally got the scripts working the way I wanted. I was going to use the Net::Twitter module, but trying to install it just resulted in errors relating to Compress::Zlib::gzopen, so it seemed easier to do things the long way round using LWP::UserAgent and HTTP::Request. Once I’ve got the scripts plugged in to my crontab and rcX.d scripts, I’ll post them here in case anybody’s trying to work out how to do the same thing.

 
Leave a comment

Posted by on Wednesday 3 February 2010 in Geeky stuff

 

Tags: , ,