Created
July 6, 2011 23:17
-
-
Save florinpatrascu/1068577 to your computer and use it in GitHub Desktop.
Count the occurrence of an IP address in a given list of IP addresses and resolve it to its hostname.
This file contains hidden or 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
#require "rubygems" | |
require 'socket' | |
# Count the occurrence of an IP address in a given list of | |
# IP addresses and resolve it to its hostname. | |
# | |
# (c)2011 Florin T.PATRASCU | |
Socket.do_not_reverse_lookup = false | |
ip_addresses = Array.new DATA.read.split | |
h = ip_addresses.inject( Hash.new(0)) {|h,x| h[x]+=1;h} | |
puts " #{ip_addresses.count} total records." | |
puts " sessions\t IP\t\t\t hostname" | |
h.sort{|a,b| a[1]<=>b[1]}.reverse.each do |e| | |
s = Socket.getaddrinfo( e[0], 0, Socket::AF_UNSPEC, | |
Socket::SOCK_STREAM, nil, Socket::AI_CANONNAME) | |
puts "\t#{e[1]}\t#{e[0]};\t#{"\t" unless e[0].size > 14}#{s[0][2]}" | |
end | |
# some IP addresses used for test | |
__END__ | |
10.167.27.192 | |
108.7.242.40 | |
109.173.122.7 | |
110.32.0.137 | |
111.118.164.210 | |
111.118.164.210 | |
112.202.147.110 | |
112.202.147.110 | |
112.202.147.110 | |
115.186.146.13 | |
115.248.190.186 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I welcome any suggestions for improvement.