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

时间:2019-05-23 19:28:15

标签: 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:验证失败。

在终端机上$ ssh my_host工作时。

我尝试用相同的结果来完成fab -i ~/.keys/somekey tt

2 个答案:

答案 0 :(得分:0)

Fabric accepts a hosts iterable as parameters in tasks. Per the documentation:

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.

One of the members of which could be:

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.

As for your example, please try this for fabfile.py:

host_list = ["my_host"]

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

Alternatively, you can omit the host declaration from the fabfile altogether. If you don't specify the host in fabfile.py, you can simply specify it as a host when invoking the fab cli utility. If your fabfile.py is this:

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

You would now run fab -H my_host tt to run it on the alias tt from your SSH client config.

Hope this helps.

答案 1 :(得分:0)

同样的问题。

运行Fabric时,您可以尝试添加-d以获得更多详细信息:

fab2 -d tt

我发现异常:paramiko.ssh_exception.SSHException: Invalid key,然后从服务器重新生成密钥,问题已解决。