Skip to content

Instantly share code, notes, and snippets.

@jxinging
Created October 19, 2016 07:27
Show Gist options
  • Save jxinging/9de42f1060a015b5c8a7b50899110995 to your computer and use it in GitHub Desktop.
Save jxinging/9de42f1060a015b5c8a7b50899110995 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python2.7
# coding: utf8
""" """
import sys
import re
import os
from ipip import IP
IP.load(os.path.join('./data/17monipdb.dat'))
IPADDR_RE = re.compile(r'\b(?:[\d]{1,3}\.){3}[\d]{1,3}\b')
def replace_func(match):
orig = match.group()
replace = "%s[%s]" % (orig, " ".join(IP.find(orig).strip().split()))
return replace
def main():
ip_char_set = set("0123456789.")
while 1:
line = []
c = sys.stdin.read(1)
while c in ip_char_set:
line.append(c)
c = sys.stdin.read(1)
line.append(c)
if len(line) >= 7 and "." in line:
sys.stdout.write(IPADDR_RE.sub(replace_func, "".join(line)))
else:
sys.stdout.write("".join(line))
sys.stdout.flush()
if not c:
break
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment