Pat
Pat

Reputation: 361

How to access a remote host with Paramiko?

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?

Upvotes: 4

Views: 24443

Answers (2)

Valeriy Solovyov
Valeriy Solovyov

Reputation: 5648

Output of your script on my host with this change: stdin, stdout, stderr = ssh.exec_command('ls -1 /root|head -n 5')

File 1.py:

import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
target_host = '127.0.0.1'
target_port = 22
pwd = 'password'
un = 'root'
ssh.connect( hostname = target_host , username = un, password = pwd )
stdin, stdout, stderr = ssh.exec_command('ls -1 /root|head -n 5')
print "STDOUT:\n%s\n\nSTDERR:\n%s\n" %( stdout.read(), stderr.read() )
# For Python3
# print("STDOUT:\n%s\n\nSTDERR:\n%s\n" %( stdout.read(), stderr.read() )) 

Run python 1.py:

STDOUT:
~
1
1421750672-TWya15.png
1.py
7


STDERR:

I think you need ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) avoid.

The authenticity of host 'localhost (::1)' can't be
established.
RSA key fingerprint is 
22:fb:16:3c:24:7f:60:99:4f:f4:57:d6:d1:09:9e:28.
Are you sure you want to continue connecting 
(yes/no)? 

import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(
    paramiko.AutoAddPolicy())
ssh.connect('127.0.0.1', username='jesse', 
    password='lol')

SSH PROGRAMMING WITH PARAMIKO | COMPLETELY DIFFERENT

Upvotes: 7

Pat
Pat

Reputation: 361

OK ... I have now solved the problem: The issue was indeed that in my script there was no specification of the port through which to go. Paramiko therefore defaults to port 22 - which was not open on my router.

Thanks to @betabandido who provided the full method signature in this post.

The fix to my problem was to include the port specification in the connect statement as follows:

ssh.connect( hostname = target_host, port = target_port, username = un, password = pwd )

Thanks to all who contributed!

Upvotes: 0

Related Questions