掃描服務器ip開放端口,用線程池ThreadPoolExecutor,i7的cpu可以開到600個左右現成,大概20s左右掃描完65535個端口,根據電腦配置適當降低線程數
#!/usr/local/python3.6.3/bin/python3.6
# coding = utf-8
import socket
import datetime
import re
from concurrent.futures import ThreadPoolExecutor, wait
DEBUG = False
# 判斷ip地址輸入是否符合規范
def check_ip(ipAddr):
compile_ip = re.compile('^(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|[1-9])\.(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|\d)\.(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|\d)\.(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|\d)$')
if compile_ip.match(ipAddr):
return True
else:
return False
# 掃描端口程序
def portscan(ip, port):
try:
s = socket.socket()
s.settimeout(0.2)
s.connect((ip, port))
openstr = f'[+] {ip} port:{port} open'
print(openstr)
except Exception as e:
if DEBUG is True:
print(ip + str(port) + str(e))
else:
return f'[+] {ip} port:{port} error'
finally:
s.close
#主程序,利用ThreadPoolExecutor創建600個線程同時掃描端口
def main():
while True:
ip = input("請輸入ip地址:")
if check_ip(ip):
start_time = datetime.datetime.now()
executor = ThreadPoolExecutor(max_workers=600)
t = [executor.submit(portscan, ip, n) for n in range(1, 65536)]
if wait(t, return_when='ALL_COMPLETED'):
end_time = datetime.datetime.now()
print("掃描完成,用時:", (end_time - start_time).seconds)
break
if __name__ == '__main__':
main()
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

