Re: Perl scripts

Ian Eiloart <[email protected]> Mon, 26 Jun 2006 17:34:32 +0100
Newsgroups gmane.comp.apache.mod-wackamole.general
Message-ID <[email protected]>
OK, so here's a slightly useful example script, which logs interfaces as 
they come up. It also shows how to extract the IP addresses that just came 
up. For example, in future I'll use that data to bring up an Exim smtp 
server listening on that IP address.

NB: The brick wall I was banging my head against today was the use of null 
terminated strings in the hash keys!

My wackamole.conf file holds this segment:
------------------------------------------

# Named socket for online control
Control = /var/run/wack.it
PerlUseLib /opt/local/etc/
PerlUse wackamole
RunDynamic wackamole::onup on up

And, here's the content of wackamole.pm:
----------------------------------------

package wackamole;

use strict;
use Sys::Syslog;
use Data::Dumper;

sub logaddressup {
    sleep 2;
    my ($ipadd) = @_;
    # let's also open a syslog facility
    openlog('wackamole.pm', 'ndelay pid', 'daemon');

    # this is how we address an element of the hash
    #  keys we could use are (all null terminated!):
    # "ifname\0" eg: en0
    # "broadcast\0" eg: "192.168.0.1"
    # "netmask\0" eg: 255.255.255.255
    # "ip\0" eg "192.168.0.1"
    my $address =  $ipadd->{ "ip\0" };
    my $if = $ipadd->{"ifname\0"};
    my $string = 'IP address '.$address.' acquired on '.$if.'   ';

    # make a syslog notice
    syslog('notice','%s',$string);

    closelog();
}

sub onup {
    # we're being passed:
    # 1. a hash reference describing what just came up
    # 2. an array of hash references - the rest of the VIF
    # 3. a hash reference with the real interface that was affected
    # we'll just put them all into separate variables
    my ($ipadd, $vifs, $extern) = @_;
    #open (NFAM, '> /opt/local/etc/klop');
    #print NFAM Dumper(\@_);
    logaddressup($ipadd);
    # report each additional ip address (NB $vifs is a reference)
    foreach my $ipaddss (@{$vifs}){
        logaddressup($ipaddss);
    }
    #close NFAM;

}
# return a true value, otherwise wackamole doesn't start
1;




-- 
Ian Eiloart
IT Services, University of Sussex