当我使用fabric == 2.4.0'sudo helper getpass.getpass'时如何解决'没有可用的身份验证方法'

时间:2019-02-14 09:55:28

标签: python fabric

当我使用python结构模块时,请按照以下教程操作:单击here来连接远程主机,并执行shell命令,但发生错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<decorator-gen-4>", line 2, in sudo
  File "/root/tempFabric/env/lib/python2.7/site-packages/fabric/connection.py", line 29, in opens
    self.open()
  File "/root/tempFabric/env/lib/python2.7/site-packages/fabric/connection.py", line 615, in open
    self.client.connect(**kwargs)
  File "/root/tempFabric/env/lib/python2.7/site-packages/paramiko/client.py", line 437, in connect
    passphrase,
  File "/root/tempFabric/env/lib/python2.7/site-packages/paramiko/client.py", line 750, in _auth
    raise SSHException("No authentication methods available")
paramiko.ssh_exception.SSHException: No authentication methods available
$ import getpass
$ from fabric import Connection, Config
$ sudo_pass = getpass.getpass("123456")
123456
$ config = Config(overrides={'sudo': {'password': sudo_pass}})
$ c=Connection(host='192.168.0.156',user='ai',port=22,config=config)
$ c.sudo('whoami', hide='stderr')

我希望c.sudo('whoami', hide='stderr')的输出为:

ai
<Result cmd="...whoami" exited=0>

1 个答案:

答案 0 :(得分:0)

尝试一下

from fabric import Connection
result = Connection('web1', user='hello', connect_kwargs={
    "password": "123456",
    }, port=22).run('uname -s', hide=True)
msg = "Ran {0.command!r} on {0.connection.host}, got stdout:\n{0.stdout}"
print(msg.format(result))