in reply to Re: How can I change web address into its IP's ?
in thread How can I change web address into its IP's ?
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
|
|---|