7

I need to connect to a server with SSH to download files. I have Ubuntu and I've set up SSH in the standard way: I have a ssh_config file in .ssh which defines a host entry (say host_key) for the server address (Hostname.com) and username, and I've set up an RSA key. So when I try to log into SSH from the command line or bash, I just need to use ssh host_key

I would like to do this in Python. The standard solutions seems to be to use Paramiko to set up the connection. I tried this:

from paramiko import SSHClient
from scp import SCPClient

ssh = SSHClient()
ssh.load_system_host_keys()
ssh.connect('host_key')

scp = SCPClient(ssh.get_transport())
# etc...

However, it always seems to hang and time out on ssh.connect('host_key'). Even when I try to include the username and password: ssh.connect('host_key', username='usrnm', password='pswd').

Are my host keys not loading properly? And would this take care of the RSA keys as well?

It only works if I use the whole Hostname.com with username and typed-out password. Which is maybe a bit insecure.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Marses
  • 1,464
  • 3
  • 23
  • 40

2 Answers2

4

Since paramiko has a SSHConfig class, you can use it for your ~/.ssh/config. However, it is slightly messy, I recommend you to use fabric instead of that. Here is the code example:

from fabric.api import put
put('local path', 'remote path')
Wereii
  • 330
  • 5
  • 16
teramonagi
  • 140
  • 9
0

I do not think that it is common to use ssh_config file with Paramiko (or any other code/language). ssh_config is a configuration file for OpenSSH tools, not for SSH in general.

Usually, you specify your private key directly in your code as an argument of SSHClient.connect method:
How to access to a remote server using Paramiko with a public key-file


If you want to keep using ssh_config, Paramiko can parse it. Check parse_ssh_config and lookup_ssh_host_config functions. But I believe, you still have to look up the key file from the config and pass it explicitly to SSHClient.connect method.

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