使用以下代码连接到SFTP服务器时,出现以下错误。

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')
错误:
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

最佳答案

问题是我没有指定ssh私钥密码短语。使用Pysftp可以将它作为另一个参数添加到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')
可以在文档中找到更多信息:https://pysftp.readthedocs.io/en/release_0.2.9/cookbook.html

09-25 21:43