Python结构响应输出中的提示 [英] Python fabric respond to prompts in output

查看:87
本文介绍了Python结构响应输出中的提示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要用python的结构解决两个问题.

I have two problems I am trying to solve with fabric for python.

  1. 我想在提示符下自动输入密码:
  1. I'd like to auto-enter the password at the prompt:
    • fabric password

以上链接所示,这两种方法都应该可行,但我无法使它们正常工作.当我通过fab test

Both of these should be possible as the above links show, but I can't get them to work. When I run the following file via 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")

我的输出是:

[<user>@<host>] Login password for '<user>':

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

At which point I have to type my password, instead of auto-entering. Then per the remote host I get a menu which I want to auto respond to, but my prompt's auto-complete doesn't work.

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

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

What do I need to do to get this to auto-enter my password and auto-answer this prompt?

推荐答案

如果您查看

这本词典主要供内部使用,并且已填充 自动作为每个主机字符串的密码缓存.密钥是完整主机 字符串和值是密码(字符串).

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).

重要说明是上述警告:

警告

如果您手动修改或生成此字典,则必须使用 具有用户和端口值的标准主机字符串.见链接 上面有关主机字符串API的详细信息.

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并查看其是否有效.

Following the above warning, make sure that the hostname is FQDN and perhaps even add the port number :22 and see if it works.

关于提示,只要您在with块内运行命令 (我无法从您的示例代码中得知),该命令就应该起作用.另外,请检查提示后是否有空格,如果没有,请从prompts键中将其删除.

As for the prompt, that should have worked as long as you ran the command inside the with block (which I can't tell from your sample code). Also check if the prompt is followed by a space, if not remove it from your prompts key.

这篇关于Python结构响应输出中的提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆