Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: gethostbyname not working

by myocom (Deacon)
on Mar 27, 2001 at 01:59 UTC ( #67346=note: print w/replies, xml ) Need Help??


in reply to gethostbyname not working

As you will see in the docs, gethostbyname returns a list, the last element of which is the IP address in packed format. You'll need to unpack it first, like so:

my $hostname = 'yahoo.com'; my $addr = (gethostbyname($hostname))[4]; # Or, as Anonymous Monk points out below, you could simply use: # my $addr = gethostbyname($hostname); my ($a,$b,$c,$d) = unpack('C4',$addr); print "$a.$b.$c.$d\n";

Update: Corrected which element of gethostbyname returns the address.

Update 2: Added AM's advice.

Replies are listed 'Best First'.
Re: Re: gethostbyname not working
by Anonymous Monk on Mar 27, 2001 at 02:32 UTC
    You can simply use
    my $addr = gethostbyname($hostname);
    instead of
    my $addr = (gethostbyname($hostname))[4];
    When used in scalar context, gethostbyname returns the address, which would have prevented your earlier bug.
      This is what I did at first, that was the problem (if you look at my original post). what's the difference?
        You weren't unpacking the address.
        You still need to unpack the results.

        A complete example would be:

        my $hostname = 'yahoo.com'; my $addr = gethostbyname($hostname); my ($a,$b,$c,$d) = unpack('C4',$addr); print "$a.$b.$c.$d\n";

        Signature void where prohibited by law.

      Ah, so it does! Thanks for the tip!

Re: Re: gethostbyname not working
by nick (Sexton) on Mar 27, 2001 at 02:14 UTC
    This gets me further, when I print them out however, I get numbers that are no relative the the IP address, or even close. for instance, yahoo.com
    my $hostname = 'yahoo.com'; my $addr = (gethostbyname($hostname))[0]; my ($a,$b,$c,$d) = unpack('C4',$addr); print "$a.$b.$c.$d\n";

    This prints out: 121.97.104.111
    which is not even close to yahoo's IP (I know yahoo has a large subnet, but this doesn't even start with the same number). Any ideas?
      Using unpack here seems a bit awkward. Why not use the builtins from Socket and simplify?
      use Socket; my $hostname = 'yahoo.com'; print inet_ntoa(inet_aton($hostname)),"\n";
      inet_aton converts from "ASCII" to "Network", and as a bonus will resolve host names. The reverse, inet_ntoa, will do the inverse, though it will always give you a dotted IP and not the name. To find that out, you will have to use Net::DNS.

      You will note that it puts out different answers on occasion, though, because there are many possible addresses to choose from, and they are selected at random, which is called "Round Robin DNS".

      As a side note, this code will die "Bad arg length..." if fed an invalid or unresolvable address. This can be fixed by splitting it into two steps and verifying that inet_aton() actually returned something.

      Oops, my mistake; it's the last bit of gethostbyname, not the first. Good catch. I've updated my node above.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2023-12-07 20:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?











    Results (33 votes). Check out past polls.

    Notices?