Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用ppk公钥通过python Paramiko进行ssh连接_Python_Ssh_Putty_Paramiko_Public Key - Fatal编程技术网

如何使用ppk公钥通过python Paramiko进行ssh连接

如何使用ppk公钥通过python Paramiko进行ssh连接,python,ssh,putty,paramiko,public-key,Python,Ssh,Putty,Paramiko,Public Key,我使用ssh连接到服务器 基本身份验证工作得很好,但我不知道如何使用公钥连接 当我连接putty时,服务器会告诉我: Using username "root". Authenticating with public key "rsa-key@ddddd.com" Passphrase for key "rsa-key@ddddd.com": [i've inserted the passphrase here] Last login: Mon Dec 5 09:25:18 2011 from

我使用ssh连接到服务器

基本身份验证工作得很好,但我不知道如何使用公钥连接

当我连接putty时,服务器会告诉我:

Using username "root".
Authenticating with public key "rsa-key@ddddd.com"
Passphrase for key "rsa-key@ddddd.com": [i've inserted the passphrase here]
Last login: Mon Dec  5 09:25:18 2011 from ...
我使用此ppk文件连接到它:

PuTTY-User-Key-File-2: ssh-rsa
Encryption: aes256-cbc
Comment: rsa-key@dddd.com
Public-Lines: 4
[4 lines key]
Private-Lines: 8
[8 lines key]
Private-MAC: [hash]
使用basic auth时,我(从日志中)得到的错误是:

我试图包含该ppk文件并设置为auth_public_key,但没有成功


你能帮我吗?

好的@Adam和@Kimvais是对的,paramiko无法解析.ppk文件

因此,方法是(也要感谢@JimB)将.ppk文件转换为openssh私钥格式;这可以使用如上所述的方法实现

那么,与它建立联系非常简单:

import paramiko
ssh = paramiko.SSHClient()

ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

ssh.connect('<hostname>', username='<username>', password='<password>', key_filename='<path/to/openssh-private-key-file>')

stdin, stdout, stderr = ssh.exec_command('ls')
print stdout.readlines()
ssh.close()
导入paramiko
ssh=paramiko.SSHClient()
ssh.set_缺少_主机_密钥_策略(paramiko.AutoAddPolicy())
ssh.connect(“”,用户名=“”,密码=“”,密钥文件名=“”)
stdin,stdout,stderr=ssh.exec_命令('ls'))
打印stdout.readlines()
ssh.close()

创建Puttygen中Paramiko支持的有效DSA格式私钥

单击转换,然后导出OpenSSH密钥

对于我来说,我这样做:

import paramiko
hostname = 'my hostname or IP' 
myuser   = 'the user to ssh connect'
mySSHK   = '/path/to/sshkey.pub'
sshcon   = paramiko.SSHClient()  # will create the object
sshcon.set_missing_host_key_policy(paramiko.AutoAddPolicy()) # no known_hosts error
sshcon.connect(hostname, username=myuser, key_filename=mySSHK) # no passwd needed
对我来说很好

如前所述,如果Paraminko不支持PPK密钥,那么官方解决方案(如图所示)将使用

但是您也可以使用直接在程序中进行相同的转换

见“

导入系统 进口chilkat key=chilkat.CkSshKey() #加载未加密或加密的PuTTY私钥。 #如果PuTTY私钥已加密,请设置密码 #属性,然后从PuttyPrivateKey调用。 #如果PuTTY私钥未加密,则不会产生差异 #是否设置了密码。 密钥。输入密码(“机密”) #首先将.ppk文件加载到字符串中: keyStr=key.loadText(“putty\u private\u key.ppk”) #导入到SSH密钥对象中: success=key.FromPuttyPrivateKey(keyStr) 如果(成功!=True): 打印(key.lastErrorText()) sys.exit() #转换为加密或未加密的OpenSSH密钥。 #首先演示如何转换为未加密的OpenSSH密钥 bEncrypt=False unencryptedKeyStr=key.toOpenSshPrivateKey(benchrypt) success=key.SaveText(unencryptedKeyStr,“unencrypted_openssh.pem”) 如果(成功!=True): 打印(key.lastErrorText()) sys.exit()
paramiko使用openssh格式密钥。由于密钥是加密的,因此还需要首先解密密钥。使用ssh代理将使事情变得更简单,paramiko可以自动检查代理密钥。这不是公钥,而是私钥。“key_filename”字段需要私钥。使用OpenSSH公钥,程序会发出此异常:paramiko.ssh_异常。SSHException:不是有效的OpenSSH私钥文件如果您有RSA密钥对ssh.connect的密码短语(“”,port=22,username=“”,password=“”,key_filename=“”)我的密钥是文件扩展名是ppk。我需要改变这个吗?ppk to pub?强制性警告:除非您不关心安全性,否则不要使用
AutoAddPolicy
。这样,您将失去对MITM攻击的保护。有关正确的解决方案,请参阅。
import paramiko
hostname = 'my hostname or IP' 
myuser   = 'the user to ssh connect'
mySSHK   = '/path/to/sshkey.pub'
sshcon   = paramiko.SSHClient()  # will create the object
sshcon.set_missing_host_key_policy(paramiko.AutoAddPolicy()) # no known_hosts error
sshcon.connect(hostname, username=myuser, key_filename=mySSHK) # no passwd needed