Skip to content

Instantly share code, notes, and snippets.

Created December 5, 2014 23:29
Show Gist options
  • Save anonymous/1967853110ed99aa627e to your computer and use it in GitHub Desktop.
Save anonymous/1967853110ed99aa627e to your computer and use it in GitHub Desktop.
def ipString(line)
match = line.scan(/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/)
match.each do |x|
split_array = x.split(".")
split_array.map{|str| str.rjust(3, '0') }.join '.'
end
end
unless ARGV.size == 1
puts "Invalid number of arguments?"
else
ip_hash = Hash.new(1)
in_file = File.new(ARGV[0], 'r')
in_file.each_line do |line|
ipString(line).each do |ip_address|
if ip_hash.key?(ip_address)
ip_hash[ip_address] += 1
else
ip_hash[ip_address] = 1
end
end
end
end
total =
puts "IP Address Total Count"
ip_hash.each do |ip_address, count|
puts "#{ip_address.to_s} #{count} times"
end
puts "-----------------------------------------------"
puts "Grand Total: #{total}"
#Format IP before storing as key
#Split by . to check each octet - each will be string, need an int
#use to_s.rjust - takes each split, format to leading 0 with max of 3 digits
#combine back together as one
#Store as key
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment