def run(self, count):
# Get unique number of available TCP ports on the system
sshd_ports = []
for try_port in random.sample(range(10000, 11000), count):
# If the port is already in use, skip it.
while can_connect(try_port):
try_port += 1
sshd_ports.append(try_port)
# Run sshd servers in parallel, cleaning up when the yield returns.
subprocesses = []
for port in sshd_ports:
subprocesses.append(subprocess.Popen(
['/usr/sbin/sshd', '-p{}'.format(port), '-f{}'.format(self.sshd_config_path), '-e', '-D'],
cwd=str(self.tmpdir)))
# Wait for the ssh servers to come up
@retry(stop_max_delay=1000, retry_on_result=lambda x: x is False)
def check_server(port):
return can_connect(port)
for port in sshd_ports:
check_server(port)
yield sshd_ports
# Stop all the subproceses. They are ephemeral temporary SSH connections, no point in being nice
# with SIGTERM.
for s in subprocesses:
s.kill()
评论列表
文章目录