Welcome, guest | Sign In | My Account | Store | Cart

Parse a text file containing a list of IP Addresses or hostnames (one per line) and print out the corresponding IP Address or hostname resolved by DNS.

Perl, 30 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#!/usr/bin/perl
###################################################
# This script will parse a text file containing a
# list of IP Addresses or hostnames (one per line).
#
# Usage: mass_nslookup.pl list_of_hosts.txt
#
###################################################

use warnings;
use strict;
use Net::Nslookup;

open (FILE, $ARGV[0]);

while (<FILE>){
	my $host = $_;
	chomp($host);
	next unless $host;
	resolve_dns($host);
}

close FILE;
sub resolve_dns{
	my $host = shift;
	my $dns = nslookup($host);
	if ($dns){
		print "$host,$dns\n";
	}
}
Created by Brett Carroll on Thu, 31 Jul 2014 (Apache)
Perl recipes (17)
Brett Carroll's recipes (3)

Required Modules

  • (none specified)

Other Information and Tasks