Created
January 7, 2011 18:45
-
-
Save waffle2k/769902 to your computer and use it in GitHub Desktop.
A little script for taking in lists of IPs/cidrs, and outputting/formatting depending on the format type
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
# A little script to take in some kinds of formats, and output | |
# the converted IPs based on the type of rbl zone being used. | |
# | |
# This is specifically used for including exclusion lists into | |
# a rbldnsd zone file. This will take a generic list of IPs, | |
# and output them for the appropriate rbldnsd zone type. | |
# | |
# Author: pblair (at) tucows (dot) com | |
# Date: Fri Jan 7 13:44:20 EST 2011 | |
use strict; | |
use Net::IP; | |
use NetAddr::IP; | |
use Getopt::Long; | |
my $verbose = 0; | |
sub addslash { | |
my $in = shift; | |
if( $in =~ /.*\/\d+/ ){ | |
print "Returning $in\n" if $verbose; | |
return $in; | |
} | |
if( $in =~ /^(\d+?)$/ ){ | |
print "Returning $in and /8\n" if $verbose; | |
return "$in/8"; | |
} | |
elsif( $in =~ /^\d+\.\d+?$/ ){ | |
print "Returning $in and /16\n" if $verbose; | |
return "$in/16"; | |
} | |
elsif( $in =~ /^(\d+\.\d+\.\d+?)$/ ){ | |
print "Returning $in and /24\n" if $verbose; | |
return "$in" . "/24"; | |
} | |
elsif( $in =~ /^\d+\.\d+\.\d+\.\d+?$/ ){ | |
print "Returning $in and /32\n" if $verbose; | |
return "$in" . "/32"; | |
} | |
return undef; | |
} | |
sub testit { | |
my @ipset = ( '127.15', '8/8', '192.168.0.1', '21.22.23' ); | |
# convert the ips to cidr | |
my @cidr = map( addslash( $_ ), @ipset ); | |
for my $ipstr ( @cidr ){ | |
print "Processing $ipstr\n"; | |
my $ip = Net::IP->new( $ipstr, 4 ); | |
print ("IP : ".$ip->ip()."\n"); | |
print ("Sho : ".$ip->short()."\n"); | |
print ("Bin : ".$ip->binip()."\n"); | |
print ("Int : ".$ip->intip()."\n"); | |
print ("Mask: ".$ip->mask()."\n"); | |
print ("Last: ".$ip->last_ip()."\n"); | |
print ("Len : ".$ip->prefixlen()."\n"); | |
print ("Size: ".$ip->size()."\n"); | |
print ("Type: ".$ip->iptype()."\n"); | |
print ("Rev: ".$ip->reverse_ip()."\n"); | |
} | |
exit 0; | |
} | |
my $test = undef; | |
my $format = undef; | |
my $input = undef; | |
my $result = GetOptions ( "input=s" => \$input, # string | |
"format=s" => \$format, | |
"test" => \$test); # flag | |
testit() if $test; | |
ENTRY:while( <> ){ | |
chomp; | |
my $input = $_; | |
print "Raw IP is: $input\n" if $verbose; | |
my $corrected = addslash( $input ); | |
print "Corrected: $corrected\n" if $verbose; | |
my $ip = Net::IP->new( $corrected, 4 ) || die("Could not parse $input\n"); | |
print "Input is: $ip\n" if $verbose; | |
# http://www.corpit.ru/mjt/rbldnsd/rbldnsd.8.html#DATASET%20TYPES%20AND%20FORMATS | |
if( $format eq 'ip4set' ){ | |
print $corrected . "\n"; | |
next ENTRY; | |
} | |
elsif( $format eq 'ip4trie' ){ | |
print $corrected . "\n"; | |
next ENTRY; | |
} | |
elsif( $format eq 'ip4tset' ){ | |
# single IP addresses.. | |
my $n = NetAddr::IP->new( $corrected ); | |
for my $singleip ( @{$n->hostenumref} ){ | |
# strip off the / | |
$singleip =~ s/\/\d+//g; | |
print $singleip . "\n"; | |
} | |
next ENTRY; | |
} | |
die("Unsuported ip format: $format\n" ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment