Marci
Marci

Reputation: 457

paramiko.ssh_exception.PasswordRequiredException: Private key file is encrypted

I'm trying to connect to server with paramiko but there's always some kind of problem with private key. id_dsa is an open ssh key, so I don't know what the problem can be.

Thanks in advance!

   import paramiko
   k = paramiko.RSAKey.from_private_key_file("C:/Users/bok/Desktop/id_dsa")

   c = paramiko.SSHClient()
   c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
   print ("connecting")
   c.connect( hostname = "3x.1x9.2x.2x", username = "taq4", password = "xxxxxx", pkey=k)
   print ("connected")
   commands = [ "ls", "pwd" ]
   for command in commands:
           print ("Executing {}").format( command )
           stdin , stdout, stderr = c.exec_command(command)
           print(stdout.read())
           print("Errors")
           print(stderr.read())
   c.close()

error:

paramiko.ssh_exception.PasswordRequiredException: Private key file is encrypted

Upvotes: 9

Views: 13805

Answers (2)

Adam Smooch
Adam Smooch

Reputation: 1302

I also got the error above:

paramiko.ssh_exception.PasswordRequiredException: private key file is encrypted

But it worked fine (passwordless) with ssh -i <key_filename> ...

Turns out my call to .connect() was simply missing the [non-standard] port# 🤦

    first_hop = {
        "hostname": "mydomain.ca",
        "username": "myuser",
        "pkey": Ed25519Key.from_private_key_file(f"cell.key"),
        # "pkey": Ed25519Key.from_private_key(open("cell.key")),
        "port": 2222
    }
    ssh.connect(**first_hop)

Upvotes: 0

Whiplash
Whiplash

Reputation: 57

It was your RSA key gen with password, just try it with your password private_key = paramiko.RSAKey.from_private_key_file(rsa_key, password)

Upvotes: 4

Related Questions