Convert Integer value to IP Address

V

Vikrant

Hi

Please tell me how do i convert a long integer IP address to dotted quad
IP address using perl language.

For example:- If IP address is 167772260 then how do i convert it into
dotted quad IP address.

I am able to do it by applying some mathematical operations, but i am
looking for a function in perl the can do the same,because in my code i
have deal with many IP addresses.

or plz suggest me any other way to do it.


With Regards
Vikrant
 
I

Ian Wilson

Vikrant said:
Hi

Please tell me how do i convert a long integer IP address to dotted quad
IP address using perl language.

For example:- If IP address is 167772260 then how do i convert it into
dotted quad IP address.

I am able to do it by applying some mathematical operations, but i am
looking for a function in perl the can do the same,because in my code i
have deal with many IP addresses.

or plz suggest me any other way to do it.

perldoc -q "IP address"
 
P

Paul Lalli

Vikrant said:
Please tell me how do i convert a long integer IP address to dotted quad
IP address using perl language.

For example:- If IP address is 167772260 then how do i convert it into
dotted quad IP address.

Relatively sure you want Socket.pm's inet_ntoa() function. Have a look
at it in
perldoc Socket

Paul Lalli
 
A

axel

Relatively sure you want Socket.pm's inet_ntoa() function. Have a look
at it in
perldoc Socket

Yes... although it needs some juggling:

print inet_ntoa(inet_aton(167772260));

Yields the required '10.0.0.100'.

Axel
 
R

Richard Gration

Yes... although it needs some juggling:

print inet_ntoa(inet_aton(167772260));

Yields the required '10.0.0.100'

This example using pack also yields the correct answer

perl -MSocket -e 'print inet_ntoa(pack 'N',167772260)'

but I'm not sure if this is a legit use of pack.

I find pack/unpack utterly confusing. Which is to say, whenever I try to
use it, I think really hard about what I'm trying to do, I read and
re-read "perldoc -f pack", I concentrate, very carefully write my line of
code thinking hard along the way and then: Voila! A complete load of tripe
comes out :-(

Rich
 
J

J. Gleixner

Vikrant said:
Hi

Please tell me how do i convert a long integer IP address to dotted quad
IP address using perl language.

For example:- If IP address is 167772260 then how do i convert it into
dotted quad IP address.

I am able to do it by applying some mathematical operations, but i am
looking for a function in perl the can do the same,because in my code i
have deal with many IP addresses.

or plz suggest me any other way to do it.

Net::Netmask (http://search.cpan.org/~muir/Net-Netmask-1.9012/) contains
a lot of useful methods pertaining to IP addresses and CIDR notation.

sub int2quad
{
return join('.',unpack('C4', pack("N", $_[0])));
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
DewittMill
Top