过完年了,重新开始板砖了。最近客户公司要进行全面的安全检测,于是给我们了一份很全的域名列表,然后让我们从外网进行简单的测试,但是这个域名实在是太多太多了,而且公司的域名很打一部分都是解析到内网的,外网根本无法访问,于是写了个简单的小脚本进行筛选,主要功能就是获取域名所对应的ip以及剔除解析到内网的域名。用队列和多线程的结构,速度能快很多,毕竟量上去了单线程还是很慢的。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2019/02/25
# @Author : XDN01
# @Site : www.raosong.cc
# @File : get_public_ip.py
import socket
import csv
import requests
import threading
import queue
q = queue.Queue()
threading_num = 50
with open("alldomain.txt" , "r") as f:
filedata = f.readlines()
q.put(filedata)
f.close()
def run():
while not q.empty():
filedata = q.get()
for i in filedata:
url = str(i).replace("\n",'')
#print(url)
try:
myaddr = socket.getaddrinfo(url, 'http')
#print(str(myaddr[0][4][0])[:3])
if str(myaddr[0][4][0])[:3] != str(172):
#print(url+" "+str(myaddr[0][4][0]))
#value_array.append(url).append(str(myaddr[0][4][0]))
with open("public_domain.csv","a") as fw:
writer = csv.writer(fw)
writer.writerow([url,str(myaddr[0][4][0])])
except:
#print('can't open')
pass
#f.close()
if __name__ =="__main__":
print('begin')
for i in range(threading_num):
t = threading.Thread(target=run)
t.start()
t.join()
print('over')