Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 抑制织物运行输出的简单方法?_Python_Fabric - Fatal编程技术网

Python 抑制织物运行输出的简单方法?

Python 抑制织物运行输出的简单方法?,python,fabric,Python,Fabric,我正在远程计算机上运行命令: remote_output=run('mysqldump--no data--user=username--password={0}数据库'。格式(密码)) 我想捕获输出,但不想将其全部打印到屏幕上。做这件事最简单的方法是什么?听起来您要找的就是这个部分 要隐藏控制台的输出,请尝试以下操作: from\uuuuuu future\uuuuuuu使用\u语句导入 从fabric.api导入隐藏、运行、获取 使用hide('output'): 运行('mysqldum

我正在远程计算机上运行命令:

remote_output=run('mysqldump--no data--user=username--password={0}数据库'。格式(密码))
我想捕获输出,但不想将其全部打印到屏幕上。做这件事最简单的方法是什么?

听起来您要找的就是这个部分

要隐藏控制台的输出,请尝试以下操作:

from\uuuuuu future\uuuuuuu使用\u语句导入
从fabric.api导入隐藏、运行、获取
使用hide('output'):
运行('mysqldump——无数据测试| tee测试。创建_表')
获取('~/test.create_table','~/test.create_table'))
以下是示例结果:

No hosts found. Please specify (single) host string for connection: 192.168.6.142
[192.168.6.142] run: mysqldump --no-data test | tee test.create_table
[192.168.6.142] download: /home/quanta/test.create_table <- /home/quanta/test.create_table
找不到主机。请为连接指定(单个)主机字符串:192.168.6.142
[192.168.6.142]运行:mysqldump--无数据测试| tee test.create_table

[192.168.6.142]下载:/home/quanta/test.create_table如果您想隐藏日志中的所有内容并避免在命令失败时引发结构异常,请尝试以下操作:

from __future__ import with_statement
from fabric.api import env,run,hide,settings

env.host_string = 'username@servernameorip'
env.key_filename = '/path/to/key.pem'

def exec_remote_cmd(cmd):
    with hide('output','running','warnings'), settings(warn_only=True):
        return run(cmd)
之后,您可以检查命令结果,如本例所示:

cmd_list = ['ls', 'lss']
for cmd in cmd_list:
    result = exec_remote_cmd(cmd)
    if result.succeeded:
        sys.stdout.write('\n* Command succeeded: '+cmd+'\n')
        sys.stdout.write(result+"\n")
    else:
        sys.stdout.write('\n* Command failed: '+cmd+'\n')
        sys.stdout.write(result+"\n")
这将是程序的控制台输出(请注意,没有来自结构的日志消息):

*命令成功:ls 桌面espaiorgcats.sql图片公共视频 文档示例.桌面项目脚本 下载音乐prueba模板 *命令失败:lss /bin/bash:lss:未找到命令
对于fabric==2.4.0,可以使用以下逻辑隐藏输出

conn = Connection(host="your-host", user="your-user")
result = conn.run('your_command', hide=True)
result.stdout.strip()  # here you can get the output

正如其他答案暗示的那样,
fabric.api
在问题提出8年后不再存在(在撰写本文时,
fabric==2.5.0
)。然而,这里的下一个最新答案意味着为每个
提供
hide=True
。run()
调用是唯一/可接受的方法

由于不满意,我去挖掘一个合理的等价物,在这个等价物中我只能指定一次。感觉应该还有一种方法可以使用,但我不想再花时间在这上面了,我能找到的最简单的方法就是使用,我们可以通过访问,而不需要任何额外的导入

>>> import fabric

>>> c = fabric.Connection(
...     "foo.example.com",
...     config=fabric.config.Config(overrides={"run": {"hide": True}}),
... )

>>> result = c.run("hostname")

>>> result.stdout.strip()
'foo.example.com'


这也适用于2.5.0版,可能是在2.x系列中隐藏输出的官方方式。登录页上也提到了我,但api文档中没有明确提到此功能。。相反,读者会参考调用包的文档:
>>> import fabric

>>> c = fabric.Connection(
...     "foo.example.com",
...     config=fabric.config.Config(overrides={"run": {"hide": True}}),
... )

>>> result = c.run("hostname")

>>> result.stdout.strip()
'foo.example.com'