--- bin/rancid.in.orig 2015-07-03 17:22:07.000000000 +0100 +++ bin/rancid.in 2015-07-06 07:10:54.000000000 +0100 @@ -45,6 +45,7 @@ # usage: rancid [-dV] [-l] [-f filename | hostname] # use Getopt::Std; +use Socket qw(AF_INET AF_INET6 inet_pton); getopts('dflV'); if ($opt_V) { print "rancid 2.3.8\n"; @@ -162,13 +162,38 @@ @sorted_lines; } -# These two routines will sort based upon IP addresses +# ipaddrval(IPaddr) converts and IPv4/v6 address to a string for comparison. sub ipaddrval { - my(@a) = ($_[0] =~ m#^(\d+)\.(\d+)\.(\d+)\.(\d+)$#); - $a[3] + 256 * ($a[2] + 256 * ($a[1] +256 * $a[0])); + my($a) = @_; + my($norder); + + if ($a =~ /:/) { + my($l); + if ($a =~ /\//) { + ($a, $l) = split(/\//, $a); + } else { + $l = 128; + } + $norder = inet_pton(AF_INET6, $a); + return unpack("H*", $norder) . unpack("H*", pack("C", $l)); + } else { + my($l); + if ($a =~ /\//) { + ($a, $l) = split(/\//, $a); + } else { + $l = 32; + } + $norder = inet_pton(AF_INET, $a); + return(unpack("H*", $norder) . unpack("H*", pack("C", $l))); + } + + # otherwise return the original key value, so as not to sort on null + return($_[0]); } + +# sortbyipaddr(IPaddr, IPaddr) compares two IPv4/v6 addresses like strcmp(). sub sortbyipaddr { - &ipaddrval($a) <=> &ipaddrval($b); + &ipaddrval($a) cmp &ipaddrval($b); } # This routine parses "show version"