Posts Tagged ‘perl’

Longest Common Prefix in perl

Friday, September 9th, 2005

I can’t help finding this slightly elegant. Unfortunately I no longer remember where the idea came from.

#!/usr/bin/perl

use strict;
use warnings;

sub longest_common_prefix {
    my $prefix = shift;
    for (@_) {
	chop $prefix while (! /^$prefix/);
    }
    return $prefix;
}

print longest_common_prefix(@ARGV), "
";

irctail – track a file on an irc channel

Sunday, June 5th, 2005

Irctail is a small program I wrote to keep track of log files on various servers through an IRC channel.

It will track stdin or a given file (like tail -f), and send the lines to an IRC channel.

There are options to specify irc server, channel, nick and some more. An example:

irctail -c channel -n nick irc.example.com /var/log/messages

It is possible to specify a regular expression where a prefix match on the input line will be removed before the line is sent to the irc channel. It can be used to remove timestamps and other stuff that might clutter an irc channel too much.

A more real life example.

I’m managing a fairly large network with private users, and sometimes somebody installs a wifi network so the dhcp server is exposed on the entire network. This will cause other users problems as they get dhcp responses from the wrong dhcp server.

To monitor the situation I run these commands on a network management server:

tcpdump -lten -i eth5 udp and src port 67 and dst port 68 and not net 10 >dhcp-servers.out &
irctail -c staff -n dhcp irc.example.com dhcp-servers.out &

I could just have piped the output of tcpdump and left out the output file but I’d like to keep it for later reference.

Download the irctail source code

Full manual below the fold. (more…)