Paramiko: "not a valid DSA private key file"

I am trying to connect to some SFTP using a private key file that looks like:

---- BEGIN SSH2 ENCRYPTED PRIVATE KEY ---- Subject: L0709146 Comment: "1024-bit dsa, L0709146@pxz102, Wed Jan 12 2022 11:25:54 +010\ 0" P2/bla...bla...bla ---- END SSH2 ENCRYPTED PRIVATE KEY ---- 

Using the following code:

import paramiko path = "path/to/my/file" transport = paramiko.Transport((self.host, self.port)) transport.connect(username=self.user,pkey=paramiko.DSSKey.from_private_key(open(path))) # ^^^ Error line ^^^ #transport.connect(username=self.user,pkey=paramiko.RSAKey.from_private_key(open(path))) #transport.connect(username=self.user,pkey=paramiko.ECDSAKey.from_private_key(open(path))) sftp = paramiko.SFTPClient.from_transport(transport) logging.info(sftp.listdir()) 

I'm not sure but I understand that is a DSA private key file, but I obtained the error:

paramiko.ssh_exception.SSHException: not a valid DSA private key file

I tried with other options but, a similar error

paramiko.ssh_exception.SSHException: not a valid RSA private key file paramiko.ssh_exception.SSHException: not a valid EC private key file paramiko.ssh_exception.SSHException: not a valid OPENSSH private key file 

I used the FileZilla client, I hadn't problem connecting to the SFTP, I don't get why with Python I have problems.

1 Answer

You have a private key in rarely used ssh.com format. Paramiko does not support it. You have to convert it to the OpenSSH format.

You can use ssh-keygen like this:

ssh-keygen -i -f sshcomkey > opensshkey 

On Windows you can also use PuTTYgen.

Related question: Paramiko: "not a valid RSA private key file"

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like