模块和包.commands

简单介绍:

此模块儿常用来调用LinuxShell命令,并返回状态码和结果


常用方法:

commands.getoutput(cmd) -> str

说明:返回基于shell执行后的结果

commands.getstatusoutput(cmd) -> tuple

说明:返回(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()


你可能感兴趣的:(模块和包.commands)