1

我需要来自scutil指挥部的信息。当我scutil -d -r xyz.com在终端上运行时。我可以看到几行输出。但是当我这样做时,文件中只能看到scutil -d -r xyz.com > file.txt最后一行命令输出。flags = 0x00000002 (Reachable)

我正在从 python 运行这个命令,我需要这个命令的全部内容。我在python中运行的方式是:

import os

output = os.popen('scutil -r -d yahoo.com').read()
print output

输出是:

标志 = 0x00000002(可达)

但我也需要这里命令的所有输出。请让我知道是否有任何解决此问题的方法。

4

3 回答 3

0

scutil打印出信息以stderr尝试使用&> file.txt而不是> file.txt

在 python 中尝试使用:

import commands 
print commands.getstatusoutput("scutil -d -r xyz.com")
于 2012-06-14T20:53:06.497 回答
0

os.popen 自 2.6 起已弃用
文档提供了os.popen使用subprocess模块替换的入门指南:http:
//docs.python.org/library/subprocess.html#replacing-os-popen-os-popen2-os-popen3

应用于您的代码的示例:

import subprocess
my_process = subprocess.Popen('scutil -r -d yahoo.com', 
                              shell=True,
                              stdout=subprocess.PIPE, 
                              stderr=subprocess.PIPE)
out, err = my_process.communicate()
print out
print err
于 2012-06-14T21:10:24.163 回答
0

你可能会使用subprocess.check_output("scutil -d -r xyz.com", stderr=subprocess.STDOUT, shell=True)

import subprocess

output = subprocess.check_output("scutil -d -r xyz.com", stderr=subprocess.STDOUT, shell=True)
print(output)

check_output您应该注意该命令的两件事:

  1. stderr=subprocess.STDOUT将错误输出流通过管道传输到标准输出
  2. shell=True通过 shell 执行命令。这使您可以访问运行 shell 进程的相同环境。
于 2012-06-15T05:23:16.750 回答