Gee*_*eek 4 python ssh fabric ssh-tunnel
我有一个nat,它有各种服务器所以从我的本地服务器我想去nat然后从nat我必须ssh到其他机器
本地 - > NAT(abcuser @ publicIP with key 1) - > server1(xyzuser @ localIP with key 2)nat有不同的ssh密钥,每个服务器都有不同的ssh密钥如何使用fabric完成这种类型的multihop ssh我尝试使用env.roledefs功能但它似乎也没有工作我也不知道如何定义两个ssh密钥.我知道我们可以用env.key_filename定义一个密钥列表但问题是它会检查每个服务器的每个密钥?我如何才能更具体,并且只将键与一台服务器匹配
我尝试使用来自我本地机器的命令 fab deploy -g'ec2-user@54.251.151.39'-i'/ home/aman/Download/aws_oms.pem' ,我的脚本是
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")
Run Code Online (Sandbox Code Playgroud)
要通过中间服务器连接到远程主机,可以使用--gateway命令行选项:
http://docs.fabfile.org/en/latest/usage/fab.html#cmdoption-g
或者,或者,env.gateway在fabfile中设置变量:
http://docs.fabfile.org/en/latest/usage/env.html#gateway
有关更多详细信息,请参阅:
http://docs.fabfile.org/en/stable/concepts/networking.html#ssh-gateways
这是可能的。10.0.0.2通过网关 hop双跳到(并列出文件)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)
conn2.close()
conn1.close()
msg = "Ran {0.command!r} on {0.connection.host}, got stdout:\n{0.stdout}"
print(msg.format(result))
Run Code Online (Sandbox Code Playgroud)
请记得手动运行一次 SSH 连接,以便相互介绍服务器!
通过安装
pip3 install --upgrade fabric
pip3 install cryptography==2.4.2 # optional to hide some annoying warnings
Run Code Online (Sandbox Code Playgroud)
http://docs.fabfile.org/en/latest/concepts/networking.html
Python 3.6+。
| 归档时间: |
|
| 查看次数: |
4133 次 |
| 最近记录: |