代码之家  ›  专栏  ›  技术社区  ›  Ricky Levi

如何在Paramiko中模拟我们自己的ssh二进制文件?

  •  1
  • Ricky Levi  · 技术社区  · 7 年前

    我有一个命令,比如

    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:
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   Martin Prikryl    7 年前

    oursshbinary ssh

    帕拉米科是 它自己。

    宋承宪 使用 (这毫无意义)。你要让帕拉米科做什么 我们的早餐 做。但我们不知道 是的,和 宋承宪 .


    这个 -S OpenSSH交换机 宋承宪 宋承宪 或其他OpenSSH工具(如 sftp ).

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

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

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

    Transport.open_sftp_client (或 SSHClient.open_sftp ), Transport.open_session ( SSHClient.invoke_shell , SSHClient.exec_command )去吧。他们都会打电话来 Transport.open_channel