summaryrefslogtreecommitdiff
path: root/py_common/common_system_cmd.py
blob: 047fa40511073f5f829d9f4f654ad97de7732c19 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#coding=utf-8
import os
import sys

#return exitcode value + output message:
#    0: succ
#    1: error
def system_cmd_run(cmd_str):
    dangerous_cmd = {"rm", "mv", "poweroff", "shutdown"}
    
    for cmd in dangerous_cmd:
        pattern = "\s*%s" %(cmd)
        match_str = re.match(pattern, cmd_str)
        if not match_str is None:
            print("can't run this cmd:%s" %(cmd_str))
            sys.exit(1)
            
    try:
        exitcode, output = subprocess.getstatusoutput(cmd_str)
    except Exception as e:  
        print(e)    
        print("###### %s" %(e.message))
        #if exitcode != 0:
        #    output = ""
        return 1, e.message

    return exitcode, output