carrieyummy
carrieyummy
采纳率0%
2019-03-29 19:23 阅读 1.6k

求教:python paramiko库:paramiko.ssh_exception.SSHException: not a valid RSA private key file

私钥是正确的;通过secureCrt可以正常访问堡垒机。
本地是mac,堡垒机那边是linux。
用python paramiko库加载私钥时会报错。。代码是:

import paramiko
private_key = paramiko.RSAKey.from_private_key_file('/Users/tian/.ssh/id_rsa')

异常是下面这个;Mac上之前是python3.6和现在是python3.7,都有这个异常。

Traceback (most recent call last):
  File "/Users/tianyawei/PycharmProjects/protocolTest/CUAdapterV1/AdapterMachine/MyMachine.py", line 11, in <module>
    private_key = paramiko.RSAKey.from_private_key_file('/Users/tian/.ssh/id_rsa')
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/paramiko/pkey.py", line 206, in from_private_key_file
    key = cls(filename=filename, password=password)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/paramiko/rsakey.py", line 55, in __init__
    self._from_private_key_file(filename, password)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/paramiko/rsakey.py", line 175, in _from_private_key_file
    data = self._read_private_key_file("RSA", filename, password)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/paramiko/pkey.py", line 279, in _read_private_key_file
    data = self._read_private_key(tag, f, password)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/paramiko/pkey.py", line 289, in _read_private_key
    raise SSHException("not a valid " + tag + " private key file")
paramiko.ssh_exception.SSHException: not a valid RSA private key file

我都尝试了很久了,但是没找到解决办法。。。。求大神帮忙。。。。。。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • qq_26359813 ssisiss 2019-10-21 15:43

    ssh-keygen -p -m PEM -f ~/.ssh/id_rsa

    点赞 1 评论 复制链接分享
  • sinat_25591161 goofts 2019-10-04 10:59

    是openssh的问题,openssh产生的RSA文件以"BEGIN OPENSSH PRIVATE KEY"开始,而paramiko需要的是以"BEGIN RSA PRIVATE KEY"开始。
    ssh-keygen -p -m PEM方法产生的RSA文件会比较合适

    点赞 评论 复制链接分享

相关推荐