在使用Python时,我如何验证SSH连接是否成功?
server = SSHTunnelForwarder(
(host_or_ip, 22),
ssh_username = "ssh_username",
ssh_private_key = path_to_key,
remote_bind_address = (another_host_or_ip, 5432)
)
server.start()发布于 2020-05-13 22:38:12
我使用此命令查看隧道是否已打开:
server.skip_tunnel_checkup = False
server.start()
server.check_tunnels()
print(server.tunnel_is_up, flush=True)在日志中,您将看到:
{('0.0.0.0', 44004): True} # this tunnel is up
{('0.0.0.0', 44004): False} # this tunnel is not up发布于 2018-08-29 14:18:52
您可以使用try-except块来做到这一点。
ssh_address_or_host = ...
ssh_username = ...
ssh_password = ...
remote_bind_address = ...
try:
with SSHTunnelForwarder(
ssh_address_or_host = ssh_address_or_host,
ssh_username = ssh_username,
ssh_password = ssh_password,
remote_bind_address = remote_bind_address
) as server:
print("connected")
except Exception as e:
print("Non connected because: " + e)https://stackoverflow.com/questions/52070456
复制相似问题