IP 去重排序脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# -*- coding:utf-8 -*-
# host_dereplication.py
import sys
import socket
def check_para():
if len(sys.argv) != 2:
print '''
Host IP Dereplication
Author: BLKStone
Date: 2017/07/27
Usage:
python host_dereplication.py ip_list.txt
'''
sys.exit(0)
if __name__ == '__main__':
check_para()
filename = sys.argv[1]
try:
simple = set()
with open(filename) as f:
for line in f.readlines():
simple.add(line.strip('\n').strip())
# print line.strip('\n').strip()
out_filename='simple_'+filename
print "Outfile:", out_filename
items = sorted(simple, key=lambda ip: long(''.join(["%02X" % long(i) for i in ip.split('.')]), 16))
with open(out_filename,'w') as pen:
for ele in items:
pen.write(ele+'\n')
pen.close()
except Exception,e:
print "Exception Details: ",e