When connecting to an SFTP server with the following code I am getting the error below.
from base64 import decodebytes
import pysftp
from paramiko import RSAKey
host = 'where_it_should_be'
username = 'thename'
private_key = 'private_key'
keydata = "AAAAAB..."
key = RSAKey(data=decodebytes(keydata))
cnopts = pysftp.CnOpts()
cnopts.hostkeys.add(host, 'ssh-rsa', key)
with pysftp.Connection(host, username, private_key, cnopts=cnopts) as server:
server.get('file_name.pdf')
Error:
Traceback (most recent call last):
File ".\venv\lib\site-packages\pysftp\__init__.py", line 166, in _set_authentication
self._tconnect['pkey'] = RSAKey.from_private_key_file(
File ".\venv\lib\site-packages\paramiko\pkey.py", line 235, in from_private_key_file
key = cls(filename=filename, password=password)
File ".\venv\lib\site-packages\paramiko\rsakey.py", line 55, in __init__
self._from_private_key_file(filename, password)
File ".\venv\lib\site-packages\paramiko\rsakey.py", line 175, in _from_private_key_file
data = self._read_private_key_file("RSA", filename, password)
File ".\venv\lib\site-packages\paramiko\pkey.py", line 308, in _read_private_key_file
data = self._read_private_key(tag, f, password)
File ".\venv\lib\site-packages\paramiko\pkey.py", line 334, in _read_private_key
data = self._read_private_key_pem(lines, end, password)
File ".\venv\lib\site-packages\paramiko\pkey.py", line 386, in _read_private_key_pem
raise PasswordRequiredException("Private key file is encrypted")
paramiko.ssh_exception.PasswordRequiredException: Private key file is encrypted
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<input>", line 1, in <module>
File ".\venv\lib\site-packages\pysftp\__init__.py", line 142, in __init__
self._set_authentication(password, private_key, private_key_pass)
File ".\venv\lib\site-packages\pysftp\__init__.py", line 170, in _set_authentication
self._tconnect['pkey'] = DSSKey.from_private_key_file(
File ".\venv\lib\site-packages\paramiko\pkey.py", line 235, in from_private_key_file
key = cls(filename=filename, password=password)
File ".\venv\lib\site-packages\paramiko\dsskey.py", line 65, in __init__
self._from_private_key_file(filename, password)
File ".\venv\lib\site-packages\paramiko\dsskey.py", line 224, in _from_private_key_file
data = self._read_private_key_file("DSA", filename, password)
File ".\venv\lib\site-packages\paramiko\pkey.py", line 308, in _read_private_key_file
data = self._read_private_key(tag, f, password)
File ".\venv\lib\site-packages\paramiko\pkey.py", line 340, in _read_private_key
raise SSHException(
paramiko.ssh_exception.SSHException: encountered RSA key, expected DSA key
The problem was I didn't specify the ssh private key passphrase. With Pysftp it can be added as another parameter into Connection
.
from base64 import decodebytes
import pysftp
from paramiko import RSAKey
host = 'where_it_should_be'
username = 'thename'
private_key = 'private_key'
keydata = "AAAAAB..."
key = RSAKey(data=decodebytes(keydata))
cnopts = pysftp.CnOpts()
cnopts.hostkeys.add(host, 'ssh-rsa', key)
with pysftp.Connection(
host, username, private_key, private_key_pass='secret',cnopts=cnopts
) as server:
server.get('file_name.pdf')
More information can be found in the documentation: https://pysftp.readthedocs.io/en/release_0.2.9/cookbook.html
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.