Created
October 19, 2016 07:55
-
-
Save techdad/4b299194336c60341a93564c38c7fb71 to your computer and use it in GitHub Desktop.
Quick and dirty IP address range to CIDR converting script
This file contains 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/env perl | |
# Quick and dirty perl to take start and end IP addresses | |
# (either ipv4 or ipv6), and convert the range between them | |
# into aggregated CIDR notation. | |
# The magic is the Net::CIDR CPAN module. | |
# Reference: http://search.cpan.org/~mrsam/Net-CIDR/CIDR.pm | |
## Daniel Shaw <[email protected]> - October 2016 | |
use strict; | |
use Net::CIDR; | |
# do some basic checks for the command-line arguments | |
my $start_ip = shift(@ARGV) or die_usage(); | |
my $end_ip = shift(@ARGV) or die_usage(); | |
my $v_ip=Net::CIDR::cidrvalidate($start_ip) or die_not_ip($start_ip); | |
$v_ip=Net::CIDR::cidrvalidate($end_ip) or die_not_ip($end_ip); | |
# sub routines for simple error messages | |
sub die_usage { | |
print "Error: Usage: " . $0 . " start-IP end-IP\n"; | |
exit 1; | |
} | |
sub die_not_ip { | |
my $bad_arg = shift(@_); | |
print "Error: Argument \'" . $bad_arg . "\' is not an IP address.\n"; | |
exit 2; | |
} | |
### start actual work here. hooray for cpan ### | |
my $ip_range = join("-", $start_ip, $end_ip); | |
my @ip_cidr = Net::CIDR::range2cidr($ip_range); | |
print join("\n", @ip_cidr) . "\n"; | |
# eof |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment