Permalink
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 68 lines (59 sloc) 1.67 KB
#!/usr/bin/env python3
import sys
import re
from myutils import is_internal_ip
from QQWry import QQWry
import ipdb
# v6 version comes from https://stackoverflow.com/a/17871737/296473
ip_re = re.compile(r'''
(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)
|
(?:(?:[0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}
|(?:[0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}
|(?:[0-9a-fA-F]{1,4}:){1,5}(?::[0-9a-fA-F]{1,4}){1,2}
|(?:[0-9a-fA-F]{1,4}:){1,4}(?::[0-9a-fA-F]{1,4}){1,3}
|(?:[0-9a-fA-F]{1,4}:){1,3}(?::[0-9a-fA-F]{1,4}){1,4}
|(?:[0-9a-fA-F]{1,4}:){1,2}(?::[0-9a-fA-F]{1,4}){1,5}
|[0-9a-fA-F]{1,4}:(?:(?::[0-9a-fA-F]{1,4}){1,6})
|:(?:(?::[0-9a-fA-F]{1,4}){1,7}|:)
|(?:[0-9a-fA-F]{1,4}:){1,7}:
|fe80:(?::[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}
|::(?:ffff(?::0{1,4}){0,1}:){0,1}(?:(?:25[0-5]
|(?:2[0-4]
|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(?:25[0-5]
|(?:2[0-4]
|1{0,1}[0-9]){0,1}[0-9])
|(?:[0-9a-fA-F]{1,4}:){1,4}:(?:(?:25[0-5]
|(?:2[0-4]
|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(?:25[0-5]
|(?:2[0-4]
|1{0,1}[0-9]){0,1}[0-9]))''', re.VERBOSE)
Q = QQWry()
D = ipdb.IPDB(ipdb.DEFAULT_FILE_LOCATION)
def lookupip(ip):
if '.' in ip:
return ''.join(Q[ip][2:])
else:
return ' '.join(D.lookup(ip).info)
def transformline(l):
shift = 0
for m in ip_re.finditer(l):
try:
ip = m.group(0)
if is_internal_ip(ip):
continue
addr = lookupip(ip)
inspos = m.end() + shift
l = '%s(%s)%s' % (l[:inspos], addr, l[inspos:])
shift += len(addr) + 2
except:
pass
return l
def main():
for l in sys.stdin:
sys.stdout.write(transformline(l))
if __name__ == '__main__':
try:
main()
except (KeyboardInterrupt, IOError):
pass