Created
December 5, 2014 23:29
-
-
Save anonymous/1967853110ed99aa627e to your computer and use it in GitHub Desktop.
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
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