interger to I P address

Michael Holstein michael.holstein at csuohio.edu
Wed Aug 27 14:13:13 UTC 2008


> 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);
  }
}





More information about the NANOG mailing list