nanog mailing list archives

Re: interger to I P address


From: Michael Holstein <michael.holstein () csuohio edu>
Date: Wed, 27 Aug 2008 10:13:13 -0400


ls it possible t convert the interger to ip

#!/usr/local/bin/perl
# Perl script to convert between numeric and dotted quad IPs.
# give credit to Paul Gregg for this one
while (<STDIN>) {
 chomp; $input = $_;
 if (/\./) {
   ($a, $b, $c, $d) = split(/\./);
   $decimal = $d + ($c * 256) + ($b * 256**2) + ($a * 256**3);
 } else {
   $decimal = $_;
   $d = $_ % 256; $_ -= $d; $_ /= 256;
   $c = $_ % 256; $_ -= $c; $_ /= 256;
   $b = $_ % 256; $_ -= $b; $_ /= 256;
   $a = $_;
 }

 if ( ($a>255) || ($b>255) || ($c>255) || ($d>255) ) {
   print "$0: Invalid input: $input\n";
 } else {
   printf ("Address: %d.%d.%d.%d is %u  (Hex:%02x%02x%02x%02x)\n",
$a,$b,$c,$d, $decimal,$a,$b,$c,$d);
 }
}



Current thread: