[英]Python sshtunnel - How to Validate SSH Connection?
使用 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()
我用它來查看隧道是否打開:
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
您可以使用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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.