实现
def get_ssh_tunnel(middle_name, remote_name, localport):
session = models.get_db_session()
middle_host = session.query(models.Server).filter(models.Server.name == middle_name).one()
remote_host = session.query(models.Server).filter(models.Server.name == remote_name).one()
return SSHTunnelForwarder(
(middle_host.host, int(middle_host.port)),
ssh_username=middle_host.username,
ssh_password=middle_host.password,
remote_bind_address=(remote_host.host, int(remote_host.port)),
local_bind_address=("0.0.0.0", int(localport))
), middle_host, remote_host
参考
https://pypi.org/project/sshtunnel/