Skip to content

Instantly share code, notes, and snippets.

@florinpatrascu
Created July 6, 2011 23:17
Show Gist options
  • Save florinpatrascu/1068577 to your computer and use it in GitHub Desktop.
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.
#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
@florinpatrascu
Copy link
Author

I welcome any suggestions for improvement.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment