Call: (480) 382-8464

Perl Convert List of Hostnames to IP4 Addresses

Instead of having to type nslookup over and over, let’s script a solution that queries a list. We can then redirect the output into a file and have a nice, CSV output – all automated.

Use this perl hostname to ip address converter like this:

perl > output.csv

use warnings;
use strict;
use Socket;

# Finds ip of a list of hosts
# type:  perl > output.csv
# prints:

# some domains, like for example, have multiple IP
# it will only report back the first ($addrs[0])

while (<DATA>) {
	my $uri = $_;
           # remove all spacing
	   $uri =~ s/\s+//g;

        # grab the data using gethostbyname()
	my ($name, $aliases, $addrtype, $length, @addrs) = gethostbyname $uri;

        # unpack $addrs[0], C4 says it's an unsigned int (octet)
	my ($a,$b,$c,$d) =  unpack('C4',$addrs[0]);

        # format the output and print it to stdout
	print "$a.$b.$c.$d,$uri\n";


Tags: ,

  • Brady

    Sometimes if I’m on a server that doesn’t have my scripts available, and I’m on Linux, I’ll just use the one-liner in bash below.
    I’ll be giving a list of hostnames in hosts.txt and need the IP and/or FQDN. Of course it’s not CSV.
    # for i in `cat hosts.txt`;do host $i | gawk {‘print $4,$1’} >> new_list.txt


Leave a Reply

Your email address will not be published. Required fields are marked *

Main Offers/Services

Hosting and website related services offered by Digital Crunch:

Managed VPS Hosting

Server Updates, Module/Software Installs, Firewall, Security, Hosting Management, Wordpress - let us manage it for you

Hosted Email

Setup and Maintain Business Class, Secure Email Hosting including spam protection and 25GB of storage space

Linux Consulting

Server Updates, Module/Software Installs, Firewall, Security, Hosting, Wordpress and Apps, anything related to Linux

Email Marketing Services

We broadcast your content to your list, capture leads for you, follow up on schedules, make more sales for you

Website Maintenance

Content updates, menu changes, graphics changes, adding content, getting links, building traffic, building sales funnels

Hosting Tutorials

Tutorials we've written for other customers that like to get their hands dirty and learn about hosting