[107142] in North American Network Operators' Group
Re: interger to I P address
daemon@ATHENA.MIT.EDU (Michael Holstein)
Wed Aug 27 10:12:28 2008
Date: Wed, 27 Aug 2008 10:13:13 -0400
From: Michael Holstein <michael.holstein@csuohio.edu>
To: kcc <peterkcc2001@gmail.com>
In-Reply-To: <a7d0162f0808270411m66a3e1b7v9a7ae997ab547bee@mail.gmail.com>
Cc: nanog@nanog.org
Errors-To: nanog-bounces@nanog.org
> 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);
}
}