通过Python sshtunnel转发SSH密钥
问题与类似,但使用通过Python sshtunnel转发SSH密钥,python,ssh,ssh-tunnel,Python,Ssh,Ssh Tunnel,问题与类似,但使用sshtunnel库,而不是直接调用paramariko 基本上,我想以编程的方式重复描述的场景(在使用代理转发的公钥访问部分)。我的想法是在服务器1上运行一个后端(参见前面的部分,它有图形化的方案)。此后端需要连接服务器2以使用我的身份验证密钥检索某些数据 我以前使用用户名/密码,现在改用公钥。当我使用普通密码时,它是这样的: from sshtunnel import SSHTunnelForwarder APP_CONFIG = {} # here should be
sshtunnel
库,而不是直接调用paramariko
基本上,我想以编程的方式重复描述的场景(在使用代理转发的公钥访问部分)。我的想法是在服务器1上运行一个后端(参见前面的部分,它有图形化的方案)。此后端需要连接服务器2以使用我的身份验证密钥检索某些数据
我以前使用用户名/密码,现在改用公钥。当我使用普通密码时,它是这样的:
from sshtunnel import SSHTunnelForwarder
APP_CONFIG = {} # here should be connection configuration
def build_ssh_tunnel_config(app_config):
cfg = app_config
return {
'ssh_address_or_host': (cfg['SSH_TUNNEL_HOST'],
int(cfg['SSH_TUNNEL_PORT'])),
'ssh_password': cfg['SSH_TUNNEL_PASSWORD'],
'ssh_username': cfg['SSH_TUNNEL_USERNAME'],
'remote_bind_address': (cfg['SSH_TUNNEL_BIND_HOST'],
int(cfg['SSH_TUNNEL_BIND_PORT']))}
cfg = build_ssh_tunnel_config(APP_CONFIG)
server = SSHTunnelForwarder(**cfg)
server.start()
# communicate with host
但是,我应该使用哪些类/参数来重复相同的过程,而不显式使用用户名/密码,而是使用密钥转发
我知道如何通过终端“手动”操作,即启用密钥转发并按预期工作
更新
以下是通过server1从我的homepc连接到server2时的操作方法:
SSH_TUNNEL_CONFIG = {
'ssh_address_or_host': (server1_address, server1_port),
'remote_bind_address': (server2_address, server2_port),
'ssh_pkey': pkey_path
'ssh_private_key_password': os.environ.get('tunnel_password'),
'ssh_username': username
}
但是我的server1主机上没有pkey\u路径
文件,不想在那里存储我的私钥。这就是为什么需要密钥转发
我在SSH配置方面不是很有经验,因此可能做了一些错误的事情,或者没有做得那么简单