获取通过 os.system() 命令执行的命令的输出 [英] Fetching the output of a command executed through os.system() command
问题描述
我正在使用一个脚本,我使用 os.system() 命令向远程服务器 (ssh) 发出 shell 命令.我需要收集我在远程服务器上执行的命令的输出.问题是双重重定向.我正在使用 os.system() 来执行 ssh 命令,该命令在远程服务器上执行预期的命令.我打算使用这个输出.我只需要一些关于如何实现这一点的指示?
使用subprocess
模块:
subprocess.check_output
以字符串形式返回命令的输出.
I am using a script where I issue a shell command to a remote server (ssh) using os.system() command. I need to collect the output of the command I executed on the remote server. The problem is the double redirection. I am using the os.system() to execute a ssh command which executes the intended command on a remote server. It is this output I intend to make use of. I just need some pointers as to how this can be achieved ?
Use the subprocess
module:
subprocess.check_output
returns the output of a command as a string.
>>> 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.
这篇关于获取通过 os.system() 命令执行的命令的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!