Paramiko 无法识别 ssh-keygen 生成的 SSH 密钥:“不是有效的 RSA 私钥文件" [英] SSH key generated by ssh-keygen is not recognized by Paramiko: "not a valid RSA private key file"

查看:111
本文介绍了Paramiko 无法识别 ssh-keygen 生成的 SSH 密钥:“不是有效的 RSA 私钥文件"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

ssh_key = paramiko.RSAKey.from_private_key_file(key_filename)

键看起来像这样:

-----BEGIN OPENSSH PRIVATE KEY-----
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAABFwAAAAdzc2gtcn
NhAAAAAwEAAQAAAQEAqdgmJ2AQlmvpCsDWjbpIvIrx4AwtKn2t10wmGZIN9pqcJgQpo3HD

并且有效:

 $ ssh-keygen -l -f <mykeyfile>
 $ 2048 SHA256:x8jlUAObU3q2KXRtuGpxwhnGvB/ZoeD2IUqSA1OkCmI thomas@Thomas-MBP-2017 (RSA)

但我收到以下错误:

不是有效的 RSA 私钥文件

not a valid RSA private key file

这是在 MacOS、Python 2.7、Paramiko 2.4.2 上

This is on MacOS, Python 2.7, Paramiko 2.4.2

我做错了什么?

推荐答案

对于 OpenSSH 7.8 以上,你必须欺骗它.运行 ssh-keygen -p [-f file] -m pem 据称更改密码,但重复使用旧密码.如果您想避免提示(如在脚本中),请使用 -P oldpw -N newpw,但要小心让其他用户看到您的密码.作为副作用,这会以旧"(OpenSSL 兼容,因此 paramiko 兼容)格式重写密钥文件(如果不是 ed25519).(如果要保留新格式的文件,请先复制.)

For OpenSSH 7.8 up, you have to trick it. Run ssh-keygen -p [-f file] -m pem to purportedly change passphrase, but reuse the old one. Use -P oldpw -N newpw if you want to avoid the prompts, as in a script, but be careful of making your passphrase visible to other users. As a side effect this rewrites the keyfile (if not ed25519) in 'old' (OpenSSL-compatible and thus paramiko-compatible) format. (If you want to keep the new-format file, copy first.)

对于旧版本的 OpenSSH,只需执行 ssh-keygen -p [-f file] 而没有 -o.

For older versions of OpenSSH just do ssh-keygen -p [-f file] WITHOUT -o.

此外,如果您拥有(或获得)它,从 0.69 开始,PuTTY 套件中的 puttygen 实用程序支持这种格式.在 Unix 版本中,只需执行 puttygen newfmtfile -O private-openssh -o oldfmtfile(同样,ed25519 除外).在 Windows 版本 AFAICT 中,您必须使用 GUI;加载 newfmtfile 并执行转换/导出 OpenSSH 密钥.

Also, if you have (or get) it, the puttygen utility in the PuTTY suite from 0.69 up supports this format. In the Unix version, just do puttygen newfmtfile -O private-openssh -o oldfmtfile (again excepting ed25519). In the Windows version AFAICT you must use the GUI; load the newfmtfile and do Conversions / Export OpenSSH key .

这篇关于Paramiko 无法识别 ssh-keygen 生成的 SSH 密钥:“不是有效的 RSA 私钥文件"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆