paramiko.Proxycommand 无法设置套接字 [英] paramiko.Proxycommand fails to setup socket

查看:49
本文介绍了paramiko.Proxycommand 无法设置套接字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Python 中的 paramiko 通过 SSH 连接到通过另一台计算机建立隧道的计算机,但我有一些奇怪的问题.我在 /.ssh/config 中的配置文件如下所示:

I am trying to connect via SSH to a computer tunneling through another computer using paramiko in Python, but I am having some strange issues. My config file in /.ssh/config looks like this:

Host remoteA
HostName 169.254.1.1
User root
IdentityFile ~/.ssh/id_dss.openssh.remoteA
ForwardX11 no
StrictHostKeyChecking no
ForwardAgent yes
UserKnownHostsFile /dev/null

Host remoteB
User root
IdentityFile ~/.ssh/id_dss.openssh.remoteB
ForwardX11 no
StrictHostKeyChecking no
UserKnownHostsFile /dev/null
ProxyCommand ssh -W 169.254.1.2:22 remoteA

我的python代码是这样的:

And my python code like this:

from paramiko import SSHClient, SSHConfig, SSHException
import getpass
import paramiko


def getSSHConnection(hostName):
     config = SSHConfig()


     user = getpass.getuser()
     config.parse(open('C:/Users/' + user +'/.ssh/config')) 
     host=config.lookup(hostName)


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

     #Check for proxy settings
     try:
        print host ['proxycommand']
        proxy = paramiko.ProxyCommand(host['proxycommand'])
    except:
        proxy = None
    #Setup the SSH connection
    try:
        if (proxy is None):
            client.connect(host['hostname'],22, username=host['user'],key_filename=host['identityfile'])
        else:
            client.connect(host['hostname'],22, username=host['user'],  key_filename=host['identityfile'], sock=proxy)
    except SSHException, ex:
        print ex

    return client





ssh_client = getSSHConnection('remoteA')

# run a command
print "\nRun a command"
cmd = 'ps aux'
stdin, stdout, stderr = ssh_client.exec_command(cmd)

print stdout.read()

ssh_client = getSSHConnection('remoteB')

# run a command
print "\nRun a command"
cmd = 'ps aux'
stdin, stdout, stderr = ssh_client.exec_command(cmd)

print stdout.read()

首先当连接到 remoteA 时它运行良好,但是当连接到 remoteB 时它在步骤中崩溃:proxy = paramiko.ProxyCommand(host['proxycommand']).

First when connecting to remoteA it works perfectly, but then when connecting to remoteB it crashes in the step: proxy = paramiko.ProxyCommand(host['proxycommand']).

File "C:\Python27\lib\site-packages\paramiko\client.py", line 291, in connect
    for (family, socktype, proto, canonname, sockaddr) in socket.getaddrinfo(hostname, port,         socket.AF_UNSPEC, socket.SOCK_STREAM):
socket.gaierror: [Errno 11004] getaddrinfo failed

在 Cygwin 中,我可以使用命令 ssh remoteB 进行连接,所以我知道配置文件应该没问题.

Within Cygwin, I am able to connect using the command ssh remoteB so I know the config file should be ok.

如果有什么不同,我在 Windows 7 上运行它.

If it makes any difference, I am running this on Windows 7.

推荐答案

所以我没有使用 ProxyCommand 而是使用端口转发来解决我的问题.

So instead of using ProxyCommand I used port forwarding to solve my issue.

def getForwardedSSHPort(self, tunnnelHostName):
    forwarderClient = self.getSSHConnection(tunnnelHostName, None)
    transport = forwarderClient.get_transport()
    dest_addr = ('169.254.1.2', 22)
    local_addr = ('127.0.0.1', 10022)
    channel = transport.open_channel('direct-tcpip', dest_addr, local_addr)

    remoteClient = self.getSSHConnection( tunnnelHostName, channel)

这篇关于paramiko.Proxycommand 无法设置套接字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆