title: How to access a remote host with Paramiko?
body: <p>I am just getting started on using Paramiko to connect to another host. I can access this host via its internal IP address in my local network and this works fine (both in Python and via <code>ssh</code> in the console).</p>
  
  <p>When I try to access the host via its external IP address, however, the access via Paramiko fails while <code>ssh</code> in the console still works. The only difference here is that instead of the host's internal IP address I use the machine's external one.</p>
  
  <p>Could anyone please help? Could it be that Paramiko somehow conflicts with the port forwarding I have configured on our router? </p>
  
  <p>Here's my code so far:</p>
  
  <pre><code>import paramiko
  
  ssh = paramiko.SSHClient()
  
  ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  
  target_host = 'external.IP'
  # target_host = 'internal.IP'
  
  # Internal access port for ssh transfer.
  # target_port = 22
  
  # External access port for ssh transfer.
  target_port = ABCD
  
  pwd = 'my.password'
  un = 'my.username'
  
  ssh.connect( hostname = target_host , username = un, password = pwd )
  
  stdin, stdout, stderr = ssh.exec_command('ls -1 .')
  
  print "STDOUT:\n%s\n\nSTDERR:\n%s\n" %( stdout.read(), stderr.read() )
  </code></pre>
  
  <p>Added:</p>
  
  <p>The full error message received is:</p>
  
  <p>Traceback (most recent call last):
    File "./test.py", line 25, in 
      ssh.connect( hostname = target_host , username = un, password = pwd )
    File "/usr/local/lib/python2.7/site-packages/paramiko/client.py", line 251, in connect
      retry_on_signal(lambda: sock.connect(addr))
    File "/usr/local/lib/python2.7/site-packages/paramiko/util.py", line 270, in retry_on_signal
      return function()
    File "/usr/local/lib/python2.7/site-packages/paramiko/client.py", line 251, in 
      retry_on_signal(lambda: sock.connect(addr))
    File "/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 224, in meth
      return getattr(self._sock,name)(*args)
  socket.error: [Errno 61] Connection refused</p>
  
  <p>The only difference between the Python and the console access is the port. Could it be Paramiko always goes via port 22 for ssh? If so, is there a way to direct Paramiko to go through another port?</p>