18.1. Simple DNS LookupsProblemYou want to find the IP address of a host or turn an IP address into a name. Network servers do this to authenticate their clients, and clients do it when the user gives them a hostname but Perl's socket library requires an IP address. Furthermore, many servers produce log files containing IP addresses, but hostnames are more useful to analysis software and humans. Solution
If you have a name like use Socket; @addresses = gethostbyname($name) or die "Can't resolve $name: $!\n"; @addresses = map { inet_ntoa($_) } @addresses[4 .. $#addresses]; # @addresses is a list of IP addresses ("208.201.239.48", "208.201.239.49")
Or, use use Socket; $address = inet_ntoa(inet_aton($name)); # $address is a single IP address "208.201.239.48"
If you have an IP address like use Socket; $name = gethostbyaddr(inet_aton($address), AF_INET) or die "Can't resolve $address: $!\n"; # $name is the hostname ("www.perl.com") Discussion
This process is complicated because the functions are mere wrappers for the C system calls, so this means you have to convert IP addresses from ASCII strings ( use Socket; $packed_address = inet_aton("208.146.140.1"); $ascii_address = inet_ntoa($packed_address);
The
A hostname may have more than one address, particularly busy web sites, where many machines serve identical web pages to share the load. In such situations, the DNS server that provides you the addresses rotates them to balance the load. If you need to pick an IP address to connect to, it is sufficient to always select the first (but if it doesn't work, try the rest as well): $packed = gethostbyname($hostname) or die "Couldn't resolve address for $hostname: $!\n"; $address = inet_ntoa($packed); print "I will use $address as the address for $hostname\n";
If you're using hostnames to permit or refuse access to a service, be careful. Anyone can set their DNS server to identify their machine as # $address is the IP address I'm checking, like "128.138.243.20" use Socket; $name = gethostbyaddr(inet_aton($address), AF_INET) or die "Can't look up $address : $!\n"; @addr = gethostbyname($name) or die "Can't look up $name : $!\n"; $found = grep { $address eq inet_ntoa($_) } @addr[4..$#addr]; It turns out that even with this algorithm, you can't be absolutely sure of the name due to a variety of methods that can be used to circumvent even this technique. Even the IP address from which the packets appear to be coming can be spoofed, so you shouldn't ever rely on the network layer for authentication. Always do authentication yourself (with passwords, or cryptographic challenges) when it really matters, because the IPv4 network was not designed to provide security. More information is kept about a host than just addresses and aliases. To fully access this information, use the Net::DNS module from CPAN. For instance, Example 18.1 shows how to retrieve the MX (mail exchange) records for an arbitrary host. Example 18.1: mxhost#!/usr/bin/perl # mxhost - find mx exchangers for a host use Net::DNS; $host = shift; $res = Net::DNS::Resolver-> Here's some output: % mxhost cnn.com
The Example 18.2: hostaddrs#!/usr/bin/perl # hostaddrs - canonize name and show addresses use Socket; use Net::hostent; $name = shift; if ($hent = gethostbyname($name)) { $name = $hent->name; # in case different $addr_ref = $hent->addr_list; @addresses = map { inet_ntoa($_) } @$addr_ref; } print "$name => @addresses\n"; Here's the output: % hostaddrs www.ora.com See Also
The |
|