Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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"

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading