2014-02-06 36 views
4

我試圖通過SSH連接到通過另一臺計算機在Python中使用paramiko進行隧道傳輸的計算機,但我遇到了一些奇怪的問題。 在/.ssh/config我的配置文件看起來像這樣:paramiko.Proxycommand無法設置套接字

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代碼:

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'])

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連接,所以我知道該配置文件應該沒問題。

如果這有什麼差別,我在Windows 7上運行此

+1

所以我設法繞過我的問題,而不是做端口轉發。 – jimmy

回答

2

因此,而不是使用ProxyCommand我用端口轉發來解決我的問題。

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) 
6

我認爲你可以堅持你的第一個解決方案。只要您需要更改您的代理命令,確保您的跳主機已經得到了NC安裝

我使用代理命令這樣

proxy = paramiko.ProxyCommand("ssh -o StrictHostKeyChecking=no jumphostIP nc targethostIP 22") 

我發現這個wiki非常有用 http://en.wikibooks.org/wiki/OpenSSH/Cookbook/Proxies_and_Jump_Hosts 只需要用實際值代替%h和%p,正如我所做的那樣。

+0

這絕對值得在將來查看。在這種情況下,我無法在跳轉主機上安裝任何東西,畢竟我對當前的解決方案非常滿意。 – jimmy