0

我正在使用一个脚本,我使用 os.system() 命令向远程服务器 (ssh) 发出 shell 命令。我需要收集我在远程服务器上执行的命令的输出。问题是双重重定向。我正在使用 os.system() 来执行 ssh 命令,该命令在远程服务器上执行预期的命令。我打算利用这个输出。我只需要一些关于如何实现这一点的指示吗?

4

1 回答 1

3

使用subprocess模块:

subprocess.check_output将命令的输出作为字符串返回。

>>> import subprocess
>>> print subprocess.check_output.__doc__
Run command with arguments and return its output as a byte string.

    If the exit code was non-zero it raises a CalledProcessError.  The
    CalledProcessError object will have the return code in the returncode
    attribute and output in the output attribute.
于 2013-07-08T18:14:59.813 回答