首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >pysftp Paramiko PasswordRequiredException:私钥文件已加密

pysftp Paramiko PasswordRequiredException:私钥文件已加密
EN

Stack Overflow用户
提问于 2020-10-28 05:17:48
回答 1查看 733关注 0票数 2

当使用以下代码连接到SFTP服务器时,我收到以下错误。

代码语言:javascript
运行
复制
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')

错误:

代码语言:javascript
运行
复制
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
EN

Stack Overflow用户

发布于 2020-10-28 05:17:48

问题是我没有指定ssh私钥密码。使用Pysftp,它可以作为另一个参数添加到Connection中。

代码语言:javascript
运行
复制
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

票数 1
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64563038

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档