如何在Fabric Python中处理交互式shell [英] how to handle interactive shell in fabric python

查看:242
本文介绍了如何在Fabric Python中处理交互式shell的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

1.在执行Expect('OPR>','show alef-users')之后,它变为无穷大 继续运行.

1.after executing expect('OPR>','show alef-users') it go to infinite means it run continuesly.

from fabric.api import *
from fabric.context_managers import settings
from ilogue.fexpect import expect, expecting, run

prompts = []
prompts += expect('Username:','kirti')
prompts += expect('Password:','kirti')
prompts += expect('OPR>','show users')
prompts +=expect('OPR>','exit')
env.password = "kirti@123"
with cd('/home/kirti/opr'):
with expecting(prompts):
run('./kirti', combine_stderr=False)

推荐答案

Fabric> = 1.9允许在单个with子句中同时处理多个提示:您只需要使用Fabric的settings函数,并且用字典喂它;键将是问题,值将是答案.就您而言,它看起来像:

Fabric >= 1.9 allows to handle multiple prompts at the same time, in a single with clause: you just have use Fabric's settings function, and feed it with a dict; the keys will be the questions, and the values will be the answers. In your case, it will look like :

with settings(prompts={'Username:':'admin', 
                       'Password:':'admin', 
                       'OPR>':'show alef-users', 
                       'OPR>':'exit'}):
    run('./go_opr_cli', combine_stderr=False)

我的答案受到了 Fabric的该部分的启发官方文件.这对我来说效果很好,但是在编写dict的键时,请确保在命令行中遵守问题的最后一个字符和光标之间的空格数,否则它将失败.

My answer is inspired by that section of Fabric's official documentation. It worked very well for me, but make sure, when writing the dict's keys, that you respect the number of spaces between the last character of the question and the cursor, in the command line, otherwise it will fail.

示例:最后一个提示是"OPR>".如果您在常规Linux CLI中看到'OPR>'和光标之间有一个空格,则键应为'OPR>'

Example: The last prompt is 'OPR>'. If you see in the regular Linux CLI, that there is one space between 'OPR>' and the cursor, then the key should be 'OPR> '

这篇关于如何在Fabric Python中处理交互式shell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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