#!/usr/bin/perl -w # Copyright (c) 2004, James O'Gorman # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions # are met: # # 1. Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # 2. Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND ANY # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE # DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF # SUCH DAMAGE. use strict; if (!defined($ARGV[0])) { @ARGV = split(/\s+/, ); } foreach my $ip (@ARGV) { if ($ip =~ /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/) { #print "$ip is IPv4\n"; &arpa($ip, 4); } elsif ($ip =~ /^([a-zA-Z0-9]{0,4}[:]{1,2})+([a-zA-Z0-9]{0,4})?$/) { #print "$ip is IPv6\n"; &arpa($ip, 6); } else { print "Error in ${ip}\n"; print "Either IPv4 ('.' notation) or IPv6 (':' notation) must be used\n"; exit 1; } #endif } #endfor sub arpa($;$) { my ($ip, $ver) = @_; my $host; if ($ver == 6) { my @addr = split(/:/, $ip); my $pad; if ($#addr < 8) { $pad = 4 * (8 - $#addr); } foreach (my $i = $#addr; $i >= 0; $i--) { if (length($addr[$i]) == 0) { for (my $p = 0; $p < $pad; $p++) { $addr[$i] .= "0"; } } else { while (length($addr[$i]) < 4) { $addr[$i] = 0 . $addr[$i]; } } $addr[$i] = "$addr[$i]"; # Convert the number to a string if (length($addr[$i])-1 == 0) { } for (my $x = length($addr[$i])-1; $x >= 0; $x--) { $a = substr($addr[$i], $x, 1); $host .= $a . "."; } } my $domain = $addr[0] eq '3ffe' ? "int" : "arpa"; $host .= "ip6.$domain."; } elsif ($ver == 4) { my @addr = split(/\./, $ip); for (my $i = $#addr; $i >= 0; $i--) { $host .= $addr[$i]."."; } $host .= "in-addr.arpa."; } print "$host\n"; }