Skip to content

Instantly share code, notes, and snippets.

@diogomonica
Last active August 29, 2015 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save diogomonica/6b6dff2c9ca474cd0a3e to your computer and use it in GitHub Desktop.
Save diogomonica/6b6dff2c9ca474cd0a3e to your computer and use it in GitHub Desktop.
Resolve IPs for a CSV with hosts on the last column
#! /bin/env ruby
require 'socket'
if ARGV.length != 1
puts "# Usage: #{$0} FILNAME.csv"
exit(-1)
end
input_filename = ARGV.first
unless File.exist? input_filename
puts "# File: #{filename} does not exist."
puts "# Usage: #{$0} FILNAME.csv"
exit(-1)
end
first_timestamp = nil
File.readlines(input_filename).each do |line|
split_line = line.split(',')
if split_line.length == 3 && split_line.last != nil
first_timestamp = split_line.first unless first_timestamp
hostname = split_line.last.chomp
begin
ip_address = IPSocket.getaddress(hostname)
split_line[2] = ip_address
split_line[0] = split_line[0].to_f - first_timestamp.to_f
puts split_line.join(',')
rescue Exception => e
puts "Error resolving: #{hostname}: #{e}"
end
else
puts "Skipping invalid line: #{line}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment