1

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

Barmar
  • 596,455
  • 48
  • 393
  • 495

1 Answers1

0

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()
VonC
  • 1,042,979
  • 435
  • 3,649
  • 4,283