I'm trying to build a list of files in a particular directory on an SFTP server and capture some of the attributes of said files. There's an issue that has been inconsistently popping up when connecting to the server, and I've been unable to find a solution. I say the issue is inconsistent because I can run my Databricks notebook one minute and have it return this particular error but then run it a few minutes later and have it complete successfully with absolutely no changes made to the notebook at all.
from base64 import decodebytes
import paramiko
import pysftp
keydata=b"""host key here"""
key = paramiko.RSAKey(data=decodebytes(keydata))
cnopts = pysftp.CnOpts()
cnopts.hostkeys.add('123.456.7.890', 'ssh-rsa', key)
hostname = "123.456.7.890"
user = "username"
pw = "password"
with pysftp.Connection(host=hostname, username=user, password=pw, cnopts=cnopts) as sftp:
* actions once the connection has been established *
I get the below error message (when it does error out), and it flags the final line of code where I establish the SFTP connection as the culprit. I am unable to reproduce this error on demand. As I said, the code will sometimes run flawlessly and other times return the below error, even though I'm making no changes to the code between runs whatsoever.
Unknown exception: from_buffer() cannot return the address of the raw string within a bytes or unicode object
Traceback (most recent call last):
File "/local_disk0/pythonVirtualEnvDirs/virtualEnv-a488e5a9-de49-48a7-b684-893822004827/lib/python3.5/site-packages/paramiko/transport.py", line 2075, in run
self.kex_engine.parse_next(ptype, m)
File "/local_disk0/pythonVirtualEnvDirs/virtualEnv-a488e5a9-de49-48a7-b684-893822004827/lib/python3.5/site-packages/paramiko/kex_curve25519.py", line 64, in parse_next
return self._parse_kexecdh_reply(m)
File "/local_disk0/pythonVirtualEnvDirs/virtualEnv-a488e5a9-de49-48a7-b684-893822004827/lib/python3.5/site-packages/paramiko/kex_curve25519.py", line 128, in _parse_kexecdh_reply
self.transport._verify_key(peer_host_key_bytes, sig)
File "/local_disk0/pythonVirtualEnvDirs/virtualEnv-a488e5a9-de49-48a7-b684-893822004827/lib/python3.5/site-packages/paramiko/transport.py", line 1886, in _verify_key
if not key.verify_ssh_sig(self.H, Message(sig)):
File "/local_disk0/pythonVirtualEnvDirs/virtualEnv-a488e5a9-de49-48a7-b684-893822004827/lib/python3.5/site-packages/paramiko/rsakey.py", line 134, in verify_ssh_sig
msg.get_binary(), data, padding.PKCS1v15(), hashes.SHA1()
File "/local_disk0/pythonVirtualEnvDirs/virtualEnv-a488e5a9-de49-48a7-b684-893822004827/lib/python3.5/site-packages/cryptography/hazmat/backends/openssl/rsa.py", line 474, in verify
self._backend, data, algorithm
File "/local_disk0/pythonVirtualEnvDirs/virtualEnv-a488e5a9-de49-48a7-b684-893822004827/lib/python3.5/site-packages/cryptography/hazmat/backends/openssl/utils.py", line 41, in _calculate_digest_and_algorithm
hash_ctx.update(data)
File "/local_disk0/pythonVirtualEnvDirs/virtualEnv-a488e5a9-de49-48a7-b684-893822004827/lib/python3.5/site-packages/cryptography/hazmat/primitives/hashes.py", line 93, in update
self._ctx.update(data)
File "/local_disk0/pythonVirtualEnvDirs/virtualEnv-a488e5a9-de49-48a7-b684-893822004827/lib/python3.5/site-packages/cryptography/hazmat/backends/openssl/hashes.py", line 50, in update
data_ptr = self._backend._ffi.from_buffer(data)
TypeError: from_buffer() cannot return the address of the raw string within a bytes or unicode object
Related
I am getting the below error when running the Python code:
sftp.put(local_path, remote_path, callback=track_progress, confirm=True)
But if I make confirm=False then this error doesn't come.
Definition of track_progress is as follows:
def track_progress(bytes_transferred, bytes_total):
total_percent = 100
transferred_percent = (bytes_transferred * total_percent) / bytes_total
result_str = f"Filename: {file}, File Size={str(bytes_total)}b |-->
" f" Transfer Details ={str(transferred_percent)}% " \ f"({str(bytes_transferred)}b)Transferred"
#self.logger.info(result_str)
print(result_str)
Can anyone please help me understand the issue here.
Traceback (most recent call last):
File "D:/Users/prpandey/PycharmProjects/PMPPractise/Transport.py", line 59, in <module>
sftp.put(local_path, remote_path, callback=track_progress, confirm=True)
File "D:\Users\prpandey\PycharmProjects\PMPPractise\venv\lib\site-packages\paramiko\sftp_client.py", line 759, in put
return self.putfo(fl, remotepath, file_size, callback, confirm)
File "D:\Users\prpandey\PycharmProjects\PMPPractise\venv\lib\site-packages\paramiko\sftp_client.py", line 720, in putfo
s = self.stat(remotepath)
File "D:\Users\prpandey\PycharmProjects\PMPPractise\venv\lib\site-packages\paramiko\sftp_client.py", line 495, in stat
raise SFTPError("Expected attributes")
paramiko.sftp.SFTPError: Expected attributes
Paramiko log file:
As suggested, I have tried:
sftp.put(local_path, remote_path, callback=track_progress, confirm=False)
t, msg = sftp._request(CMD_STAT, remote_path)
The t is 101.
When you set confirm=True, the SFTPClient.put asks the server for a size of the just uploaded file. It does that to verify that the file size matches the size of the source local file. See also Paramiko put method throws "[Errno 2] File not found" if SFTP server has trigger to automatically move file upon upload.
The request for size uses SFTP "STAT" message, to which the server should either return "ATTRS" message (file attributes) or an "STATUS" message (101) with an error. Your server seems to return "STATUS" message with "OK" status (my guess based on the data from you and Paramiko source code). The "OK" is an invalid response to the "STAT" request. Paramiko does not expect such nonsense response, so it reports a bit unclear error. But ultimately it's a bug of the server. All you can do is to disable the verification by setting confirm=False.
I'm having trouble connecting to a db I set up in Atlas MongoDB.
I have already read multiple other questions but no answer made my connection work.
I want to point out that I am trying to access it behind a company's proxy (if that has anything to do with it).
My code is this and fails only on the last line where I try to count the documents:
from pymongo import MongoClient
client = MongoClient(
"mongodb+srv://name:pass#iliastrialcluster-1tl2y.azure.mongodb.net/test?retryWrites=true&w=majority&ssl=true&ssl_cert_reqs=CERT_NONE"
)
db = client.get_database('sample_airbnb')
print(db)
listings_and_reviews = db.listingAndReviews
print(listings_and_reviews)
listings_and_reviews.count_documents({})
The error I get is:
Traceback (most recent call last):
File "C:~/mongo_connection1.py", line 11, in <module>
listings_and_reviews.count_documents({})
File "C:~\lib\site-packages\pymongo\collection.py", line 1721, in count_documents
_cmd, self._read_preference_for(session), session)
File "C:~\lib\site-packages\pymongo\mongo_client.py", line 1454, in _retryable_read
read_pref, session, address=address)
File "C:~\lib\site-packages\pymongo\mongo_client.py", line 1253, in _select_server
server = topology.select_server(server_selector)
File "C:~\lib\site-packages\pymongo\topology.py", line 235, in select_server
address))
File "C:~\lib\site-packages\pymongo\topology.py", line 193, in select_servers
selector, server_timeout, address)
File "C:~\lib\site-packages\pymongo\topology.py", line 209, in _select_servers_loop
self._error_message(selector))
pymongo.errors.ServerSelectionTimeoutError: iliastrialcluster-shard-00-00-1tl2y.azure.mongodb.net:27017: timed out,iliastrialcluster-shard-00-02-1tl2y.azure.mongodb.net:27017: timed out,iliastrialcluster-shard-00-01-1tl2y.azure.mongodb.net:27017: timed out
ServerSelectionTimeout error is the client's way of telling you you can't connect to the server. The primary cause of these errors when using MongoDB Atlas is the failure to enable access for the IP address of the node the client is running on.
To verify try connecting using the MongoDB shell mongo using the same connection string. if you get a connection failed then you know it's not a Python problem. Your client code looks OK BTW so I am pretty sure this is what it is.
The connection to the server is lazily evaluated so we don't try to initiate a connection until you make an actual request. In this case the count_documents call. This is why this is the call that generates the error.
#JoeDrumgoole Thanks for your answer. The problem was the proxy after all, after deactivating it the connection works fine. Good to know that the connection to the server is lazily evaluated.
I'm running Ubuntu 18.04 droplets on DigitalOcean and need to set file transfers between them.
Pysftp/Paramiko libraries fits the job well, but I have trouble establishing connection. Connecting with same code from desktop works well.
cnopts = pysftp.CnOpts()
cnopts.hostkeys = None
with pysftp.Connection(host='ip adress', username='username',
password='pass', cnopts=cnopts) as sftp:
Error message when initializing connection is:
Traceback (most recent call last):
File "/home/airflow/airflow/tasks/ta_auto/screener_slave.py", line 315, in <module>
cnopts = pysftp.CnOpts()#knownhosts='/home/airflow/.ssh/known_hosts')
File "/home/airflow/pipeline/lib/python3.6/site-packages/pysftp/__init__.py", line 54, in __init__
self.hostkeys.load(knownhosts)
File "/home/airflow/pipeline/lib/python3.6/site-packages/paramiko/hostkeys.py", line 101, in load
e = HostKeyEntry.from_line(line, lineno)
File "/home/airflow/pipeline/lib/python3.6/site-packages/paramiko/hostkeys.py", line 364, in from_line
key = ECDSAKey(data=decodebytes(key), validate_point=False)
File "/home/airflow/pipeline/lib/python3.6/site-packages/paramiko/ecdsakey.py", line 163, in __init__
key = ec.EllipticCurvePublicKey.from_encoded_point(
AttributeError: type object 'EllipticCurvePublicKey' has no attribute 'from_encoded_point'
Upgrade to the latest version of Paramiko – 2.6 as of now.
This is most likely the same problem as in:
How to silence EllipticCurvePublicNumbers.encode_point CryptographyDeprecationWarning when using Paramiko in Python.
Obligatory warning: Do not set cnopts.hostkeys = None, unless you do not care about security. For the correct solution see Verify host key with pysftp.
I tried connecting my application to my cluster on mongodb Atlas like the examples but I'm still having trouble.
I used
import pymongo
client = pymongo.MongoClient('mongodb+srv://kay:myrealpassword#cluster0.mongodb.net/test')
I replaced myrealpassword with my password and cluster0 with my cluster's name .
This is the error i got :
Traceback (most recent call last): File "", line 1, in
File
"/Library/Python/2.7/site-packages/pymongo/mongo_client.py", line 468,
in init
res = uri_parser.parse_uri(entity, port, warn=True) File "/Library/Python/2.7/site-packages/pymongo/uri_parser.py", line 399,
in parse_uri
nodes = _get_dns_srv_hosts(fqdn) File "/Library/Python/2.7/site-packages/pymongo/uri_parser.py", line 289,
in _get_dns_srv_hosts
raise ConfigurationError(str(exc)) pymongo.errors.ConfigurationError: None of DNS query names exist:
_mongodb._tcp.saudimood.mongodb.net., _mongodb._tcp.saudimood.mongodb.net.Home.
I was having similar DNS error, I misspelled the DNS seed list :s
To avoid this and another errors try:
In the Clusters view, click Connect for the cluster to which you
want to connect.
Select Connect your application.
Select your driver and version.
Copy the connection string.
Replace <password> with the password for the <username> user. Replace myFirstDatabase with the name of the database that connections will use by default.
Try it
pymongo.MongoClient('mongodb://user:password#servername/dbname')
I'm trying to read a text file from a server using ssh from python 3.5. I'm using paramiko to connect to the server but unfortunately, I'm having trouble actually connecting to the server.
this is the code I'm using to connect to the server
import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('IP ADDRESS OF THE SERVER', key_filename="/home/user/.ssh/id_ecdsa" ,look_for_keys=True)
but every time I try to connect to the server I'm getting an authentication failed error message, can anyone see what I'm doing wrong here?
Any advice would be appreciated
This is the error I'm getting everytime i connect.
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.5/site-packages/paramiko/client.py", line 380, in connect
look_for_keys, gss_auth, gss_kex, gss_deleg_creds, gss_host)
File "/usr/local/lib/python3.5/site-packages/paramiko/client.py", line 603, in _auth
raise saved_exception
File "/usr/local/lib/python3.5/site-packages/paramiko/client.py", line 580, in _auth
allowed_types = set(self._transport.auth_publickey(username, key))
File "/usr/local/lib/python3.5/site-packages/paramiko/transport.py", line 1331, in auth_publickey
return self.auth_handler.wait_for_response(my_event)
File "/usr/local/lib/python3.5/site-packages/paramiko/auth_handler.py", line 208, in wait_for_response
raise e
paramiko.ssh_exception.AuthenticationException: Authentication failed.
You should use your private key to connect to a remote server. Your public key must be already installed in the server side, i.e. it must be listed in ~/.ssh/authorized_keys.
Try first from the command line, and only then use Python/paramiko. Check the permissions of the files/directories if all that fails.
Turns out I simply just forgot to add in the username in the connection string. works perfectly now.