将 SSH 密钥文件与 Fabric 结合使用

发布于 2024-10-22 06:44:45 字数 51 浏览 3 评论 0原文

如何配置结构以使用 SSH 密钥文件连接到远程主机(例如 Amazon EC2 实例)?

How do you configure fabric to connect to remote hosts using SSH keyfiles (for example, Amazon EC2 instances)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

舟遥客 2024-10-29 06:44:45

由于某种原因,找到一个带有 SSH 密钥文件使用示例的简单 fabfile 并不容易。我写了一篇关于它的博客文章具有匹配的要点)。

基本上,用法是这样的:

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 配置可以在连接时查找它。

Finding a simple fabfile with a working example of SSH keyfile usage isn't easy for some reason. I wrote a blog post about it (with a matching gist).

Basically, the usage goes something like this:

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')

The important part is setting the env.key_filename environment variable, so that the Paramiko configuration can look for it when connecting.

一梦等七年七年为一梦 2024-10-29 06:44:45

这里还值得一提的是,您可以使用命令行参数来执行此操作:

fab command -i /path/to/key.pem [-H [user@]host[:port]]

Also worth mentioning here that you can use the command line args for this:

fab command -i /path/to/key.pem [-H [user@]host[:port]]
辞取 2024-10-29 06:44:45

Fabric 1.4 提供的另一个很酷的功能 - Fabric 现在支持 SSH配置

如果您的 ~/.ssh/config 文件中已包含所有 SSH 连接参数,Fabric 将原生支持它,您所需要做的就是

env.use_ssh_config = True

在 fabfile 的开头添加: 。

Another cool feature available as of Fabric 1.4 - Fabric now supports SSH configs.

If you already have all the SSH connection parameters in your ~/.ssh/config file, Fabric will natively support it, all you need to do is add:

env.use_ssh_config = True

at the beginning of your fabfile.

冷月断魂刀 2024-10-29 06:44:45

对于 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(
        '[email protected]',
        '[email protected]',
        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

For fabric2 in fabfile use the following:

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')

and run it with:

fab staging do_something_remote

UPDATE:
For multiple hosts (one host will do also) you can use this:

from fabric2 import task, SerialGroup

@task
def staging(ctx):
    conns = SerialGroup(
        '[email protected]',
        '[email protected]',
        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)

and run it with fab or fab2:

fab staging stop
燃情 2024-10-29 06:44:45

对我来说,以下内容不起作用:

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=["[email protected]"]

env.key_filename=['keyfileq.pem']
env.host_string="[email protected]"

For me, the following didn't work:

env.user=["ubuntu"]
env.key_filename=['keyfile.pem']
env.hosts=["xxx-xx-xxx-xxx.ap-southeast-1.compute.amazonaws.com"]

or

fab command -i /path/to/key.pem [-H [user@]host[:port]]

However, the following did:

env.key_filename=['keyfile.pem']
env.hosts=["[email protected]"]

or

env.key_filename=['keyfileq.pem']
env.host_string="[email protected]"
不再让梦枯萎 2024-10-29 06:44:45

我今天必须这样做,我的 .py 文件尽可能简单,就像 @YuvalAdam 的答案中发布的文件一样,但我仍然不断收到输入密码的提示......

看看 paramiko (fabric 用于 ssh 的库)日志,我发现了这一行:

不兼容的 ssh 对等点(没有可接受的 kex 算法)

我更新了 paramiko

sudo pip install paramiko --upgrade

现在它可以工作了。

I had to do this today, my .py file was as simple as possible, like the one posted in the answer of @YuvalAdam but still I kept getting prompted for a password...

Looking at the paramiko (the library used by fabric for ssh) log, I found the line:

Incompatible ssh peer (no acceptable kex algorithm)

I updated paramiko with:

sudo pip install paramiko --upgrade

And now it's working.

和我恋爱吧 2024-10-29 06:44:45

这些答案在 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.... 

None of these answers worked for me on py3.7, fabric2.5.0 and paramiko 2.7.1.

However, using the PKey attribute in the documentation does work: 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.... 
倾城泪 2024-10-29 06:44:45

如上所述,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。

As stated above, Fabric will support .ssh/config file settings after a fashion, but using a pem file for ec2 seems to be problematic. IOW a properly setup .ssh/config file will work from the command line via 'ssh servername' and fail to work with 'fab sometask' when env.host=['servername'].

This was overcome by specifying the env.key_filename='keyfile' in my fabfile.py and duplicating the IdentityFile entry already in my .ssh/config.

This could be either Fabric or paramiko, which in my case was Fabric 1.5.3 and Paramiko 1.9.0.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文