Information Technology Grimoire

Version .0.0.1

IT Notes from various projects because I forget, and hopefully they help you too.

host to ip

the script

#!/usr/bin/perl
use warnings;
use strict;
use Socket;
# Finds ip of a list of hosts
# type:  perl ipfinder.pl > output.csv
# prints:
# 1.1.1.1,a.com
# 2.2.2.2,b.com
# 3.3.3.3,c.com
# some domains, like google.com 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";
}
__DATA__
a.com
b.com
c.com