Python Forum
pysftp connection issue - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: pysftp connection issue (/thread-37249.html)



pysftp connection issue - geil007 - May-18-2022

hi all,

i am trying to connect to an AWS hosted SFTP server from my Azure Databricks Notebook.

import pysftp as sftp
from base64 import decodebytes
import paramiko

keydata = b"""AAAAB3NzaC1yc2EAAAADAQABAAABAQC5EVdi40ppCS+ctFidNOUxKOkzZbKkHzWYMnM+q3qvqqiouNSWlpU/zNinxIh5mQPCZnzK67dhhJfIzIoRiZ4D9/geTEeWqOtf4LjDveGEx/WCU6sjL1IUQFFa   /1oI8SyfYKwzxLNcJ7nRBCpwrnDngLPuYEurDTdy5lx8MP6VLlLqm3nGN77YtB+vuQKBZ437uduVy6xH0abcuB6H3S26EfyQoWZBJng1jn/O2yXvNhtNpVMKueCuW4NXbJoqPAU9/U6F0YGH9w2qsXFXdfdBUljonEady0vnB2ieAnUe7q1QTJ6MX+UvrEyg/EK7xHNe4GgBWM5dfRXF56B2tbYn"""
key = paramiko.RSAKey(data=decodebytes(keydata))
cnopts = pysftp.CnOpts()
cnopts.hostkeys.add('s-936a09baf53d41da9.server.transfer.eu-central-1.amazonaws.com', 'ssh-rsa', key)
 
with pysftp.Connection('s-936a09baf53d41da9.server.transfer.eu-central-1.amazonaws.com', 'user', 'xxxx', cnopts=cnopts) as sftp:
    #with sftp.cd('/pmd-bisnode-dnb/test/'):           # temporarily chdir to allcode
         print(sftp.listdir())
         #sftp.get('Bayer_CMPELK_DEV_20210326045204_SEEDFILE_HEADER.json') 
but getting the SSH Exception error:

Error:
--------------------------------------------------------------------------- SSHException Traceback (most recent call last) <command-34066901391228> in <module> 8 cnopts.hostkeys.add('s-936a09baf53d41da9.server.transfer.eu-central-1.amazonaws.com', 'ssh-rsa', key) 9 ---> 10 with pysftp.Connection('s-936a09baf53d41da9.server.transfer.eu-central-1.amazonaws.com', 'user', 'xxx', cnopts=cnopts) as sftp: 11 #with sftp.cd('/pmd-bisnode-dnb/test/'): # temporarily chdir to allcode 12 print(sftp.listdir()) /local_disk0/.ephemeral_nfs/envs/pythonEnv-02b3e90a-fb4b-4efb-9cfe-85dc962946d6/lib/python3.8/site-packages/pysftp/__init__.py in __init__(self, host, username, private_key, password, port, private_key_pass, ciphers, log, cnopts, default_path) 138 # Begin the SSH transport. 139 self._transport = None --> 140 self._start_transport(host, port) 141 self._transport.use_compression(self._cnopts.compression) 142 self._set_authentication(password, private_key, private_key_pass) /local_disk0/.ephemeral_nfs/envs/pythonEnv-02b3e90a-fb4b-4efb-9cfe-85dc962946d6/lib/python3.8/site-packages/pysftp/__init__.py in _start_transport(self, host, port) 174 '''start the transport and set the ciphers if specified.''' 175 try: --> 176 self._transport = paramiko.Transport((host, port)) 177 # Set security ciphers if set 178 if self._cnopts.ciphers is not None: /local_disk0/.ephemeral_nfs/envs/pythonEnv-02b3e90a-fb4b-4efb-9cfe-85dc962946d6/lib/python3.8/site-packages/paramiko/transport.py in __init__(self, sock, default_window_size, default_max_packet_size, gss_kex, gss_deleg_creds, disabled_algorithms, server_sig_algs) 439 break 440 else: --> 441 raise SSHException( 442 "Unable to connect to {}: {}".format(hostname, reason) 443 ) SSHException: Unable to connect to s-936a09baf53d41da9.server.transfer.eu-central-1.amazonaws.com: [Errno 111] Connection refused
I have generated the keydata above using: ssh-keyscan s-936a09baf53d41da9.server.transfer.eu-central-1.amazonaws.com

If I don't set the "cnopts" attribute in the Connection, then i get the SSH Exception: No hostkey for host s-936a09baf53d41da9.server.transfer.eu-central-1.amazonaws.com found.

I have checked the AWS SFTP logs, but the request has not even reached there, so there is no logging information.

I am complete lost as to what am I doing wrong here.

Any help or pointer shall really be appreciated, thanks.