python执行Linux命令,返回字典

来源:互联网 时间:1970-01-01

#!/usr/bin/env pythonimport commands
 
 
def monitor():
    shell_command = 'sar 1 3| grep "^Average:"'
    status,result = commands.getstatusoutput(shell_command)
    if status != 0:
        value_dic = {'status': status}
    else:
        value_dic = {}
        user,nice,system,iowait,steal,idle = result.split()[2:]
        value_dic['data_value'] = {
            'user': user,
            'nice': nice,
            'system': system,
            'iowait': iowait,
            'steal': steal,
            'idle': idle,
            'status': status
        }
    return value_dic
 
if __name__ == '__main__':
    print monitor()
 
 
结果为:
{'data_value': {'status': 0, 'iowait': '0.67', 'system': '0.34', 'idle': '98.66', 'user': '0.00', 'steal': '0.34', 'nice': '0.00'}}



相关阅读:
Top