Perl



Published August 7th, 2003 by Jim O'Halloran

Simple SMS Responder

We’ve been using a combination of NetSaint for monitoring and gnokii to do SMS notifications when something breaks on our internal network for a while now. We have a Nokia 5110 phone wired up to an old machine in our server rack which is dedicated to monitoring. Works good as gold, and has saved my bacon a number of times.

However we’ve now had a situation arise where the Tech on call might need to access a small bit of information (which changes daily) from our Intranet, but do so without having access to a PC.

The tech always has a mobile phone, so the logical answer seems to be to have him/her send an SMS message to our phone, and have it send back the info he/she needs in response. Thus I needed to try and hack together a quick and dirty SMS responder in perl.

The good news is that it wasn’t too hard at all… Click here to download it. It assumes that you already have gnokii installed and running. I’ve tested it against gnokii 0.3.5 which I’ve found to be very stable. Just set it up to run as a cron job every few minutes. The responder understands two commands “ping” (will SMS back a “PONG!”), and “time” (will SMS back the current server time).

Its unbeleivably basic and only really suited to low volume applications, but I figured I’d release it in case its of use to someone. If you end up using it in anything, drop me an email and let me know.

Published April 14th, 2003 by Jim O'Halloran

Perl for Football Tipping Update

A while ago I posted a perl script for football tipping. Well, I’ve now updated the script in question, and offer it for download here. The script will now also parse the winning margins on the tips page to produce an average margin, and also downloads and parses the AFL home page to get this weeks fixture. Once it has the fixture, it will now combine the experts tips, and the fixture to tell you who you should tip and why. Dead simple tipping for non-experts!

After the first three rounds of he season I’m now 1 point off the lead in our cpompetition, and I used the script to tip every round.

Published March 28th, 2003 by Jim O'Halloran

Perl for Football Tipping

The AFL footy season is upon us again, which in most offices means football tips… I don’t follow the footy, but I do go in the tipping comps for a bit of fun. The AFL web site has an “Expert Tips” page, so this year I decided to do something a bit different and write a perl script to do most of my footy tips for me :) Perl script is below…

#!/usr/bin/perl -w
#
#AFL Footie Tipping
#
#Author: jim@jimohalloran.com
#

#Configurable Items
$TIP_URL = "http://afl.com.au/default.asp?pg=tipsters&spg=default";
$TIP_PREFIX = "class\=p3\>";

#Download Page
$TipPage =`wget "$TIP_URL" -q -O -`;

@HTMLLines = split(/\n/, $TipPage);
foreach $ThisOne (@HTMLLines) {
  if ( ($Team) = ($ThisOne =~ /$TIP_PREFIX([A-Za-z ]+)/) ) {
    $Tipped{$Team}++;
  };
};

foreach $ThisOne (sort {$Tipped{$b}<=>$Tipped{$a} } keys %Tipped) {
  print “$ThisOne  was picked ” . $Tipped{$ThisOne} . ” time(s)\n”;
};

… run the script once each week, and it’ll show you who the experts tipped. Then go through the fixture for the current week and pick whichever team the experts tipped most.

One day I might automate the last part and submit my tips using a cron job, but it does the job for now :)

UPDATE: I’ve updated the script with a few enhanced features, and posted it here

Published March 14th, 2003 by Jim O'Halloran

Perl and mod_perl

DevShed has an excelent series of articles on web programming with mod_perl, including a basic introduction to Perl itself.

Published February 7th, 2003 by Jim O'Halloran

LogWatch hacking

I’ve been hacking LogWatch again. Today I offer two patches… The first is a minor change to the sendmail module. This makes my previous mod for Amavis message counting a little more flexible, allowing it to work with Amavis, Amavis-Perl, and Amavis-New instead of just the Amavisd which I currently run.

The second patch is against the “secure” script. fixes a minor problem with the deleted used regexp whih caused user deletes to be listed under “Unmatched Entries” on my machine. Also added code to list Group Adds and Deletes in addition to users.

Both patches are against the 4.3.1 release of LogWatch. v4.3.1 also includes all of my previous patches.

Published January 29th, 2003 by Jim O'Halloran

LogWatch Patch - Unknown Users

Kirk Bauer posted a request from Ammar T. Al-Sayegh to Logwatch-Devel a few hours ago…

The log messages contain a sendmail section for Unknown Users. The section shows the addresses for the unknown users that receive emails, but doesn’t show the IP numbers of the relays that send these emails. I have to look them up by hand in the maillog. Is it possible to set the sendmail LogWatch script to include the senders relay next to the email address of the known user?

To which I offered this patch. The new output appears as follows….

Unknown users:

    allen@example.com.au
      from [202.27.217.203]    4 time(s).

    ashley@example.com.au
      from [139.130.107.32]    1 time(s).
      from mail.chariot.net.au [203.87.95.38]    1 time(s).

The relay= field and the User Unknown message are actually in two seperate log entries. The patched script gets around this by creating an associative array containing the relay host keyed by the sendmail Queue ID for every message passing through the server. When later compiling the report, we look up the relay host in the array and summarise the results. There is probably an easier way to do this, but my perl knowledge is a little limited, so its the best I was able to come up with. It does mean we can still generate the whole report with only a single pass through the log.

I’ve tested this on my own logs and it seems to work fine.