Last active
August 29, 2015 14:10
-
-
Save diogomonica/6b6dff2c9ca474cd0a3e to your computer and use it in GitHub Desktop.
Resolve IPs for a CSV with hosts on the last column
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /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