Created
April 30, 2014 10:58
-
-
Save miwarin/8085ce9ed7a8c60eb367 to your computer and use it in GitHub Desktop.
denyhostsで記録したIPアドレスをGEO IPで国コードへ変換する
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
# -*- coding: utf-8 -*- | |
# cjheath/geoip https://github.com/cjheath/geoip | |
require 'geoip' | |
def build_hosts(hosts_file) | |
hosts ||= [] | |
lines = File.open(hosts_file).readlines() | |
lines.each{|line| | |
if line =~ /^sshd/ | |
hosts << line.gsub('sshd: ', '').chomp | |
end | |
} | |
return hosts | |
end | |
def build_geoip(geoip_dat) | |
return geoip = GeoIP.new(geoip_dat) | |
end | |
def main(argv) | |
hosts_file = argv[0] | |
geoip_dat = argv[1] | |
hosts = build_hosts(hosts_file) | |
geoip = build_geoip(geoip_dat) | |
countries ||= {} | |
countries.default = 0 | |
hosts.each{|host| | |
c3 = geoip.country(host).country_code3 | |
countries[c3] += 1 | |
} | |
countries.sort {|a, b| (b[1] <=> a[1]) }.each{|c| | |
puts "#{c[0]} #{c[1]}" | |
} | |
end | |
main(ARGV) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment