Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to connect to SFTP through Paramiko with SSH key - Pageant

I am trying to connect to an SFTP through Paramiko with a passphrase protected SSH key. I have loaded the key into Pageant (which I understand is supported by Paramiko) but I can't get it to decrypt my private key.

I have found this example here that references allow_agent=True but this does not appear to be a parameter that can be used with the SFTPClient.

Can anyone advise if it is possible to work with Paramiko and Pageant in this way?

This is my code at the moment - which raises PasswordRequiredException

privatekeyfile = 'path to key'
mykey = paramiko.RSAKey.from_private_key_file(privatekeyfile)
transport = paramiko.Transport(('host', 'port'))
transport.connect('username',pkey = mykey)
sftp = paramiko.SFTPClient.from_transport(transport)
like image 360
Tim S_ Avatar asked Aug 20 '14 08:08

Tim S_


People also ask

How do I connect to a Paramiko remote server?

A Paramiko SSH Example: Connect to Your Server Using a Password. This section shows you how to authenticate to a remote server with a username and password. To begin, create a new file named first_experiment.py and add the contents of the example file. Ensure that you update the file with your own Linode's details.

How do I pass a private key using SFTP?

Right-click the icon and select “Add Key” and select your private key (PPK) file. Follow the prompt to enter your pass phrase and you're done. Now simply launch FileZilla Pro and connect to your server using SFTP using SSH2 with a username and an empty password. Don't forget to close pageant when you're done.


1 Answers

You have to provide a passphrase, when loading an encrypted key using the RSAKey.from_private_key_file.

Though note that you do not have to load the key at all, when using the Pageant. That's the point of using an authentication agent. But only the SSHClient class supports the Pageant. The Transport class does not, on its own.

You can follow the code in How to use Pageant with Paramiko on Windows?
Though as the allow_agent is True by default, there is actually nothing special about the code.

Once connected and authenticated, use the SSHClient.open_sftp method to get your instance of the SFTPClient.

ssh = paramiko.SSHClient()
ssh.connect(host, username='user', allow_agent=True)
sftp = ssh.open_sftp()

You will also need to verify the host key:
Paramiko "Unknown Server"

like image 77
Martin Prikryl Avatar answered Oct 25 '22 03:10

Martin Prikryl