Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to establish a SSH connection via proxy using Fabric?

Tags:

python

fabric

I am trying to establish a SSH connection between a Windows PC and a Linux server(amazon ec2).

I decided to use Fabric API implemented using python.

I have Putty installed on the Windows PC.

My fabfile script looks like this:

import sys
from fabric.api import *


def testlive():
  print 'Test live ...'
  run("uptime")

env.use_ssh_config = False
env.host_string = "host.something.com"
env.user = "myuser"
env.keys_filename = "./private_openssh.key"
env.port = 22
env.gateway = "proxyhost:port"

testlive()

I am running Fabric in the same directory with the private key.

I am able to login on this machine using Putty.

The problem: I am constantly asked for Login password for specified user.

Based on other posts(here and here) I already tried:

  • pass as a list the key file to env.keys_filename
  • use username@host_string
  • use env.host instead of env.host_string

How to properly configure Fabric to deal with proxy server and ssh private key file ?

like image 260
John Smith Avatar asked Feb 16 '17 12:02

John Smith


2 Answers

The following should work.

env.key_filename = "./private_openssh.key"

(notice the typo in your attempt)

like image 198
Alvra Avatar answered Sep 21 '22 11:09

Alvra


Fabric's API is best avoided really, way too many bugs and issues (see issue tracker).

You can do what you want in Python with the following:

from __future__ import print_function

from pssh import ParallelSSHClient
from pssh.utils import load_private_key

client = ParallelSSHClient(['host.something.com'],
                           pkey=load_private_key('private_openssh.key'),
                           proxy_host='proxyhost',
                           proxy_port=<proxy port number>,
                           user='myuser',
                           proxy_user='myuser')
output = client.run_command('uname')
for line in output['host.something.com'].stdout:
    print(line)

ParallelSSH is available from pip as parallel-ssh.

like image 44
danny Avatar answered Sep 21 '22 11:09

danny