websoft9/appmanage/api/utils/shell_execute.py
qiaofeng1227 364582825e excepiton
2023-04-14 02:51:29 +00:00

36 lines
1.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/python3
import os, io, sys, platform, shutil, time, subprocess, json, datetime
from api.utils.common_log import myLogger
from api.exception.command_exception import CommandException
def execute_command_output(cmd_str):
print(cmd_str)
out_str = subprocess.getoutput(cmd_str)
print(out_str)
return out_str
# cmd_str: 执行的command命令 times如果不成功的重复次数
def execute_command_output_all(cmd_str, max_time = 3):
myLogger.info_logger("Start to execute cmd: " + cmd_str)
execute_time = 0
while execute_time < max_time:
process = subprocess.run(f'nsenter -m -u -i -n -p -t 1 sh -c "{cmd_str}"', capture_output=True, check=False, text=True, shell=True)
if process.returncode == 0 and 'Fail' not in process.stdout and 'fail' not in process.stdout and 'Error' not in process.stdout and 'error' not in process.stdout:
return {"code": "0", "result": process.stdout,}
else:
execute_time = execute_time + 1
if execute_time > 3:
raise CommandException(process.stdout)
def convert_command(cmd_str):
convert_cmd = ""
if cmd_str == "":
convert_cmd=cmd_str
else:
convert_cmd="nsenter -m -u -i -n -p -t 1 sh -c " + "'"+cmd_str+"'"
return convert_cmd