使用 Fabric 时连接到 ~/.ssh/config 中列出的主机

发布于 2024-09-06 11:07:41 字数 1080 浏览 2 评论 0原文

我在使用 Fabric 无法识别 ~/.ssh/config 中的主机。

我的 fabfile.py 如下:

from fabric.api import run, env

env.hosts = ['lulu']

def whoami():
    run('whoami')

运行 $ fab whoami 给出:

[lulu]运行:whoami

致命错误:名称查找失败 露露

名称 lulu 位于我的 ~/.ssh/config 中,如下所示:

Host lulu
     hostname 192.168.100.100
     port 2100
     IdentityFile ~/.ssh/lulu-key

我解决此问题的第一个想法是添加类似 lulu.lulu 的内容> 到 /etc/hosts (我在 Mac 上),但随后我还必须将身份文件传递给 Fabric - 而且我宁愿保留我的身份验证(即 ~ /.ssh/config)与我的部署(即fabfile.py)分开。

另外,顺便说一句,如果您尝试连接到主机文件中的主机,fabric.contrib.projects.rsync_project 似乎不承认 hosts.env< 中的“端口” /code> (即,如果您使用 hosts.env = [lulu:2100] 调用 rsync_project 似乎会尝试连接到 lulu:21 )。

Fabric 无法识别这个 lulu 名称是否有原因?

I'm having trouble with Fabric not recognizing hosts that I have in ~/.ssh/config.

My fabfile.py is as follows:

from fabric.api import run, env

env.hosts = ['lulu']

def whoami():
    run('whoami')

Running $ fab whoami gives:

[lulu] run: whoami

Fatal error: Name lookup failed for
lulu

The name lulu is in my ~/.ssh/config, like this:

Host lulu
     hostname 192.168.100.100
     port 2100
     IdentityFile ~/.ssh/lulu-key

My first thought to solving this is adding something like lulu.lulu to /etc/hosts (I'm on a Mac), but then I have to also pass in the identity file to Fabric - and I'd rather keep my authentication (i.e. ~/.ssh/config) separate from my deployment (i.e. fabfile.py).

As well, incidentally, if you try to connect to a host in the hosts file, fabric.contrib.projects.rsync_project doesn't seem to acknowledge 'ports' in the hosts.env (i.e. if you use hosts.env = [lulu:2100] a call to rsync_project seems to try connecting to lulu:21).

Is there a reason Fabric doesn't recognize this lulu name?

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

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

发布评论

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

评论(4

二智少女猫性小仙女 2024-09-13 11:07:41

从版本 1.4.0 开始,Fabric 使用您的 ssh 配置(部分)。但是,您需要在

env.use_ssh_config = True

fabfile 顶部附近的某个位置显式启用它。执行此操作后,Fabric 应该读取您的 ssh 配置(默认从 ~/.ssh/config 或从 env.ssh_config_path)。

一个警告:如果您使用早于 1.5.4 的版本,并且设置了 env.use_ssh_config 但不存在配置文件,则会发生中止。在这种情况下,您可以使用如下解决方法:

if env.ssh_config_path and os.path.isfile(os.path.expanduser(env.ssh_config_path)):
    env.use_ssh_config = True

Since version 1.4.0, Fabric uses your ssh config (partly). However, you need to explicitly enable it, with

env.use_ssh_config = True

somewhere near the top of your fabfile. Once you do this, Fabric should read your ssh config (from ~/.ssh/config by default, or from env.ssh_config_path).

One warning: if you use a version older than 1.5.4, an abort will occur if env.use_ssh_config is set but there is no config file present. In that case, you can use a workaround like:

if env.ssh_config_path and os.path.isfile(os.path.expanduser(env.ssh_config_path)):
    env.use_ssh_config = True
可可 2024-09-13 11:07:41

请注意,当名称不在 /etc/hosts 中时也会发生这种情况。我遇到了同样的问题,必须将主机名添加到该文件和 ~/.ssh/config 中。

Note that this also happens when the name is not in /etc/hosts. I had the same problem and had to add the host name to both that file and ~/.ssh/config.

携君以终年 2024-09-13 11:07:41

更新:此答案现已过时


Fabric 目前不支持 .ssh/config 文件。您可以在函数中设置它们,然后在 cli 上调用,例如:fab 生产任务;其中 Production 设置用户名、主机名、端口和 ssh 身份。

至于 rsync 项目,现在应该具有端口设置能力,如果没有,您可以随时运行 local("rsync ..."),因为这本质上就是贡献函数的作用。

update: This Answer is now outdated.


Fabric doesn't have support currently for the .ssh/config file. You can set these up in a function to then call on the cli, eg: fab production task; where production sets the username, hostname, port, and ssh identity.

As for rsync project, that should now have port setting ability, if not, you can always run local("rsync ...") as that is essentially what that contributed function does.

撞了怀 2024-09-13 11:07:41

可以使用以下代码来读取配置(原始代码取自:http://markpasc.typepad.com/blog/2010/04/loading-ssh-config-settings-for-fabric.html):

from fabric.api import *
env.hosts = ['servername']

def _annotate_hosts_with_ssh_config_info():
    from os.path import expanduser
    from paramiko.config import SSHConfig

    def hostinfo(host, config):
        hive = config.lookup(host)
        if 'hostname' in hive:
            host = hive['hostname']
        if 'user' in hive:
            host = '%s@%s' % (hive['user'], host)
        if 'port' in hive:
            host = '%s:%s' % (host, hive['port'])
        return host

    try:
        config_file = file(expanduser('~/.ssh/config'))
    except IOError:
        pass
    else:
        config = SSHConfig()
        config.parse(config_file)
        keys = [config.lookup(host).get('identityfile', None)
            for host in env.hosts]
        env.key_filename = [expanduser(key) for key in keys if key is not None]
        env.hosts = [hostinfo(host, config) for host in env.hosts]

        for role, rolehosts in env.roledefs.items():
            env.roledefs[role] = [hostinfo(host, config) for host in rolehosts]

_annotate_hosts_with_ssh_config_info()

One can use following code to read the config (original code taken from: http://markpasc.typepad.com/blog/2010/04/loading-ssh-config-settings-for-fabric.html):

from fabric.api import *
env.hosts = ['servername']

def _annotate_hosts_with_ssh_config_info():
    from os.path import expanduser
    from paramiko.config import SSHConfig

    def hostinfo(host, config):
        hive = config.lookup(host)
        if 'hostname' in hive:
            host = hive['hostname']
        if 'user' in hive:
            host = '%s@%s' % (hive['user'], host)
        if 'port' in hive:
            host = '%s:%s' % (host, hive['port'])
        return host

    try:
        config_file = file(expanduser('~/.ssh/config'))
    except IOError:
        pass
    else:
        config = SSHConfig()
        config.parse(config_file)
        keys = [config.lookup(host).get('identityfile', None)
            for host in env.hosts]
        env.key_filename = [expanduser(key) for key in keys if key is not None]
        env.hosts = [hostinfo(host, config) for host in env.hosts]

        for role, rolehosts in env.roledefs.items():
            env.roledefs[role] = [hostinfo(host, config) for host in rolehosts]

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