gpt4 book ai didi

python - 在机器中使用 Fabric 运行 ssh-add

转载 作者:太空狗 更新时间:2023-10-30 01:23:57 25 4
gpt4 key购买 nike

我正在使用 Fabric 运行一些部署任务,需要将 Mercurial 存储库 check out /更新到机器,然后执行适当的复制/配置。

每次我启动一台新机器(我们目前正在使用 EC2 作为我们的基础设施)或者当我在机器上运行 hg pull 时,它都会询问我的 ssh key 密码,这是一个当我们需要一次初始化十几台机器时有点烦人。

当新的 EC2 实例初始化时,我尝试在 Fabric 中运行 ssh-add 但似乎 ssh-agent 没有为那个 shell 运行我从 Fabric 的输出中收到了 Could not open a connection to your authentication agent. 消息。

当通过 Fabric 脚本连接到实例时,如何使 ssh-add 工作?

最佳答案

A comment在 fabric 的问题跟踪器上为我解决了这个问题。它是 lincolnloop solution 的修改版本.使用此“运行”而不是 fabric 的将通过本地 ssh 传输您的命令,允许您本地的 ssh-agent 提供 key 。

from fabric.api import env, roles, local, output
from fabric.operations import _shell_escape

def run(command, shell=True, pty=True):
"""
Helper function.
Runs a command with SSH agent forwarding enabled.

Note:: Fabric (and paramiko) can't forward your SSH agent.
This helper uses your system's ssh to do so.
"""
real_command = command
if shell:
cwd = env.get('cwd', '')
if cwd:
cwd = 'cd %s && ' % _shell_escape(cwd)
real_command = '%s "%s"' % (env.shell,
_shell_escape(cwd + real_command))
if output.debug:
print("[%s] run: %s" % (env.host_string, real_command))
elif output.running:
print("[%s] run: %s" % (env.host_string, command))
local("ssh -A %s '%s'" % (env.host_string, real_command))

请注意,我正在运行 Fabric 1.3.2,并且不会需要更长的时间进行此修复。

关于python - 在机器中使用 Fabric 运行 ssh-add,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7772373/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com