【发布时间】:2020-03-17 13:27:20
【问题描述】:
如何配置结构以使用 SSH 密钥文件(例如,Amazon EC2 实例)连接到远程主机?
【问题讨论】:
如何配置结构以使用 SSH 密钥文件(例如,Amazon EC2 实例)连接到远程主机?
【问题讨论】:
由于某种原因,找到一个简单的带有 SSH 密钥文件使用示例的简单 fabfile 并不容易。我为此写了一个blog post (with a matching gist)。
基本上,用法是这样的:
from fabric.api import *
env.hosts = ['host.name.com']
env.user = 'user'
env.key_filename = '/path/to/keyfile.pem'
def local_uname():
local('uname -a')
def remote_uname():
run('uname -a')
重要的部分是设置env.key_filename环境变量,以便Paramiko配置在连接时可以查找。
【讨论】:
env.key_filename can contain a list of strings 尝试使用多个密钥文件进行连接。
settings 上下文管理器以编程方式设置密钥,直到我将key_filename='/path/to/key' 更改为key_filename=['/path/to/key'] 之前,它无法识别key_filename,所以如果有其他人遇到问题,将 key_filename 设置为密钥列表可能会解决它。这是 fab 1.10.1 和 Paramiko 1.15.2
这里还值得一提的是,您可以为此使用命令行参数:
fab command -i /path/to/key.pem [-H [user@]host[:port]]
【讨论】:
Fabric 1.4 提供的另一个很酷的功能 - Fabric now supports SSH configs。
如果您的~/.ssh/config 文件中已经包含所有 SSH 连接参数,Fabric 将原生支持它,您只需添加:
env.use_ssh_config = True
在您的 fabfile 的开头。
【讨论】:
IOError: [Errno 2] No such file or directory: ' /path/to/.ssh/key' 或Login password for ' root': 之类的错误,请确保您的.ssh/config 中没有空格。例如 User=root 而不是 User = root...
对于 fabfile 中的 fabric2,请使用以下内容:
from fabric import task, Connection
@task
def staging(ctx):
ctx.name = 'staging'
ctx.user = 'ubuntu'
ctx.host = '192.1.1.1'
ctx.connect_kwargs.key_filename = os.environ['ENV_VAR_POINTS_TO_PRIVATE_KEY_PATH']
@task
def do_something_remote(ctx):
with Connection(ctx.host, ctx.user, connect_kwargs=ctx.connect_kwargs) as conn:
conn.sudo('supervisorctl status')
然后运行它:
fab staging do_something_remote
更新:
对于多台主机(一台主机也可以),您可以使用:
from fabric2 import task, SerialGroup
@task
def staging(ctx):
conns = SerialGroup(
'user@10.0.0.1',
'user@10.0.0.2',
connect_kwargs=
{
'key_filename': os.environ['PRIVATE_KEY_TO_HOST']
})
ctx.CONNS = conns
ctx.APP_SERVICE_NAME = 'google'
@task
def stop(ctx):
for conn in ctx.CONNS:
conn.sudo('supervisorctl stop ' + ctx.APP_SERVICE_NAME)
并使用 fab 或 fab2 运行它:
fab staging stop
【讨论】:
staging任务中支持多个主机?
对我来说,以下方法不起作用:
env.user=["ubuntu"]
env.key_filename=['keyfile.pem']
env.hosts=["xxx-xx-xxx-xxx.ap-southeast-1.compute.amazonaws.com"]
或
fab command -i /path/to/key.pem [-H [user@]host[:port]]
但是,以下情况发生了:
env.key_filename=['keyfile.pem']
env.hosts=["ubuntu@xxx-xx-xxx-xxx-southeast-1.compute.amazonaws.com"]
或
env.key_filename=['keyfileq.pem']
env.host_string="ubuntu@xxx-xx-xxx-xxx.ap-southeast-1.compute.amazonaws.com"
【讨论】:
env.user="ubuntu" 而不是env.user=["ubuntu"],您的第一个示例对我有用。
我今天必须这样做,我的 .py 文件尽可能简单,就像在@YuvalAdam 的答案中发布的那样,但我仍然不断收到提示输入密码...
查看paramiko(fabric for ssh 使用的库)日志,找到了一行:
不兼容的 ssh 对等体(没有可接受的 kex 算法)
我将paramiko 更新为:
sudo pip install paramiko --upgrade
现在它开始工作了。
【讨论】:
这些答案都不适用于 py3.7、fabric2.5.0 和 paramiko 2.7.1。
但是,使用文档中的 PKey 属性确实有效:http://docs.fabfile.org/en/2.5/concepts/authentication.html#private-key-objects
from paramiko import RSAKey
ctx.connect_kwargs.pkey = RSAKey.from_private_key_file('path_to_your_aws_key')
with Connection(ctx.host, user, connect_kwargs=ctx.connect_kwargs) as conn:
//etc....
【讨论】:
如上所述,Fabric 将支持 .ssh/config 文件设置,但使用 ec2 的 pem 文件似乎有问题。 IOW 正确设置的 .ssh/config 文件将通过 'ssh servername' 从命令行工作,并且在 env.host=['servername'] 时无法使用 'fab sometask'。
通过在我的 fabfile.py 中指定 env.key_filename='keyfile' 并复制我的 .ssh/config 中已有的 IdentityFile 条目来克服这个问题。
这可以是 Fabric 或 paramiko,在我的例子中是 Fabric 1.5.3 和 Paramiko 1.9.0。
【讨论】: