0
運行一次,我寫了一個Python腳本來獲取IP地址的列表的證書相匹配的域:for循環中的線程在Python 3
#! /usr/bin/env python3
import ssl
import socket
import argparse
from threading import Thread, Lock
from itertools import islice
class scanThread(Thread):
def __init__(self,iplist, q, hostname, port):
Thread.__init__(self)
self.iplist = iplist
self.hostname = hostname
self.port = port
self.queue = q
def dummy(self,ip):
print("Running dummy")
def checkCert(self, ip):
print('Processing IP: %s' % ip)
ctx = ssl.create_default_context()
s = ctx.wrap_socket(socket.socket(), server_hostname=self.hostname)
try:
s.connect((ip, self.port))
cert = s.getpeercert()
if cert['subjectAltName'][0][1].find(hostname) != -1:
return ip
except (ssl.CertificateError, ssl.SSLError):
print('Ignore: %s' % ip)
finally:
s.close()
return
def run(self):
for ip in self.iplist:
returnIP = self.checkCert(ip)
if returnIP:
self.queue.append(ip)
def main(l, hostname, port):
iplist = []
threads = []
hostPool = []
with open(l,'r') as f:
#while True:
iplist.extend([f.readline().strip() for x in islice(f, 10000)])
#print(iplist)
t = scanThread(iplist, hostPool, hostname, port)
t.start()
threads.append(t)
iplist.clear()
for t in threads:
t.join()
for h in hostPool:
print(h)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("hostname",help="root hostname")
parser.add_argument("-l","--list",required=True, help="IP list for scanning")
parser.add_argument("-p","--port", nargs='?', const=443, default=443, type=int, help="port to scan")
arg = parser.parse_args()
main(arg.list,arg.hostname, arg.port)
我只是註釋掉while
迴路main
功能,因此該腳本創建一個線程並掃描10,000個IP。
以 'google.com' 爲例,它擁有衆多的IP地址全世界:
./google.py -l 443.txt google.com
輸出示例:
Processing IP: 13.76.139.89
Ignore: 13.76.139.89
一些測試後,我敢肯定的是,for ... in
環在scanThread.run()
執行一次。我在這段代碼中做了不適當的事嗎?
相當。我沒有弄清楚這一點。謝謝您的回答。 –
@ J.K是否有效? –
是的。它炒鍋。順便說一下。:) –