Python结构响应outpu中的提示

2024-04-23 21:56:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用fabric for python解决两个问题。在

  1. 我想在提示下自动输入密码:
  2. 基于会话的自动响应提示:

这两个应该是可能的,因为上面的链接显示,但我不能让他们工作。当我通过fab test运行以下文件时

from fabric.api import run, env, settings

env.hosts = ['<user>@<host>']
env.passwords = {'<user>@<host>': '<password>'}

def test():
    with settings(prompts={"Please select a menu item: ": "1",}):
        return
    run("ls")

我的输出是:

^{pr2}$

此时,我必须键入密码,而不是自动输入。然后根据远程主机,我会得到一个菜单,我想自动响应,但我的提示符自动完成功能不起作用。在

[<user>@<host>] out: <a menu>
[<user>@<host>] out: Please select a menu item: 

我需要怎么做才能自动输入我的密码并自动回答这个提示?在


Tags: toruntestenvhost密码settingswith
1条回答
网友
1楼 · 发布于 2024-04-23 21:56:44

如果您查看env.passwords上的文档,它会说:

This dictionary is largely for internal use, and is filled automatically as a per-host-string password cache. Keys are full host strings and values are passwords (strings).

值得注意的是,上述警告如下:

Warning

If you modify or generate this dict manually, you must use fully qualified host strings with user and port values. See the link above for details on the host string API.

出现上述警告后,请确保主机名是FQDN,甚至可以添加端口号:22,并查看它是否有效。在

至于提示,只要您在with块中运行命令,它就应该起作用了(从示例代码中我看不出来)。还要检查提示后面是否有空格,如果没有,请将其从prompts键中删除。在

相关问题 更多 >