paramiko.Proxycommand fails to setup socket

匿名 (未验证) 提交于 2019-12-03 01:34:02

问题:

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 

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

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 

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

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

回答1:

I think you can stick to your first solution. Just you need to change your proxy command and make sure your jump host has got nc installed

I use a proxy command like this

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

I found this WiKi very useful http://en.wikibooks.org/wiki/OpenSSH/Cookbook/Proxies_and_Jump_Hosts Just use actual values instead of %h and %p as I have done.



回答2:

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) 


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!