paramiko中的proxycommand

时间:2017-05-18 16:15:10

标签: python python-3.x paramiko

我正在尝试使用python中的paramiko进行简单的代理命令。

基本上我正在尝试复制这个ssh命令的行为:

ssh -i ~/.ssh/destination_key user@destination.test.internal -o 'ProxyCommand ssh -i ~/.ssh/jumpbox_key -W %h:%p user@jumpbox.test.internal'

以上按预期工作amd连接到destination.test.internal。 我试图在python中做同样的事情,在同一个盒子上有以下内容:

#!/usr/bin/python3
import paramiko
import argparse

addresses = ["destination.test.internal"];

ssh = paramiko.SSHClient()
ssh.load_system_host_keys()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

proxy = paramiko.ProxyCommand("ssh -i ~/.ssh/jumpbox_key -W %h:%p user@jumpbox.test.internal")

for address in addresses:
    #Connect over ssh to each server
    try:
        ssh.connect(address , username='user', key_filename="~/.ssh/destination_key", sock = proxy )       
    except paramiko.AuthenticationException:
        print ("Authentication Failed")
    except paramiko.SSHException:
        print ("Connection Failed")


    stdin,stdout,stderr = ssh.exec_command('ls -l')
    print (stdout.readlines())

    ssh.close()

毋庸置疑,这不起作用。它失败了:

Traceback (most recent call last):
Exception: Error reading SSH protocol banner
  File "/usr/local/lib/python3.5/dist-packages/paramiko/transport.py", line 1893, in _check_banner
    buf = self.packetizer.readline(timeout)
  File "/usr/local/lib/python3.5/dist-packages/paramiko/packet.py", line 331, in readline
    buf += self._read_timeout(timeout)
  File "/usr/local/lib/python3.5/dist-packages/paramiko/packet.py", line 501, in _read_timeout
    raise socket.timeout()
socket.timeout

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/paramiko/transport.py", line 1749, in run
    self._check_banner()
  File "/usr/local/lib/python3.5/dist-packages/paramiko/transport.py", line 1897, in _check_banner
    raise SSHException('Error reading SSH protocol banner' + str(e))
paramiko.ssh_exception.SSHException: Error reading SSH protocol banner

Connection Failed
Traceback (most recent call last):
  File "./log_file_fix.py", line 31, in <module>
    stdin,stdout,stderr = ssh.exec_command('ls -l')
  File "/usr/local/lib/python3.5/dist-packages/paramiko/client.py", line 436, in exec_command
    chan = self._transport.open_session(timeout=timeout)
  File "/usr/local/lib/python3.5/dist-packages/paramiko/transport.py", line 716, in open_session
    timeout=timeout)
  File "/usr/local/lib/python3.5/dist-packages/paramiko/transport.py", line 800, in open_channel
    raise SSHException('SSH session not active')
paramiko.ssh_exception.SSHException: SSH session not active

但是我不确定我哪里出错了。

0 个答案:

没有答案