From 04c1bfcf135cbd0ea4cf311fbf8b5eca202c4131 Mon Sep 17 00:00:00 2001 From: zhanghan2021 Date: Thu, 23 Nov 2023 14:56:21 +0800 Subject: [PATCH] Execute system commands, capture output, and return execution results --- common/command.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/common/command.py b/common/command.py index df56211..3f4d3bb 100644 --- a/common/command.py +++ b/common/command.py @@ -2,6 +2,9 @@ #!/usr/bin/env python # cython:language_level=3 +import subprocess +import os +import sys from common.log import Logger class Command: @@ -36,4 +39,33 @@ class Command: return False else: return True + + @staticmethod + def cmd_run(cmd, caller = ''): + ''' + Encapsulate the general RUN function, get the result + ''' + command_result = '' + try: + env_c = os.environ + env_c['LANG'] = 'en_US.UTF-8' + + check_cmd = cmd.split() + if check_cmd[0] == 'cat' or check_cmd[0] == 'ls': + if not os.path.exists(check_cmd[1]): + Logger().error("{} 不存在, cmd_run 命令 {} 无法执行".format(check_cmd[1], cmd)) + return command_result + + ret = subprocess.Popen(cmd, shell = True, stdout = subprocess.PIPE , stderr = subprocess.PIPE, env = env_c) + stdout,stderr = ret.communicate() + if not sys.version_info[0] >= 3: + stdout = stdout.replace('\x1b[7l', '') + if Command.cmd_check(stdout, stderr, ret.returncode, cmd): + command_result = cmd + '\n'+ stdout.decode('utf8') + + return command_result + + except Exception as err: + Logger().error("An exception occurred when executing [{}]: {}".format(cmd, err)) + return command_result \ No newline at end of file -- Gitee