直接上代码:
import time
import socket
import ipaddress
import re
from time import sleep
from multiprocessing import Pool, Manager
def TCP_connect(ip,port):
"""模拟TCP连接"""
TCP_sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
TCP_sock.settimeout(3) #设置连接超时
try:
result = TCP_sock.connect_ex((ip,port))
#print(port)
if result == 0:
print("[+]%s opening\t"%str(port))
else:
pass
TCP_sock.close()
except socket.error as e:
pass
def test(ip):
start_time = time.time()
print(ip)
p = Pool(600)#600的线程对于端口扫描一般不会炸
for i in range(1,65536):
p.apply_async(TCP_connect,args=(ip,i))
p.close()
p.join()
end_time = time.time()
print('Total %f second' % (end_time - start_time))
if __name__ == '__main__':
#设置socket超时不一样,结果可能不一样,可能涉及网络延迟等,理论就是这样.
net4 = ipaddress.ip_network('192.168.1.1/24', strict=False)#C段扫描
#for i in net4.hosts():
#test(str(i))
test('192.168.1.1')#单个扫描