Very informative post.

I just wanted to point out the URI module is excellent if you need to break apart a URI (a susperset of URL and URN). It handles all of the odd syntax that Maclir posted above. You can just do:

use URI; use Socket; my $url = "http://www.yahoo.com/whatever/foo?bar=baz # Get the host out of the URL string my $uri = URI->new($url); my $host = $url->host; # Get the IP address for the host my $packed_ip = gethostbyname($host) or die "Unable to resolve '$host'\n"; my $ipaddr = inetntoa($packed_ip); print "Got '$ipaddr' for host '$host'\n";

Another gotcha is that the IP addresses can revolve. So www.yahoo.com resolves to 9 addresses. My code above does not take that into account so if you want to know all of the addresses you will need to use Net::DNS. This multiple 'round-robin' address resolution is a common way of implementing load balancing. Your resolver will pick one of the addresses at random so you will only connect to one server, but other people will get a different server so on average the load will be sort of balanced across all of the machines.

As you can see DNS is not exacly simple. So what are you really trying to do?

-ben


In reply to Re: Re: How can I change web address into its IP's ? by knobunc
in thread How can I change web address into its IP's ? by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.