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 ssh in the console).
When I try to access the host via its external IP address, however, the access via Paramiko fails while ssh 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.
Could anyone please help? Could it be that Paramiko somehow conflicts with the port forwarding I have configured on our router?
Here's my code so far:
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() )
Added:
The full error message received is:
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
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?
sshdlogs? - 9000ABCDdefined, and to what?target_port = ABCDwill throw an exception in this code, and the ssh connection will never even be attempted... - twalbergABCDto mask the actual port used. In my implementation I used an actual numerical value for that port, of course. I validated that I can enter through it usingssh username@host -p ABCD. Sorry for not making this clear from the start. - Pat