Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python sshtunnel-如何验证SSH连接?_Python_Python 3.x_Ssh_Ssh Tunnel - Fatal编程技术网

Python sshtunnel-如何验证SSH连接?

Python sshtunnel-如何验证SSH连接?,python,python-3.x,ssh,ssh-tunnel,Python,Python 3.x,Ssh,Ssh Tunnel,使用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() 您可以使用try except块来执行此操作 ssh

使用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()

您可以使用
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)

我用这个来查看隧道是否打开:

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

你好,菜鸟!让我用另一种方式问我的问题。如果我创建了一个SSHTunnelForwarder,启动它,并没有收到任何异常:现在有没有办法验证我是否已成功连接?我目前正在阅读sshtunnelgithub上的_check_tunnel函数,sshtunnel.py,看看它们是如何实现的。我想可能有一种简单的方法来验证连接。“如果我可以执行X,我知道ssh隧道Y已成功建立。”