簡體   English   中英

獲取通過os.system()命令執行的命令的輸出

[英]Fetching the output of a command executed through os.system() command

我使用的腳本是在其中使用os.system()命令向遠程服務器(ssh)發出shell命令的。 我需要收集在遠程服務器上執行的命令的輸出。 問題是雙重重定向。 我正在使用os.system()執行ssh命令,該命令在遠程服務器上執行預期的命令。 我打算利用此輸出。 我只需要一些有關如何實現此目標的指示?

使用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.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM