首页 > 解决方案 > 如何在 Paramiko 中模拟我们自己的 ssh 二进制文件?

问题描述

我有一个命令,例如

ssh -S myAuthServer hostname

正在尝试创建一个代码片段,但我不确定如何实现该-S部分我目前拥有的代码:

#!/usr/bin/python

import paramiko
from paramiko import SSHClient, SSHConfig, SSHException

paramiko.util.log_to_file("/tmp/script.log")

def getSSHConnection():
    config = SSHConfig()


    host='server1'        

    # setup SSH client
    client = SSHClient()
    client.load_system_host_keys()
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())

    #Check for proxy settings
    proxy = paramiko.ProxyCommand('ssh -S myAuthServer root@%s' % host)
    print "proxy:", proxy

    #Setup the SSH connection
    try:
        if (proxy is None):
            client.connect(host, port=22, username='root')
        else:
            print "-> Using paramiko with proxy"
            client.connect(host, port=22, username='root', sock=proxy)

    except SSHException, ex:
        print ex

    if client:
        stdin, stdout, stderr = client.exec_command('hostname')
        tables=stdout.readlines()

        print "stdin:", stdin
        print "stdout:", stdout
        print "stderr:", stderr
        print "tables:", tables    

    return client

getSSHConnection()

我收到的错误是

INF [20181011-11:14:55.131] thr=1   paramiko.hostkeys: Unable to handle key of type 1024
DEB [20181011-11:14:55.161] thr=2   paramiko.transport: starting thread (client mode): 0x4f35fd0L
DEB [20181011-11:14:55.161] thr=2   paramiko.transport: Local version/idstring: SSH-2.0-paramiko_2.2.1
ERR [20181011-11:15:10.242] thr=2   paramiko.transport: Exception: Error reading SSH protocol banner
ERR [20181011-11:15:10.245] thr=2   paramiko.transport: Traceback (most recent call last):
ERR [20181011-11:15:10.246] thr=2   paramiko.transport:   File "/Users/root/Library/Python/2.7/lib/python/site-packages/paramiko/transport.py", line 1805, in run
ERR [20181011-11:15:10.246] thr=2   paramiko.transport:     self._check_banner()
ERR [20181011-11:15:10.246] thr=2   paramiko.transport:   File "/Users/root/Library/Python/2.7/lib/python/site-packages/paramiko/transport.py", line 1957, in _check_banner
ERR [20181011-11:15:10.246] thr=2   paramiko.transport:     'Error reading SSH protocol banner' + str(e)
ERR [20181011-11:15:10.246] thr=2   paramiko.transport: SSHException: Error reading SSH protocol banner
ERR [20181011-11:15:10.246] thr=2   paramiko.transport:

标签: pythonsshparamiko

解决方案


您不能将 Paramikooursshbinary用作 SSH 实现。你甚至不能让它使用ssh

帕拉米科就是ssh它自己。

它是一样的(正如你现在所知道的那样),就好像你想要ssh使用一样oursshbinary(这没有意义)。您必须让 Paramiko 做该做的事oursshbinary。但我们不知道oursshbinary相比ssh.


回复您的问题的先前版本:

OpenSSH 客户端的-S切换ssh创建了一个连接共享,可以被其他实例ssh或其他 OpenSSH 工具(如sftp)重用。

Paramiko 不支持连接共享。它实际上这样做是没有意义的。

连接共享对工具/应用程序很有意义。工具的一个实例创建一个连接,相同或不同工具的其他实例可以重用该连接。

但是您在脚本中/编程时不需要它。你有你的 SSH 会话实例。您可以在整个脚本/程序中将其用于多种用途。换句话说,您可以共享您的实例。

具体来说,使用 SSH,OpenSSH 连接共享会为共享客户端打开单独的 SSH 通道。使用 Paramiko,您也可以通过同一个 SSH 连接打开多个单独的通道。这就是像Transport.open_sftp_client(or SSHClient.open_sftp), Transport.open_session( SSHClient.invoke_shell, SSHClient.exec_command) 这样的方法。他们都在内部调用Transport.open_channel. 对于单个 SSH 会话,您可以根据需要多次调用所有这些。


推荐阅读