#!/usr/bin/ruby -w
# This script takes a list of hostnames (one per line), resolves their address,
# then creates a new comma-seperated value file with each hostname followed by
# its IP address.

require 'resolv'

input = File.open("hosts.csv", "r")
output = File.open("hosts2.csv", "w")

input.each_line {|line|
    line = line.chop
    Resolv::DNS.new.each_address("#{line}") {|addr|
        output.puts "#{line},#{addr}"
    }
}

The above script can be extended using pipes to combine multiple commands such as Sed, Awk, Nmap, etc. The example below shows how you can take a given list of hostnames, resolve their IP addresses, remove duplicate IP’s, perform an Nmap scan and then output the results to an XML file.

$ ruby resolve.rb
$ sed 's/,/\ /' hosts2.csv | awk '{print $2}' | uniq | nmap -sS -A -T4 -iL - -oX scan_results.xml