websoft9/appmanage/api/utils/shell_execute.py

34 lines
1.1 KiB
Python
Raw Normal View History

2023-02-22 10:44:15 +08:00
#!/usr/bin/python3
2023-02-21 14:21:53 +08:00
import os, io, sys, platform, shutil, time, subprocess, json, datetime
2023-03-15 17:55:05 +08:00
from api.utils.common_log import myLogger
2023-02-21 14:21:53 +08:00
2023-02-22 11:29:06 +08:00
def execute_command_output(cmd_str):
print(cmd_str)
2023-02-22 11:29:14 +08:00
out_str = subprocess.getoutput(cmd_str)
2023-02-22 11:29:06 +08:00
print(out_str)
return out_str
2023-02-22 14:43:36 +08:00
# cmd_str: 执行的command命令 times如果不成功的重复次数
def execute_command_output_all(cmd_str, max_time = 3):
2023-03-15 17:55:05 +08:00
myLogger.info_logger("Start to execute cmd: " + cmd_str)
execute_time = 0
while execute_time < max_time:
2023-03-06 07:38:14 +08:00
process = subprocess.run(cmd_str, shell=True, stdout=subprocess.PIPE, universal_newlines=True)
2023-02-22 14:43:36 +08:00
if process.returncode == 0:
2023-02-22 14:50:10 +08:00
return {"code": "0", "result": process.stdout,}
2023-02-22 14:43:36 +08:00
else:
2023-03-15 17:55:05 +08:00
execute_time = execute_time + 1
2023-02-22 14:43:36 +08:00
2023-03-16 10:03:54 +08:00
myLogger.error_logger("Command execute failed Commend: " + cmd_str)
2023-03-15 17:55:05 +08:00
return {"code": "-1", "result": "command execute failed, please check your command!"}
2023-03-02 14:56:08 +08:00
def convert_command(cmd_str):
convert_cmd = ""
if cmd_str == "":
convert_cmd=cmd_str
else:
2023-03-02 17:28:57 +08:00
convert_cmd='echo "'+cmd_str+'" > /data/pipe/appmanage'
2023-03-02 14:56:08 +08:00
return convert_cmd