Python Fabric - 有没有办法捕获运行标准输出?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21443690/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 22:51:37  来源:igfitidea点击:

Fabric - Is there any way to capture run stdout?

pythonfabric

提问by RadiantHex

I'm trying to do the following:

我正在尝试执行以下操作:

output = run("ls -l backups")
for line in output.split("/n"):
    do_stuff(line)

Any way of having the stdoutof lssent to output?

具有任何方式stdoutls发送到output



To be more specific I'm using a CLI app called s3cmdwhich does something similar to ls, but with remote Amazon S3 buckets.

更具体地说,我正在使用一个名为的 CLI 应用程序s3cmd,它执行类似于 的操作ls,但使用远程 Amazon S3 存储桶。

So a replacement for lswon't help unfortunately.

因此,ls不幸的是,替换将无济于事。



回答by JCotton

Exactly what you are asking for should be happening. From the docs:

正是您所要求的应该发生。从文档

run will return the result of the remote program's stdout as a single (likely multiline) string.

run 将远程程序的标准输出结果作为单个(可能是多行)字符串返回。

run(), and related commands like local()and sudo(), return an _AttributeStringobject that is just a wrapper around stdout with attribute access to additional information like failure/success booleans, stderr, the command run, etc. The result object also has a stdoutattribute, which is just more explicit.

run(),以及相关的命令,如local()and sudo(),返回一个_AttributeString对象,它只是 stdout 的一个包装器,具有对附加信息的属性访问,如失败/成功布尔值、stderr、命令运行等。结果对象也有一个stdout属性,它只是更明确.

To troubleshoot, print type(output), outputto be sure the response is what you expect. Examine output.failedand output.stderr. It could be the command isn't doing what you expect, there is no "backups" directory, etc.

要进行故障排除,print type(output), output请确保响应符合您的预期。检查output.failedoutput.stderr。可能是命令没有按照您的预期执行,没有“备份”目录等。

回答by user1406490

Try as below using String IO

尝试如下使用 String IO

from fabric.api import *
from StringIO import StringIO

fh = StringIO()
run("ls -l backups", stdout=fh)

fh.seek(0)
for line in fh.readlines():
    do_stuff(line)

回答by Evhz

In case you need to use run(), you can do it like this:

如果你需要使用 run(),你可以这样做:

with settings(
    hide('warnings', 'running', 'stdout', 'stderr'),
    warn_only=True
):
    command = 'ls -l backups'
    output = run(command)
    for line in output.splitlines():
        do_stuff(line)

For local() there is a bit more simple solution:

对于 local() 有一个更简单的解决方案:

command = 'ls -l backups'
output = local(command, capture=True)
for line in output.splitlines():
    do_stuff(line)

I hope it helps.

我希望它有帮助。

回答by Bekzot Asimov

Just simply return it:

只需简单地返回它:

def output():
    return run("ls -l backups")
a = execute(output, host=hostname)
print a

a will be dictionary of results.

a 将是结果字典。

回答by Itay Katz

Try split using "\r\n":

尝试使用“ \r\n”拆分:

output = run("ls -l backups")
output_stdout = output.stdout.split("\r\n")

回答by pitaside

You can also use this if you are using the local()api, by setting the capture=True

如果您正在使用local()api,您也可以使用它,通过设置capture=True

@task
def login_ecr_docker():
    ecr_login = local("aws ecr get-login --region us-west-2", capture=True)
    docker_login = ecr_login.stdout
    status = local(docker_login, capture=True)
    print (status.stdout)