python - 将 ssh 隧道命令翻译成 SSHTunnelForwarder 代码?
问题描述
我正在尝试将此 ssh 隧道命令转换为使用 python SSHTunnelForwarder。这有效:
ssh -i mypk -N -L 5901:localhost:5432 user@100.0.0.100
我试过这个:
with SSHTunnelForwarder(
('localhost', 5901),
ssh_username='user',
ssh_private_key=path_to_mypk,
remote_bind_address=('100.0.0.100', 5432)
但是出现错误(在它暂停几秒钟后):“无法建立与 SSH 网关的会话”。我应该做什么?
解决方案
我缺少的部分是从服务器的角度使用“localhost”。以下作品:
with SSHTunnelForwarder(
('100.0.0.100', 22),
ssh_username='user',
ssh_private_key=path_to_mypk,
remote_bind_address=('localhost', 5432), # localhost from server's perspective
local_bind_address=('localhost', 5901) # any available port
)
推荐阅读
- python-3.x - Pickle 包中的“TypeError:load() 最多接受 1 个位置参数(给定 2 个)”
- java - JSHELL JDK API:抛出异常时拒绝片段
- c++ - 传入模板类
- javascript - 无法导入 JavaScript 类文件
- swift - 扫描目录中的文件(同时忽略目录)
- javascript - 使用来自另一个组件的 href 转发 Ref
- wpf - 将 MenuItem 的 #items 绑定到 CompositeCollection 中的可见性
- azure-devops - 使用 azure devops rest api 跳过阶段不起作用
- python-3.x - 如何优化我的 Python 反向图像搜索以限制到特定域?
- javascript - 一种使用JS在两个日期之间生成一个长度为30的随机时间数组的方法?