【问题标题】:pysftp Paramiko PasswordRequiredException: Private key file is encryptedpysftp Paramiko PasswordRequiredException:私钥文件已加密
【发布时间】:2020-10-30 21:06:56
【问题描述】:

使用以下代码连接到 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

【问题讨论】:

    标签: python ssh rsa paramiko pysftp


    【解决方案1】:

    问题是我没有指定 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

    【讨论】:

      猜你喜欢
      • 2013-03-12
      • 2018-03-30
      • 2016-11-16
      • 2018-07-04
      • 2017-01-04
      • 2021-10-29
      • 2018-01-31
      • 2019-07-03
      相关资源
      最近更新 更多