#!/usr/bin/perl -w # # test_DNSBL # # AUTHOR: # Dan Harkless # # COPYRIGHT: # This file is Copyright (C) 2008 by Dan Harkless, and is released under the # GNU General Public License . # # USAGE: # % test_DNSBL [-W ] [-s [-d ] [-e ]] [...] # # EXAMPLES: # % test_DNSBL list.dsbl.org # % test_DNSBL -s -W 45 sbl.spamhaus.org 127.0.0.1 127.0.0.2 # % test_DNSBL -e you@domain.tld -s relays.osirusoft.com 127.0.0.9 # # DESCRIPTION: # Looks up one or more IP addresses on the specified DNS Blackhole List # , using BIND's 'host' command. If no parameters are given, the # default 127.0.0.2 address is looked up. # # The -W option, if specified, will be passed to the 'host' command to cause # it to wait seconds for a reply before giving up. If not specified, # it will default to however many seconds 'host' defaults to -- usually 5. (I # would have added a -R parameter as well to control the number of retries, # but on my machine, at least, host's -R parameter does nothing and it always # tries twice.) # # If -s is given, the 'host' command's output is piped through 'sort' (for # cases like 2.0.0.127.sbl-xbl.spamhaus.org where a lookup returns multiple # results, in random order) and saved to /. If is not # specified with -d, ~/.test_DNSBL is used (which may not work as expected on # systems where /bin/sh is Bourne shell rather than POSIX shell). The # directory is assumed to already exist. Next, my monitor_file script is # called to compare the results to the previous ones, as a verification that # the DNSBL is functional. Naturally 'test_DNSBL -s' is most useful when # called from a periodic cron job. # # When -s is being used, -e can be specified to cause monitor_file to email # the specified address if a difference is found, rather than just # outputting to stderr. # # DATE MODIFICATION # ========== ================================================================== # 2008-09-02 "use English qw(-no_match_vars)": avoid regex performance penalty. # 2006-08-12 monitor_file now does a 'diff' and no longer has a -w option. # 2004-09-19 When using -s, pipe the results through sort for multiple-answer. # 2004-09-19 monitor_file was incorrectly being called when -s not specified. # 2003-03-10 Original. ## Modules used ################################################################ use English qw(-no_match_vars); # allow use of names like @ARG rather than @_ use File::Basename; # for basename() use Getopt::Std; # for getopts() ## Main ######################################################################## $progname = basename($PROGRAM_NAME); use vars qw($opt_d $opt_e $opt_s $opt_W); # eliminate "used only once" warning if (not getopts("d:e:sW:") or scalar(@ARGV) < 1) { print STDERR "Usage:", " $progname [-W ] [-s [-d ] [-e ]] [...]\n"; exit 1; } if (not $opt_d) { $opt_d = "~/.test_DNSBL"; } $zone = shift; if (scalar(@ARGV) < 1) { @ARGV = ("127.0.0.2"); } $first_one = 1; foreach $IP (@ARGV) { @octets = split /\./, $IP; $reversed_IP = join '.', reverse(@octets); $host_commandline = "host"; if ($opt_W) { $host_commandline .= " -W $opt_W"; } $host_commandline .= " $reversed_IP.$zone"; if ($opt_s) { $host_commandline .= " | sort"; if ($first_one) { $host_commandline .= " >"; $first_one = 0; } else { $host_commandline .= " >>"; } $host_commandline .= "$opt_d/$zone 2>&1"; } system $host_commandline; } if ($opt_s) { $monitor_file_commandline = "monitor_file"; if ($opt_e) { $monitor_file_commandline .= " -e $opt_e"; } $monitor_file_commandline .= " -m $opt_d/$zone"; system $monitor_file_commandline; }