python - 使用身份的Fabrics 2.x ssh连接无法正常工作

标签 python ssh fabric python-fabric-2

尝试使用结构2和身份文件连接到ssh config中描述的主机。

con = Connection('my_host')
@task
def tt(c):
    con.run('uname -a')

〜/ .ssh / config:
Host my_host
    HostName 123.144.76.84
    User ubuntu
    IdentityFile ~/.keys/somekey

它失败了

paramiko.ssh_exception.AuthenticationException: Authentication failed.



虽然终端上的$ ssh my_host有效。

我试图用相同的结果做fab -i ~/.keys/somekey tt

最佳答案

构造accepts a hosts iterable作为任务中的参数。根据文档:

An iterable of host-connection specifiers appropriate for eventually instantiating a Connection. The existence of this argument will trigger automatic parameterization of the task when invoked from the CLI, similar to the behavior of --hosts.



其成员之一可以是:

A string appropriate for being the first positional argument to Connection - see its docs for details, but these are typically shorthand-only convenience strings like hostname.example.com or user@host:port.



对于您的示例,请对fabfile.py尝试以下操作:
host_list = ["my_host"]

@task(hosts=host_list)
def tt(c):
    c.run('uname -a')

或者,您可以完全省略fabfile中的主机声明。如果未在fabfile.py中指定主机,则只需在调用fab cli实用程序时将其指定为主机。如果您的fabfile.py是这样的:
@task
def tt(c):
    c.run('uname -a')

现在,您将运行fab -H my_host tt,以通过SSH客户端配置在别名tt上运行它。

希望这可以帮助。

关于python - 使用身份的Fabrics 2.x ssh连接无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56281810/

相关文章:

python - 如何在循环中检查索引值而不增加索引?

python - 有没有办法在 Atom 中使用多个版本的 Python 运行 Python 脚本?

ssh - autossh 不工作并且运行时没有任何错误消息

python - ssh在终端中工作,但不能通过Python

python - 在 Python/Bash 中通过 SSH/Sudo 测试文件/目录是否存在

python - Python web项目: development,打包、部署剖析

python - 自动编码器 : accuracy & number of images

python - Python 中的数组是按值分配还是按引用分配?

linux - 无法通过 ssh 在 x11 窗口中打开远程桌面

python 织物: put() results in permission denied