Resolving hostnames with Perl


I recently added name resolution support to ldap-stats.pl. This was super easy to do, and only required three lines of Perl code:

### Import the required modules
use Socket;

### Convert the IP address string to an Internet address
my $ipaddr = inet_aton($index);

### Resolve the IP address to a hostname
my $host = gethostbyaddr($ipaddr, AF_INET);

Once the conversion and resolution complete, the name will be available in the $host scalar variable. Giddie up!

This article was posted by Matty on 2005-10-07 14:43:00 -0400 -0400