Can't login via SSH key using Python [duplicate]

本小妞迷上赌 提交于 2021-02-18 19:10:10

问题


I am trying to login via ssh in my server using python. I have generated key using putty. when i using this key in putty, its working fine. but when i am trying to connect from python it's saying authentication failed

import paramiko

router_ip = "157.230.16.214"
router_username = "root"

ssh = paramiko.SSHClient()

# Load SSH host keys.
ssh.load_system_host_keys()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(router_ip, 
            username=router_username,
            look_for_keys="private.ppk" ) #This is private file and its have in same folder


ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command("show ip route")

output = ssh_stdout.readlines()
ssh.close()

Server Error Message


回答1:


If, as commented, Paraminko does not support PPK key, the official solution, as seen here, would be to use Puttygen.

But you can also use the Python library CkSshKey to make that same conversion directly in your program.

See "Convert PuTTY Private Key (ppk) to OpenSSH (pem)"

import sys
import chilkat

key = chilkat.CkSshKey()

#  Load an unencrypted or encrypted PuTTY private key.

#  If  your PuTTY private key is encrypted, set the Password
#  property before calling FromPuttyPrivateKey.
#  If your PuTTY private key is not encrypted, it makes no diffference
#  if Password is set or not set.
key.put_Password("secret")

#  First load the .ppk file into a string:

keyStr = key.loadText("putty_private_key.ppk")

#  Import into the SSH key object:
success = key.FromPuttyPrivateKey(keyStr)
if (success != True):
    print(key.lastErrorText())
    sys.exit()

#  Convert to an encrypted or unencrypted OpenSSH key.

#  First demonstrate converting to an unencrypted OpenSSH key

bEncrypt = False
unencryptedKeyStr = key.toOpenSshPrivateKey(bEncrypt)
success = key.SaveText(unencryptedKeyStr,"unencrypted_openssh.pem")
if (success != True):
    print(key.lastErrorText())
    sys.exit()


来源:https://stackoverflow.com/questions/63509691/cant-login-via-ssh-key-using-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!