簡單介紹:python
此模塊兒經常使用來調用LinuxShell命令,並返回狀態碼和結果shell
經常使用方法:less
commands.getoutput(cmd) -> strspa
說明:返回基於shell執行後的結果.net
commands.getstatusoutput(cmd) -> tuplecode
說明:返回(status, output)元祖對象對象
# -*- coding: utf-8 -*- """ # # Authors: limanman # OsChina: http://my.oschina.net/pydevops/ # Purpose: # """ import commands import blessings def main(): """Main function.""" command_str = 'echo "lm_521314_lz"|passwd root --stdin' command_res = commands.getstatusoutput(command_str) command_status = 'True' if command_res[0] == 0 else 'False' print '''%s %s''' % (TERMINAL.green(command_str), TERMINAL.yellow(command_status)) if __name__ == '__main__': TERMINAL = blessings.Terminal() main()