【发布时间】:2018-12-01 07:24:06
【问题描述】:
Moin!
情况:通过 jump.host 连接到 destination.host 并在 destination.host 上运行命令,连接到another.host 的背景(在此主机上需要我的 ssh 密钥)。
方案:client --> jump.host -->destination.host --- remote_command with ssh key on the other host --> another.host
#!/usr/bin/python
import paramiko
jumpHost=paramiko.SSHClient()
sshKey = paramiko.RSAKey.from_private_key_file('path.to.key/file', password = 'the.passphrase')
jumpHost.set_missing_host_key_policy(paramiko.AutoAddPolicy())
jumpHost.connect('jump.hostname',username='foo', pkey = sshKey)
jumpHostTransport = jumpHost.get_transport()
dest_addr = ('destination.hostname', 22)
local_addr = ('jump.hostname', 22)
jumpHostChannel = jumpHostTransport.open_channel("direct-tcpip", dest_addr, local_addr)
destHost=paramiko.SSHClient()
destHost.set_missing_host_key_policy(paramiko.AutoAddPolicy())
destHost.connect('destination.hostname', username='foo', sock=jumpHostChannel, pkey=sshKey)
destHostAgentSession = destHost.get_transport().open_session()
paramiko.agent.AgentRequestHandler(destHostAgentSession)
stdin, stderr, stdout = destHost.exec_command("my.command.which.connects.to.another.host")
print(stdout.read())
print(stderr.read())
destHost.close()
jumpHost.close()
如果在 destination.host 上运行“本地”命令,上述代码运行良好 - 例如uname, whoami, hostname, ls 等等...但是如果我运行一个命令,它在后台连接到另一个需要我的 ssh 密钥的主机,错误中引发的代码:
引发 AuthenticationException("无法连接到 SSH 代理") paramiko.ssh_exception.AuthenticationException:无法连接到 SSH 代理
如果我通过 Putty 在同一条链上连接,它运行良好。
谁能给我一个提示来解决我的问题?
提前谢谢。
【问题讨论】:
标签: python python-3.x ssh paramiko ssh-tunnel