It's possible. Double hop to 10.0.0.2
(and list files) via gateway hop 10.0.0.1
. Basically, you simply nest the connections with the gateway
parameter.
# 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)
conn2.close()
conn1.close()
msg = "Ran {0.command!r} on {0.connection.host}, got stdout:\n{0.stdout}"
print(msg.format(result))
Please remember to run the SSH connection manually once to introduce the servers to each other!
Install via
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+.