提问人:Z4R1 提问时间:11/1/2023 最后编辑:Z4R1 更新时间:11/1/2023 访问量:83
在 Linux 服务器中侦听 60000 个端口
listen 60000 ports in linux server
问:
我在 Linux 中收听多个端口时遇到问题。我想侦听此服务器中从 20 到 63350 的所有端口。但是,我一次只能侦听 4700 个端口。这是我是如何做到的。
我正在研究网络望远镜的东西(又名暗网,分组望远镜)。我需要确保所有端口都被监听,或者当我尝试 tcpdump 以捕获具有非侦听端口的数据包时,我的服务器将拒绝请求,这会阻止我捕获数据包。RST
SYN
我该如何实现?我应该修改脚本还是修改服务器的设置?或者我可能需要使用 13 台服务器来侦听 60000 个端口。
- 我修改了 Linux 的文件限制,允许我同时打开更多文件。
ulimit -n 100000
- 编写脚本以侦听端口。我试过了.但他们最多只能监听 4700 个端口。
threading
multiprocessing
// When this script listens on more than 4700 ports, "too many open files" error pops up.
#!usr/bin/python3
import socket
import threading
host = ''
start_port = 10
end_port = 2010
def port_listen(port):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.bind((host, port))
print(f'bound success(port: {port})')
s.listen(2000)
except Exception as e:
print(f'failed to bind(port: {port})\nerror msg: {e}')
while True:
conn, addr = s.accept()
conn.close()
threads = []
for port in range(start_port, end_port):
thread = threading.Thread(target = port_listen, args=(port,))
threads.append(thread)
thread.start()
for thread in threads:
thread. Join()
// When this script listen more than 1000 ports, the server crashes with the extremely high consume of cpu and memory. Error msg is "can't start new process".
#!usr/bin/python3
import socket
import multiprocessing
host = ''
start_port = 23
end_port = 50
def listen(port):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.bind((host, port))
s.listen(100)
except Exception as e:
print(f"port {port} failed to bound: {e}")
return
while True:
conn, addr = s.accept()
print(f"{port} connected!")
conn.close()
processes = []
for port in range(start_port, end_port + 1):
process = multiprocessing.Process(target = listen, args = (port,))
processes.append(process)
process.start()
for process in processes:
process. Join()
答: 暂无答案
评论
select()