Python:远程服务器关闭后自动重新连接 ssh 隧道 [英] Python: Automatically reconnect ssh tunnel after remote server gone down

查看:63
本文介绍了Python:远程服务器关闭后自动重新连接 ssh 隧道的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在 Python 中实现了一个建立 ssh 隧道的功能,因此我可以在 NAT 后面的数据库中插入数据(没有可用的端口转发).

import paramiko从 sshtunnel 导入 SSHTunnelForwarderdef fnc_ssh_tunnel():尝试:sshServer = SSHTunnelForwarder((SSH_IP, SSH_PORT),ssh_username = SSH_USER,ssh_password = SSH_PASS,set_keepalive = float(SSH_KEEP_ALIVE),remote_bind_address = (DB_IP, DB_PORT),)sshServer.start()localPort = sshServer.local_bind_portlogPrint("SSH Tunnel Started to " + str(sshServer.tunnel_bindings), DEBUG_ID)sshServer.check_tunnels()返回本地端口除了作为 e 的例外:logPrint("SSH 隧道错误", DEBUG_ID)日志打印(e,DEBUG_ID)放弃()

现在,这工作得很好.通过这个实现,我稍后会在 localhost:localport(localportfnc_ssh_tunnel() 返回)上建立到数据库的连接.

如果由于某种原因,DB 所在的远程 PC 重新启动,就会出现问题.如果是这样,PC 上的 SSH 服务器将断开所有连接,因此我的 ssh 隧道也将关闭.

要从这种情况中恢复过来,我需要重新启动 Python 程序:这样,隧道将重新建立.

如果远程服务器关闭->启动,是否有一种巧妙的方法可以重新建立 ssh 隧道?

谢谢!

解决方案

我面临同样的问题,这是我的解决方案:

启动服务器后,继续检查服务器是否处于活动状态,如果不是,则重新启动.

def get_server():# 定义ssh服务器并返回服务器 = get_server()服务器启动()而真:如果(服务器.is_active):打印(活着..." +(time.ctime()))别的:打印(重新连接..." + time.ctime())服务器停止()服务器 = get_server()服务器启动()时间.睡眠(60)

I have implemented a function to establish an ssh-tunnel in Python, so I can insert data in a DB behind a NAT (no port forwarding available).

import paramiko
from sshtunnel import SSHTunnelForwarder
def fnc_ssh_tunnel():
    try:
        sshServer = SSHTunnelForwarder(
                (SSH_IP, SSH_PORT),
                ssh_username            = SSH_USER,
                ssh_password            = SSH_PASS,
                set_keepalive           = float(SSH_KEEP_ALIVE),
                remote_bind_address     = (DB_IP, DB_PORT),
            )
        sshServer.start()
        localPort = sshServer.local_bind_port
        logPrint("SSH Tunnel Started to " + str(sshServer.tunnel_bindings), DEBUG_ID)
        sshServer.check_tunnels()
        return localPort

    except Exception as e:

        logPrint("Error SSH Tunnel", DEBUG_ID)
        logPrint(e,DEBUG_ID)
        quit()

Now, this is working very well. With this implementation I will later bring up a connection to the DB on localhost:localport (localport being returned by fnc_ssh_tunnel()).

The problem arises if for some reason, the remote PC where the DB resides, reboots. If so, the SSH server on the PC will tear all the connections down and hence also my ssh-tunnel will go down.

To recover from that situation, I need to restart the Python program: with this, the tunnel will reestablish.

Is there a neat way of reestablishing the ssh-tunnel if the remote server went down->up?

Thanks!

解决方案

I am facing the same problem, and here is my solution :

After starting the server, keep checking if the server is active, if it's not, start it again.

def get_server():
    # define ssh server and return it

server = get_server()
server.start()
while True :
    if(server.is_active):
        print("alive... " + (time.ctime()))
    else:
        print("reconnecting... " + time.ctime())
        server.stop()
        server = get_server()
        server.start()
    time.sleep(60)

这篇关于Python:远程服务器关闭后自动重新连接 ssh 隧道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆