将带有本地端口转发连接的 Oracle SQL Developer SSH 主机转换为 Python [英] Translate Oracle SQL Developer SSH Host w/ Local Port Forward Connection to Python

查看:119
本文介绍了将带有本地端口转发连接的 Oracle SQL Developer SSH 主机转换为 Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过 SSH 跳转主机(我已在 Oracle SQL Developer 中成功创建的主机)创建到远程服务器的 Python 连接,但无法在 Python 中复制.可以成功连接到 SSH 主机,但由于超时或错误打开隧道而无法将端口转发到远程服务器.可以安全地假设我的代码不正确而不是服务器问题.还需要一个不使用with SSHTunnelForwarder() as server:"方法的解决方案,因为我需要一个类似于 OSD/cx_Oracle 会话的连续会话而不是批处理功能.

I'm trying to create a Python connection to a remote server through an SSH Jump Host (one I've successfully created in Oracle SQL Developer) but can't replicate in Python. Can connect to SSH Host successfully but fail to forward the port to the remote server due to timeout or error opening tunnels. Safe to assume my code is incorrect rather than server issues. Also need a solution that doesn't use the "with SSHTunnelForwarder() as server:" approach because I need a continuous session similar to OSD/cx_Oracle session rather than a batch processing function.

此处(和其他地方)使用 paramiko、sshtunnel 和 cx_Oracle 提供的类似示例对我不起作用.许多其他示例不需要(或至少明确指定)远程服务器的单独登录凭据.我希望关键的不明确部分是使用哪个本地主机 + 端口,我的 SQL Developer 连接没有明确要求(尽管我已经尝试使用 OSD 选择的端口,但不是同时使用).

Similar examples provided here (and elsewhere) using paramiko, sshtunnel, and cx_Oracle haven't worked for me. Many other examples don't require (or at least clearly specify) separate login credentials for the remote server. I expect the critical unclear piece is which local host + port to use, which my SQL Developer connection doesn't require explicitly (although I've tried using the ports OSD chooses, not at the same time).

我认为最接近的比赛是 paramiko-port 的最佳答案-forwarding-around-a-nat-router

Closest match I think was best answer from paramiko-port-forwarding-around-a-nat-router

SSH 主机
- 主机 = 代理主机名
- 端口 = proxy_port = 22
- 用户名 = proxy_username
- 密码 = proxy_password

SSH Host
- host = proxy_hostname
- port = proxy_port = 22
- username = proxy_username
- password = proxy_password

本地端口转发
- 主机 = 远程主机名
- 端口 = 远程端口 = 1521
- 自动分配本地端口 = True

Local Port Forward
- host = remote_hostname
- port = remote_port = 1521
- automatically assign local port = True

连接
- 用户名 = 远程用户名
- 密码 = remote_password
- 连接类型 = SSH
- SID = remote_server_sid

Connection
- username = remote_username
- password = remote_password
- connection type = SSH
- SID = remote_server_sid

即来自 paramiko-port-forwarding-的类似代码绕过nat路由器

import paramiko
from paramiko import SSHClient

# Instantiate a client and connect to the proxy server
proxy_client = SSHClient()
proxy_client.connect(
    proxy_hostname,
    port=proxy_port,
    username=proxy_username,
    password=proxy_password)

# Get the client's transport and open a `direct-tcpip` channel passing
# the destination hostname:port and the local hostname:port
transport = proxy_client.get_transport()
dest_addr = (remote_hostname,remote_port)
local_addr = ('localhost',55587)
channel = transport.open_channel("direct-tcpip", dest_addr, local_addr)

# Create a NEW client and pass this channel to it as the `sock` (along 
# with whatever credentials you need to auth into your REMOTE box
remote_client = SSHClient()
remote_client.connect(
    'localhost',
    port=55587, 
    username=remote_username, 
    password=remote_password,
    sock=channel)

我得到的不是与远程服务器的连接

Rather than a connection to the remote server I get

transport.py in start_client()
SSHException: Error reading SSH protocol banner

推荐答案

解决方案

终于找到解决办法了!类似于 OSD 的自动本地端口分配,不需要 SSHTunnelForwarder 的 with 语句.希望它可以帮助其他人 - 使用问题的 OSD 输入变量...

Solution

Finally figured out a solution! Analogous to OSD's automatic local port assignment and doesn't require SSHTunnelForwarder's with statement. Hope it can help someone else- use the question's OSD input variables with...

from sshtunnel import SSHTunnelForwarder
import cx_Oracle 

server=SSHTunnelForwarder(
    (proxy_hostname,proxy_port),
    ssh_username=proxy_username,
    ssh_password=proxy_password,
    remote_bind_address=(remote_hostname,remote_port))

server.start()
db=cx_Oracle.connect('%s/%s@%s:%s/%s'%(remote_username,remote_password,'localhost',server.local_bind_port,remote_server_sid))
# do something with db
server.close()

这篇关于将带有本地端口转发连接的 Oracle SQL Developer SSH 主机转换为 Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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