Testing banners with Perl

May 27, 2014

Sometimes you need to iterate over a series of hosts, checking for banners and calculating how long it takes for a banner to respond. This is basic bread and butter sysadmin testing. I wrote a little CPAN module to handle this which can easily be rolled into any monitoring environment.

You write a handler function which gets called by the API upon execution of the run_tests function. Your callback function will return the results of whatever you’re testing, which gets stored by run_tests and returned in the hash reference upon completion.

#!/usr/bin/perl
use strict;
use Net::PortTest;
 
on 143 => sub {
    my $sock = shift;
    my $results = {};
    my $rc = -1;
   
    $results->{banner} = $sock->getline;
   
    $rc = 0
        if $results->{banner} =~ '^\* OK';
   
    return $rc, $results;
};
 
alias qw/ 10143 20143 / => 143;
 
my $results = run_tests 'mail.hostedemail.com' => 143;

if ($results->{143}->{rc} == 0){
    print "mail.hostedemail.com:143\t" . $results->{143}->{delta} . "\n";
}
else {
    print "mail.hostedemail.com:143\tFAIL\n";
}
$ perl port-test.pl 
mail.hostedemail.com:143        0.00374698638916016

:)

Discussion, links, and tweets

Follow me on Twitter