请给我一段python3代码,用于端口扫描。其中有多个进程,发包进程只进行发包,监听端口操作由收包进程做
时间: 2024-03-13 20:44:51 浏览: 122
好的,以下是一个使用Scapy进行多进程端口扫描的Python 3代码示例,其中发包进程只负责发送TCP SYN包,收包进程负责监听扫描结果并将其打印出来:
```python
from scapy.all import *
from multiprocessing import Process, Queue
def scan(ip, port, result_queue):
packet = IP(dst=ip)/TCP(dport=port, flags='S')
response = sr1(packet, timeout=1, verbose=0)
if response:
if response.haslayer(TCP) and response.getlayer(TCP).flags == 0x12:
result_queue.put((ip, port))
elif response.haslayer(ICMP) and int(response.getlayer(ICMP).type) == 3:
pass
def listen(result_queue):
while True:
if not result_queue.empty():
ip, port = result_queue.get()
print(f"Port {port} is open on {ip}")
if __name__ == '__main__':
ip = '192.168.1.1'
ports = range(1, 1001)
result_queue = Queue()
# Start listening process
listening_process = Process(target=listen, args=(result_queue,))
listening_process.start()
# Start scanning processes
processes = []
for port in ports:
process = Process(target=scan, args=(ip, port, result_queue))
process.start()
processes.append(process)
# Wait for scanning processes to finish
for process in processes:
process.join()
# Stop listening process
listening_process.terminate()
```
在这个示例中,我们将扫描任务拆分成多个进程,每个进程只负责扫描一个端口。发包进程只发送TCP SYN包,并且将扫描结果添加到结果队列中。监听进程负责从结果队列中获取结果并将其打印出来。在扫描过程中,我们依次扫描所有端口,因此不需要使用端口范围来划分扫描任务。
阅读全文