python 多线程得到域名ip

得到一个域名列表对应的ip

1
python sortout.py target.txt
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#! /usr/bin/env python
# coding=utf-8
#
# Uaeage:
# python sortout.py targets/jju.edu.cn
#
import sys
import socket
import threading

def check(filePath):
targets = open(filePath,'r')
ipfile = filePath + '.ip'
ipsfile = filePath + '.ips'
wryfile = filePath + '.wrytarget'
ip = open(ipfile, "w")
ips = open(ipsfile, "w")
wry = open(wryfile,'w')

targetips = []
targetlist = targets.readlines()
targetall = {}
targetfinal = {}
for target in targetlist:
target = target.strip()
try:
targetip = socket.gethostbyname(target)
print target + ':' + targetip
if targetip not in targetips:
targetips.append(targetip)
targetall[target] = targetip
except:
print target + " didn't find host!"
# 记录未找到ip的域名
try:
wry.writelines(target + 'n')
except:
print "write to file wrytarget failed !"


targetips.sort()

# 写入文件
for target in targetlist:
target = target.strip()
if targetall.has_key(target):
if targetfinal.has_key(targetall[target]):
targetfinal[targetall[target]] = targetfinal[targetall[target]] + ' , ' + target
else:
targetfinal[targetall[target]] = target
else:
pass

try:
items = targetfinal.items()
items.sort()
for key,value in items:
print key, value
ips.writelines(key + ':' + value + 'n')
except:
print 'write file .ips failed !'

try:
for targetip in targetips:
ip.writelines(targetip + "nn")
except:
print 'write file .ip failed !'


targets.close()
ip.close()
ips.close()
wry.close()


def getIp(filePath):
try:
t = threading.Thread(target=check, args=(filePath,))
t.start()
t.join()
print "complete !"
except:
print "ERROR !"


if __name__=='__main__':
if len(sys.argv)==1:
print u"必须输入文件路径,最好不要使用中文路径"
else:
getIp('/root/tools/1.information/BBScan/targets/jju.edu.cn')