3

In my script, I need to do an SSH to a remote system using a private key and dump the file into its directory.

The command I am using to SSH into the system is this:

ssh -i private_key localhost

Followed by the standard input:

Enter passphrase for key 'private_key'

I am trying to do this in a Python script, but am not sure about the way of writing a command and passing a passphrase as a parameter so that the whole sequence can be automated.

Please suggest me a way to achieve this via a library (Paramiko SSHClient) or a code snippet would be highly really appreciated.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
AgentLog
  • 97
  • 1
  • 5

1 Answers1

5

SSHClient.connect can handle public key authentication with a simple call:

import paramiko

ssh = paramiko.SSHClient()
ssh.connect(hostname, username=username, key_filename=key_path, password=passphrase)

The password argument is used as a passphrase, when key_filename is provided.


Additionally, you will also have to verify the server's host key (as you must have done with ssh before). See Paramiko "Unknown Server".

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992