diff options
| author | handingkang <[email protected]> | 2024-05-29 22:18:06 +0800 |
|---|---|---|
| committer | handingkang <[email protected]> | 2024-05-29 22:18:06 +0800 |
| commit | 6bc2600c7ad368d3b16b4921c90f0c4e8273a37f (patch) | |
| tree | efc17c0bdc4f1fb71498ca948d056b2db476a657 | |
| parent | ed9fbe4239ddd3b9994985e48772c2e3535bbfeb (diff) | |
1. 初步实现代理接受下发指令,并打印执行脚本输出的功能
| -rw-r--r-- | agent/app.py | 4 | ||||
| -rw-r--r-- | agent/apps/code/test/README.md | 2 | ||||
| -rw-r--r-- | agent/apps/code/test/hello.py | 5 | ||||
| -rw-r--r-- | agent/apps/script.py | 35 | ||||
| -rw-r--r-- | agent/apps/util.py | 13 |
5 files changed, 36 insertions, 23 deletions
diff --git a/agent/app.py b/agent/app.py index ee88577..784696b 100644 --- a/agent/app.py +++ b/agent/app.py @@ -10,10 +10,12 @@ import requests import yaml from apiflask import APIFlask -from apps.delay import bp as scriptbp +from apps.delay import bp as delaybp +from apps.script import bp as scriptbp # 注册蓝图 app = APIFlask(__name__, template_folder='./static/templates') +app.register_blueprint(delaybp) app.register_blueprint(scriptbp) # 代理配置 config = {} diff --git a/agent/apps/code/test/README.md b/agent/apps/code/test/README.md index 2dda2f9..91c1b86 100644 --- a/agent/apps/code/test/README.md +++ b/agent/apps/code/test/README.md @@ -2,4 +2,4 @@ hello.py 是一个测试文件,用于验证主控端任务下发到代理执行的可行性 -hello.py接收一个字符串,并返回对应的执行输出
\ No newline at end of file +hello.py接收一个字符串,并返回对应的执行输出,循环10次
\ No newline at end of file diff --git a/agent/apps/code/test/hello.py b/agent/apps/code/test/hello.py index 8271b56..1e00d19 100644 --- a/agent/apps/code/test/hello.py +++ b/agent/apps/code/test/hello.py @@ -1,7 +1,10 @@ import argparse +import time parser = argparse.ArgumentParser() parser.add_argument('-s', '--string', type=str, help="输出到控制台的字符串", required=True) args = parser.parse_args() print("hello.py开始执行,执行参数为" + args.string) -print("hello" + str(args.string)) +for _ in range(10): + time.sleep(1) + print("hello " + str(args.string)) diff --git a/agent/apps/script.py b/agent/apps/script.py index 1e2a5f3..a6a30eb 100644 --- a/agent/apps/script.py +++ b/agent/apps/script.py @@ -2,13 +2,15 @@ import subprocess from concurrent.futures import ThreadPoolExecutor -import select from apiflask import APIBlueprint from apiflask.fields import String +from .util import debug + bp = APIBlueprint('script', __name__, url_prefix='/script') # 线程池 executor = ThreadPoolExecutor(5) +BASE_PATH = "./apps/code/" @bp.post('/') @@ -17,12 +19,12 @@ executor = ThreadPoolExecutor(5) 'policy': String(required=True), 'param': String(required=True) }) -def start_script(query_data): +def start_script(json_data): # 执行命令 - exe = [query_data['name']] + exe = [BASE_PATH + json_data['policy']] # 执行参数 - params = query_data['param'].split() - if '.py' in query_data['name']: + params = json_data['param'].split() + if '.py' in json_data['policy']: exe = ["python"] + exe # 通过 executor.submit(process_script, exe + params) @@ -33,21 +35,14 @@ def process_script(command): # 开始执行命令,不等待其执行完毕 process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) while True: - # 使用select来监测stdout和stderr是否有数据 - # select函数会阻塞,直到有数据可读 - # 返回值是三个列表,第一个列表是可读的文件对象 - readable, _, _ = select.select([process.stdout, process.stderr], [], []) - - for stream in readable: - # 读取输出并转换成字符串 - output = stream.readline().decode('utf-8') - if output: - # TODO:存储输出到数据库中 - print(output.strip()) - - # 检查子进程是否结束 - if process.poll() is not None: + output = process.stdout.readline().rstrip().decode('utf-8') + if output == '' and process.poll() is not None: break + if output == '': + continue + if output: + debug(output.strip()) # 获取子进程的返回值 - rc = process.wait() + rc = process.poll() + return rc diff --git a/agent/apps/util.py b/agent/apps/util.py index 8cadf4d..d25d8a7 100644 --- a/agent/apps/util.py +++ b/agent/apps/util.py @@ -1,5 +1,6 @@ # 工具函数集合 import requests +from loguru import logger # 代理输出回传 @@ -8,3 +9,15 @@ def agent_echo(proto, server, level, info): r = requests.post(proto + "://" + server, json=data) if r.status_code == 200: print("ok") + + +def debug(message, *args, **kwargs): + logger.debug(message, *args, **kwargs) + + +def info(message, *args, **kwargs): + logger.info(message, *args, **kwargs) + + +def error(message, *args, **kwargs): + logger.error(message, *args, **kwargs) |
