Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't connect to remote server with Fabric and SSH using key file

Tags:

python

ssh

fabric

I'm trying to use a Fabric python script to log into the production server then run the 'ls' command remotely. Well I actually have lots of other commands to run, but I'm starting off with a simple list to get it working. My production server uses SSH and is locked down so it needs a private key file and password.

Now I've been reading up on some sites about how to get this to work, but can't get it to log in for some reason. I think it connects ok but a message comes up saying:

Login password for 'root':

So I enter my password (same as the one in env.password) and it just keeps popping up the message.

Here's my fabfile.py:

from fabric.api import *

env.use_ssh_config = True
env.hosts = ["myserver.net"]
env.user = "root"
env.key_filename = "/home/myusername/.ssh/id_rsa.ppk"
env.password = "mypassword"
env.port = 22

def testlive():
  run("ls")

Here's my SSH config in /home/myusername/.ssh/config:

Host myserver
  hostname myserver.net
  port 22
  IdentityFile ~/.ssh/id_rsa.ppk

Any ideas on how to get this working?

Many thanks

like image 958
zuallauz Avatar asked Feb 19 '23 05:02

zuallauz


1 Answers

I ended up testing the SSH config separately from the command line first to get that part working. I think there was a problem with the SSH keys as I had used PuTTY to generate them and that format may have been incompatible with the OpenSSH ones that Linux uses.

So first I made new SSH keys on my linux machine without a password for the private key which made two files for me id_rsa and id_rsa.pub. Then I copied the public key string from id_rsa.pub into the authorized_keys file on the production server. Then I tested from the command line. Once that was working I tested with Fabric.

So config changed to look like:

from fabric.api import *

env.use_ssh_config = True
env.hosts = ["myserver"]
env.user = "root"
env.key_filename = "/home/myusername/.ssh/id_rsa"
env.password = ""
env.port = 22

def testlive():
  run("ls")

Here's my SSH config in /home/myusername/.ssh/config:

Host myserver
  hostname myserver.net
  port 22
  IdentityFile ~/.ssh/id_rsa

Now works fine when I run fab testlive from the commandline.

like image 180
zuallauz Avatar answered Feb 21 '23 19:02

zuallauz