Created
April 18, 2012 20:37
-
-
Save j3tm0t0/2416408 to your computer and use it in GitHub Desktop.
mapper to check domain of 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
#!/usr/bin/env ruby | |
require 'socket' | |
ARGF.each do |line| | |
line = line.chomp | |
data = line.split(/\t/) | |
ip = data[0] | |
# resolve IP to hostname and split by '.' | |
hostname=Socket.getnameinfo([nil, nil, nil, ip])[0] | |
d=hostname.split('.') | |
if ip == hostname # IP was not resolved to hostname | |
if d[0] == "10" | |
domain = "*.*.*.10" # assume 10.0.0.0/8 as 10.*.*.* | |
else | |
domain = ['*',d[2],d[1],d[0]].join(".") | |
end | |
else | |
tld=d.last | |
secd=d[d.length-2] | |
thrd=d[d.length-3] | |
# check if it is TLD/generic jp domain or not | |
if tld =~/^(com|net|org|info|biz|name|pro|aero|coop|museum|jobs|travel|mail|cat|post|asia|mobi|tel|xxx)$/ | |
domain = [tld,secd].join(".") | |
elsif d.last == "jp" && ( secd.length > 2) | |
domain = [tld,secd].join(".") | |
else | |
domain = [tld,secd,thrd].join(".") | |
end | |
end | |
key = domain.split(".").reverse.join(".") | |
value = data[1] | |
# output to STDOUT | |
# | |
puts key + "\t" + value.to_s | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment