Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

INET_NTOA Equivalent

by turbo3k (Beadle)
on Apr 14, 2004 at 22:19 UTC ( [id://345227]=perlquestion: print w/replies, xml ) Need Help??

turbo3k has asked for the wisdom of the Perl Monks concerning the following question:

I can execute the following SQL query in MySQL:
mysql> SELECT INET_ATON('192.168.1.100'); +----------------------------+ | INET_ATON('192.168.1.100') | +----------------------------+ | 3232235876 | +----------------------------+ 1 row in set (0.00 sec)
I can then execute another query to reverse the process:
mysql> SELECT INET_NTOA('3232235876'); +-------------------------+ | INET_NTOA('3232235876') | +-------------------------+ | 192.168.1.100 | +-------------------------+ 1 row in set (0.00 sec)
How would I duplicate this process in Perl? There is an ‘inet_aton’ operator in Perl but it does not return a decimal number as shown above. I would like the ability to print this number out to the screen.
#!/usr/bin/perl -w use strict; use Socket; my $string = inet_aton("204.87.241.1"); print "$string\n";
Thanks in advance for any help.

Replies are listed 'Best First'.
Re: INET_NTOA Equivalent
by saintmike (Vicar) on Apr 14, 2004 at 22:27 UTC
    Just unpack the "unsigned long" in network order:
    use Socket; my $iaddr = inet_aton("192.168.1.100"); print unpack('N', $iaddr), "\n";
Re: INET_NTOA Equivalent
by esskar (Deacon) on Apr 14, 2004 at 22:28 UTC
    hi
    #!/usr/bin/perl use strict; use Socket; print unpack("N", inet_aton('192.168.1.100'));
    HTH
Re: INET_NTOA Equivalent
by NetWallah (Canon) on Apr 14, 2004 at 23:58 UTC
    In case you dont want to load a module for this purpose, here is some code you can use (For IPv4):
    sub to_dotquad { # Given a binary int IP, returns dotted-quad (Reverse of ip2num) my $bin = shift; my $result = ''; # Empty string for (24,16,8,0){ $result .= ($bin >> $_ & 255) . '.'; } chop $result; # Delete extra trailing dot return $result; }

    Offense, like beauty, is in the eye of the beholder, and a fantasy.
    By guaranteeing freedom of expression, the First Amendment also guarntees offense.

      Here's one more potential way to do it, without loading additional modules. Assuming the IPv4 address is in $address (and of a form matching m/((\d{1,3}\.){3}(\d{1,3}))/ ), then:

      $ip_as_long = unpack("N", pack("C4", split(/\D/, $address, 4))); $ip_as_string = join('.', unpack("C4", pack("N", $ip_as_long)));

      Hope that helps...

Re: INET_NTOA Equivalent
by jaco (Pilgrim) on Apr 15, 2004 at 00:15 UTC

    Not that it really matters because Socket is part of every perl distribution. But you could also do it like so

    my $string = unpack('N',scalar(gethostbyname("192.168.1.100")));
Re: INET_NTOA Equivalent
by iburrell (Chaplain) on Apr 15, 2004 at 20:23 UTC
    Socket also has inet_ntoa. So this will return the string version of the IP address:
    my $string = inet_ntoa(inet_aton('192.168.1.100'));
Re: INET_NTOA Equivalent
by Anonymous Monk on Sep 01, 2018 at 13:24 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://345227]
Approved by Thelonius
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (11)
As of 2024-03-28 09:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found