首页IT科技coafeup牙膏(farbic plugin fexpect,bravo!)

coafeup牙膏(farbic plugin fexpect,bravo!)

时间2025-05-26 13:33:33分类IT科技浏览3559
导读:Fexpect: Dealing with prompts in fabric with pexpect...

Fexpect: Dealing with prompts in fabric with pexpect

Tuesday 06 March 2012

Update: Fexpect has been improved and is now on pypi and github (new blogpost)

Fabric is a popular python library for executing commands on a remote computer over ssh. It is commonly used for deployment or provisioning tasks. Often however, these tasks ask for user feedback through a prompt (“Continue? Y/N”). Fabric has no built-in way to code how to deal with such a prompt, which means that execution of the script will pause until the user provides the answer to the prompt.

There are several other tools available to ‘expect’ such a prompt and answer with a scripted response. Among those are the unix tools ‘yes’ and ‘expect’ [1]. Others have suggested to use these for situations that require prompt interaction in fabric scripts. One common use case is for an OS-upgrade, which usually includes several prompts.

A more elegant solution would be an extension of fabric. I recently suggested such a plugin on the fabric mailing list [2], and will describe my solution in more details here. It involves using the python version of ‘expect’, pexpect [3].

In short, it entails compiling a short fexpect script, sending it along with the fexpect library to the target machine using put(), and executing it.

Fabric scripts have to formulate expectations, i.e. prompts, and the corresponding answer (‘y’):

from fexpect import expect expectation = expect("Continue \[yN\]",y)

Notice that we escape the brackets, since the expectation is formulated as a regular expression to match the prompt.

We can combine expectations in a list, since often a command prompts multiple questions:

prompts = [] prompts += expect(‘What is your name?’,Jasper) prompts += expect(Where do you live?,Frankfurt)

And then we run the command with a context manager for the expectations:

from fexpect import expecting, run #or importas erun’ not to confuse with fabric.api.run with expecting(prompts): run(‘command’)

Here are some extracts from the code to see what’s going on:

The context manager just stores the expectations in the fabric state:

class ExpectationContext(object): def __init__(self,expectations): self.expectations = expectations def __enter__(self): env.expectations = self.expectations def __exit__(self, type, value, tb): env.expectations = []

A custom run function (sudo also available):

def run(cmd): #sudo wrapper wrappedCmd = wrapExpectations(cmd,env) return fabric.api.run(wrappedCmd)

Which calls the actual ‘magic’:

def wrapExpectations(cmd,env): script = createScript(cmd,env) remoteScript = /tmp/fexpect_+shortuuid.uuid() fabric.api.put(resource(pexpect.py),/tmp/) fabric.api.put(StringIO(script),remoteScript) wrappedCmd = python +remoteScript return wrappedCmd

Which in turn calls a simple function which creates the pexpect script.

Get the complete module and some tests at github: fexpect

Links:

[1] unix expect [2] original post to fab-user@nongnu.org [3] pexpect

Comments? Write me an email or discuss on facebook, google+ or twitter.

创心域SEO版权声明:以上内容作者已申请原创保护,未经允许不得转载,侵权必究!授权事宜、对本内容有异议或投诉,敬请联系网站管理员,我们将尽快回复您,谢谢合作!

展开全文READ MORE
win10电脑防火墙在哪里关闭(win10电脑防火墙怎么关闭) 苹果电脑如何wifi共享(Mac怎么共享wifi网络?苹果电脑wifi共享教程)