如何从.pem文件中获取公钥

2024-05-16 10:59:23 发布

您现在位置:Python中文网/ 问答频道 /正文

当我运行python代码(使用parmiko库python)时,我遇到了以下错误:

Bad authentication type; allowed types: ['publickey', 'gssapi-keyex', 'gssapi-with-mic']

我提供的路径指向一个.pem文件,该文件是我访问服务器的密钥

如何从这个不一定使用python的.pem文件中获取公钥

(我正在使用mac电脑)

以下是我使用的代码:

import paramiko

def file_move(): 
    k = paramiko.RSAKey.from_private_key_file("Insertadress") 
    c = paramiko.SSHClient()
    c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    print ("connecting...") 
    c.connect(hostname = "inserthostname", username = None, password = "insert pw" ,pkey = k) 
    print ("connected!!!") 
    stdin, stdout, stderr = c.exec_command('ls')
    c.close() 
    
file_move()

Tags: 文件key代码paramikomoveauthenticationtype错误
1条回答
网友
1楼 · 发布于 2024-05-16 10:59:23

Paramiko似乎没有用于从私钥提取公钥的API,但由于您也欢迎非Python解决方案,下面是一个命令行版本:

ssh-keygen -y -f myprivatekey

其中myprivatekey是包含您的私钥的文件(问题中名为Insertaddress

这将在标准输出上输出公钥

在Linux上测试(该命令由包openssh-client提供),但也应该在Mac上工作

相关问题 更多 >