summaryrefslogtreecommitdiff
path: root/agent/apps/target_gz.py
blob: 873479c4befa34771c8da6965899b6eb41c6cd4b (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
29
30
31
32
33
34
# 目标感知
import ast
import subprocess
from apiflask import APIBlueprint
from apiflask.fields import String
from flask import json

bp = APIBlueprint('target_gz', __name__, url_prefix='/target_gz')
PATH = "./apps/code/12_TargetGZ/target_perception.py"

@bp.get('/<IP>')
@bp.doc("对攻击目标进行目标感知")
def execute_command(IP):
    output = None
    while not output:
        proc = subprocess.Popen(f"python3 {PATH} {IP}", 
                                shell=True, 
                                stdout=subprocess.PIPE,
                                stderr=subprocess.STDOUT, 
                                universal_newlines=True,
                                text=True, 
                                encoding='utf-8')

        output = proc.communicate()[0].strip()
    try:
        output = ast.literal_eval(output)
        # 替换空字符串为 None
        for key, value in output.items():
            if value == "":
                output[key] = None
        print(output)
        return output, 200
    except:
        return 500