gpt4 book ai didi

python - 结构 2.5 : Trouble supplying sudo password to target host

转载 作者:行者123 更新时间:2023-12-04 18:37:24 29 4
gpt4 key购买 nike

我正在尝试学习 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="albe@192.168.88.64")
# 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

最佳答案

文档中的示例展示了如何使用 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
所以我只能邀请你写错密码。

关于python - 结构 2.5 : Trouble supplying sudo password to target host,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63350485/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com