Fabric 2.5:无法向目标主机提供 sudo 密码

问题描述 投票:0回答:2

我正在尝试学习 Fabric 2.5,但我很挣扎。我读了很多页,试图忽略那些涉及旧织物版本的内容。

我运行以下命令,得到

The password submitted to prompt '[sudo] password: ' was rejected.

有人可以建议我做错了什么吗?

(f5) albe@vamp398:/srv/file/f5$ fab tt --prompt-for-login-password --prompt-for-sudo-password
Desired 'sudo.password' config value:
Enter login password for use with SSH auth:
Linux vamp398 4.4.0-116-generic #140-Ubuntu SMP Mon Feb 12 21:23:04 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
albe
[sudo] password: Sorry, try again.
[sudo] password: Traceback (most recent call last):
  File "/srv/file/f5/bin/fab", line 8, in <module>
    sys.exit(program.run())
  File "/srv/file/f5/lib/python3.5/site-packages/invoke/program.py", line 384, in run
    self.execute()
  File "/srv/file/f5/lib/python3.5/site-packages/invoke/program.py", line 566, in execute
    executor.execute(*self.tasks)
  File "/srv/file/f5/lib/python3.5/site-packages/invoke/executor.py", line 129, in execute
    result = call.task(*args, **call.kwargs)
  File "/srv/file/f5/lib/python3.5/site-packages/invoke/tasks.py", line 127, in __call__
    result = self.body(*args, **kwargs)
  File "/srv/file/f5/fabfile.py", line 33, in tt
    c.sudo('whoami')
  File "/srv/file/f5/lib/python3.5/site-packages/invoke/context.py", line 173, in sudo
    return self._sudo(runner, command, **kwargs)
  File "/srv/file/f5/lib/python3.5/site-packages/invoke/context.py", line 226, in _sudo
    raise_from(error, None)
  File "<string>", line 2, in raise_from
invoke.exceptions.AuthFailure: The password submitted to prompt '[sudo] password: ' was rejected.
(f5) albe@vamp398:/srv/file/f5$ a
a: command not found
(f5) albe@vamp398:/srv/file/f5$

我的 fabfile.py 是:

# import getpass
# from fabric import Connection, Config
# from invocations.console import confirm
from fabric import Connection
from invoke import Exit
from fabric import task


# noworky

# env.user = "albe"
# env.password = "a"
# sudo_pass = getpass.getpass("What's your sudo password?")
# config = Config(overrides={'sudo': {'password': sudo_pass}})
# c = Connection(host='192.168.88.64', user='albe', config=config)
# c = Connection(host='192.168.88.64', user='albe')
# c = Connection(host="[email protected]")
    # c.sudo('whoami', hide='stderr')
# c = Connection(host="192.168.88.64",user="albe" , connect_kwargs={"password":"a", "sudo.password":"a"})

# maybe works

# c = Connection(host="192.168.88.64",user='albe', connect_kwargs={"password": "a"})

# fab tt --prompt-for-login-password --prompt-for-sudo-password
c = Connection(host='192.168.88.64', user='albe')

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

在 Ubuntu 16.04 上我像这样设置 Fabric 2.5..

cd /srv/file
sudo apt-get install python3-venv
    python3 -m venv f5
    cd f5
    source bin/activate
echo fabric>>requirements.txt
sudo chown -R albe:  /home/albe/.cache
pip3 install --upgrade pip
pip3 install -r requirements.txt
pip3 list

我的点子列表是:

(f5) albe@vamp398:/srv/file/f5$ pip3 list
Package       Version
------------- -------
bcrypt        3.1.7
cffi          1.14.1
cryptography  3.0
fabric        2.5.0
invoke        1.4.1
paramiko      2.7.1
pip           20.2.1
pkg-resources 0.0.0
pycparser     2.20
PyNaCl        1.4.0
setuptools    20.7.0
six           1.15.0

这些页面似乎最相关。

https://docs.fabfile.org/en/2.5/getting-started.html

https://www.fabfile.org/upgrading.html#the-whole-thing

python ubuntu fabric
2个回答
3
投票

文档中的示例显示了如何使用

Connection()
,但它们总是在没有
@task
的情况下显示它,因为当您使用
@task
时,它会使用您在命令行中使用的值自动创建自己的
context
并进行连接。

如果您在

c = Connection(...)
中放入一些随机值并在
print(c)
中使用
task
,那么您会发现它不使用您的
c = Connection(...)

kwargs = {
    "password": 'random_password',
}
ctx = Connection(host='random_host', user='random_user', connect_kwargs=kwargs)    

@task
def one(ctx):
    
    print(ctx)
    
    print('connect_kwargs:', ctx['connect_kwargs'])
    print('user:', ctx['user'])
    
    print('sudo user:', ctx['sudo']['user'])
    print('sudo password:', ctx['sudo']['password'])
        
    #ctx.run('uname -a')
    #ctx.run('whoami')
    #ctx.sudo('whoami')

您最终可以在

ctx
内创建自己的
task
来替换现有的

def create_ctx():
    password = getpass.getpass("password: ")

    kwargs = {
        "password": password,
    }

    ctx = Connection(host='192.168.88.64', user='albe', connect_kwargs=kwargs)

    return ctx

@task
def two(ctx):
    
    ctx = create_ctx() # replace original `ctx` with own `ctx`

    print(ctx)
    
    print('connect_kwargs:', ctx['connect_kwargs'])
    print('user:', ctx['user'])
    
    print('sudo user:', ctx['sudo']['user'])
    print('sudo password:', ctx['sudo']['password'])
          
    #ctx.run('uname -a')
    #ctx.run('whoami')
    #ctx.sudo('whoami')

您还可以替换现有的值

ctx

@task
def three(ctx0): #, password='xxx', sudo='yyy'):
    
    password = getpass.getpass("password: ")
    sudo = getpass.getpass("sudo: ")

    ctx['user'] = 'pi'
    ctx['connect_kwargs']['password'] = password
    ctx['sudo']['password'] = sudo
    
    print(ctx)
    
    print('connect_kwargs:', ctx['connect_kwargs'])
    print('user:', ctx['user'])
    
    print('sudo user:', ctx['sudo']['user'])
    print('sudo password:', ctx['sudo']['password'])
          
    #ctx.run('uname -a')
    #ctx.run('whoami')
    #ctx.sudo('whoami')

如果我使用,代码对我有用

 fab tt --prompt-for-login-password --prompt-for-sudo-password

或者首先使用参数(应该设置全局值)

 fab --prompt-for-login-password --prompt-for-sudo-password  tt

所以我只能来客人你写错密码了。


0
投票

我发现的一件事是您需要分别传递密码和

sudo
密码。

from fabric import Connection, Config

# we need this to login
connect_kwargs = {"password":"hello123"}
# needed for sudo commands
config = Config(overrides={"sudo":{"password":"hello123"}})

conn = Connection(host="127.0.0.1", user="example", config=config, connect_kwargs=connect_kwargs)
conn.run("ls")
conn.sudo("ls")
© www.soinside.com 2019 - 2024. All rights reserved.