How to make multihop ssh with fabric

I have nat and it has different servers. Therefore, from my local server I want to switch to nat, and then from nat I have to ssh to other machines

Local -> NAT (abcuser @publicIP with key 1) -> server1 (xyzuser @localIP with key 2) nat has a different ssh key and each server has a different ssh key how can I do this type of multihop ssh using fabric I tried to use the env function . roledefs , but it doesn’t seem to work either. I'm not sure how to define two ssh keys. I know that we can define a list of keys with env.key_filename, but the problem is checking each key with each server? How can I be more specific and map the key to one server only

I tried using the command from my local machine fab deploy -g ' ec2-user@54.251.151.39 ' -i '/home/aman/Downloads/aws_oms.pem' and my script

from __future__ import with_statement
from fabric.api import local, run, cd, env, execute
env.hosts=['ubuntu@10.0.0.77']
env.key_filename=['/home/ec2-user/varnish_cache.pem']
def deploy():
    run("uname -a")
+4
source share
2 answers

To connect to remote hosts through an intermediate server, you can use the command line option --gateway:

http://docs.fabfile.org/en/latest/usage/fab.html#cmdoption-g

Or, alternatively, set a variable env.gatewayinside your fabfile:

http://docs.fabfile.org/en/latest/usage/env.html#gateway

For more information, see

http://docs.fabfile.org/en/stable/concepts/networking.html#ssh-gateways

+5

. 10.0.0.2 ( ) 10.0.0.1 10.0.0.1. , gateway.

# coding: utf-8

from fabric import Connection

path = '/'
conn1 = Connection(host='user1@10.0.0.1', connect_kwargs={'password': '***'})
conn2 = Connection(host='user2@10.0.0.2', connect_kwargs={'password': '***'}, gateway=conn1)
result = conn2.run(f'''cd {path} && ls -al''', hide=True)
msg = "Ran {0.command!r} on {0.connection.host}, got stdout:\n{0.stdout}"
print(msg.format(result))

, SSH- , !

pip3 install --upgrade fabric
pip3 install cryptography==2.4.2  # optional to hide some annoying warnings

http://docs.fabfile.org/en/latest/concepts/networking.html

Python 3. 6+.

0

All Articles