简体   繁体   中英

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.

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM