Python中的SSH隧道

时间:2017-02-27 09:22:48

标签: python linux ssh ssh-tunnel

我有3台Ubuntu机器,机器2有两个以太网端口,即它有两个不同子网的两个IP。机器1可以ping到机器2,机器2可以ping到机器3以及机器1.但是机器1不能ping到机器3,反之亦然。以下是机器的IP:

  1. Machine 1 IP:192.168.0.10
  2. Machine 2 eth0 IP:192.168.0.20
  3. Machine 2 eth1 IP:192.168.167.10
  4. Machine 3 IP:192.168.167.20
  5. 我可以使用机器2的用户名和密码从机器1到机器2。 我可以使用机器3的用户名和机器3提供的密钥从机器2到机器3进行ssh。

    现在我想在机器1上运行一个python脚本,我想使用这个python脚本在机器3上运行一个命令(比如uname -a)。

    我使用下面的代码执行此操作:

    import paramiko
    from sshtunnel import SSHTunnelForwarder
    import logging
    
    logging.basicConfig(level=logging.DEBUG)
    
    key = paramiko.RSAKey.from_private_key_file("machineKey.key", password='Password')
    
    with SSHTunnelForwarder(
    ("192.168.0.20", 22),             #Machine 2 IP
    ssh_username="user",              #Machine 2's user name
    ssh_password="password",          #Machine 2's password
    ssh_pkey=key,                     #Machine 3's key (required for authentication)
    remote_bind_address=("192.168.167.20", 22),        #Machine 3's IP
    ) as tunnel:
    
    client = paramiko.SSHClient()
    client.load_system_host_keys()
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    client.connect('0.0.0.0', 22)
    print (client.exec_command("uname -a"))
    client.close()
    print('FINISH!')
    

    当我跑步时,我收到了以下错误:

    DEBUG:paramiko.transport:starting thread (client mode): 0xb6de7b8cL
    INFO:paramiko.transport:Connected (version 2.0, client OpenSSH_6.6.1p1)
    DEBUG:paramiko.transport:Ciphers agreed: local=aes128-ctr, remote=aes128-    ctr
    DEBUG:paramiko.transport:using kex diffie-hellman-group1-sha1; server key     type ssh-rsa; cipher: local aes128-ctr, remote aes128-ctr; mac: local     hmac-sha1, remote hmac-sha1; compression: local none, remote none
    DEBUG:paramiko.transport:Switch to new keys ...
    DEBUG:paramiko.transport:Attempting public-key auth...
    DEBUG:paramiko.transport:userauth is OK
    INFO:paramiko.transport:Authentication (publickey) failed.
    DEBUG:paramiko.transport:EOF in transport thread
    DEBUG:paramiko.transport:starting thread (client mode): 0xb6de7b2cL
    INFO:paramiko.transport:Connected (version 2.0, client OpenSSH_6.6.1p1)
    DEBUG:paramiko.transport:Ciphers agreed: local=aes128-ctr, remote=aes128-ctr
    DEBUG:paramiko.transport:using kex diffie-hellman-group1-sha1; server key type ssh-rsa; cipher: local aes128-ctr, remote aes128-ctr; mac: local     hmac-sha1, remote hmac-sha1; compression: local none, remote none
    DEBUG:paramiko.transport:Switch to new keys ...
    DEBUG:paramiko.transport:Attempting password auth...
    DEBUG:paramiko.transport:userauth is OK
    INFO:paramiko.transport:Authentication (password) successful!
    DEBUG:paramiko.transport:EOF in transport thread
    Traceback (most recent call last):
      File "tunnelTest.py", line 37, in <module>
        client.connect("127.0.0.1", 2947, username = "root", pkey = key)
      File "/usr/lib/python2.7/dist-packages/paramiko/client.py", line 300, in connect
        retry_on_signal(lambda: sock.connect(addr))
      File "/usr/lib/python2.7/dist-packages/paramiko/util.py", line 278, in retry_on_signal
        return function()
      File "/usr/lib/python2.7/dist-packages/paramiko/client.py", line 300, in <lambda>
        retry_on_signal(lambda: sock.connect(addr))
      File "/usr/lib/python2.7/socket.py", line 224, in meth
        return getattr(self._sock,name)(*args)
    socket.error: [Errno 111] Connection refused
    

    任何人都可以建议我改变我必须做的事情让它运行吗? 感谢

0 个答案:

没有答案