commit 92afcbd14fe6152fbe427efd7fb225c745aa38e2 Author: z66 <1415243231@qq.com> Date: Fri Aug 22 17:31:14 2025 +0800 bat_manage diff --git a/app.py b/app.py new file mode 100644 index 0000000..03f93ea --- /dev/null +++ b/app.py @@ -0,0 +1,181 @@ +from flask import Flask, render_template, request, jsonify +import os +import subprocess +import json +import threading +import time +from urllib.parse import unquote + +app = Flask(__name__) + +# 配置 +TASKS_DIR = os.path.join(os.getcwd(), 'tasks') +CONFIG_FILE = 'config.json' +LOG_LINES = 200 + +# 全局变量 +SCRIPT_CONFIGS = {} +running_processes = {} +script_outputs = {} + +os.makedirs(TASKS_DIR, exist_ok=True) + + +def load_configs(): + global SCRIPT_CONFIGS + try: + if os.path.exists(CONFIG_FILE): + with open(CONFIG_FILE, 'r', encoding='utf-8') as f: + SCRIPT_CONFIGS = json.load(f) + if not isinstance(SCRIPT_CONFIGS, dict): + SCRIPT_CONFIGS = {} + except: + SCRIPT_CONFIGS = {} + + +def save_configs(): + with open(CONFIG_FILE, 'w', encoding='utf-8') as f: + json.dump(SCRIPT_CONFIGS, f, indent=2, ensure_ascii=False) + + +def get_script_path(script_name): + return os.path.join(TASKS_DIR, script_name) + + +@app.route('/') +def index(): + return render_template('index.html') + + +@app.route('/api/scripts') +def api_scripts(): + if not os.path.exists(TASKS_DIR): + return jsonify([]) + + files = [] + for f in os.listdir(TASKS_DIR): + path = os.path.join(TASKS_DIR, f) + if os.path.isfile(path) and f.endswith(('.bat', '.py')): + files.append(f) + return jsonify(sorted(files)) + + +@app.route('/api/config') +def api_get_config(): + return jsonify(SCRIPT_CONFIGS) + + +@app.route('/api/config', methods=['POST']) +def api_save_config(): + try: + data = request.get_json() + if not isinstance(data, dict): + return jsonify({"error": "Invalid data format"}), 400 + + for script_name, config in data.items(): + SCRIPT_CONFIGS[script_name] = config + + save_configs() + return jsonify({"msg": "配置已保存"}) + except Exception as e: + return jsonify({"error": str(e)}), 500 + + +@app.route('/api/start/') +def api_start_script(script_name): + script_name = unquote(script_name) + if script_name not in SCRIPT_CONFIGS: + return jsonify({"error": "未找到配置"}), 404 + + if script_name in running_processes: + return jsonify({"error": "已在运行"}), 400 + + script_path = get_script_path(script_name) + if not os.path.exists(script_path): + return jsonify({"error": f"脚本不存在: {script_path}"}), 404 + + if script_name.endswith('.bat'): + cmd = ['cmd', '/c', script_path] + elif script_name.endswith('.py'): + cmd = ['python', script_path] + else: + return jsonify({"error": "不支持的类型"}), 400 + + def target(): + try: + proc = subprocess.Popen( + cmd, + stdout=subprocess.PIPE, + stderr=subprocess.STDOUT, + stdin=subprocess.PIPE, + text=True, + encoding='utf-8', + cwd=TASKS_DIR + ) + running_processes[script_name] = proc + script_outputs[script_name] = [] + + while True: + line = proc.stdout.readline() + if line: + line = f"[{time.strftime('%H:%M:%S')}] {line.strip()}" + script_outputs[script_name].append(line) + script_outputs[script_name] = script_outputs[script_name][-LOG_LINES:] + if proc.poll() is not None: + break + except Exception as e: + script_outputs.setdefault(script_name, []).append(f"❌ 执行错误: {e}") + finally: + running_processes.pop(script_name, None) + + thread = threading.Thread(target=target, daemon=True) + thread.start() + return jsonify({"msg": f"✅ 开始执行: {script_name}"}) + + +@app.route('/api/stop/') +def api_stop_script(script_name): + script_name = unquote(script_name) + if script_name not in running_processes: + return jsonify({"msg": "未在运行"}) + + proc = running_processes[script_name] + proc.terminate() + try: + proc.wait(timeout=5) + except: + proc.kill() + running_processes.pop(script_name, None) + script_outputs.setdefault(script_name, []).append("⏹️ 已停止") + return jsonify({"msg": f"⏹️ 已停止: {script_name}"}) + + +@app.route('/api/status/') +def api_status(script_name): + script_name = unquote(script_name) + output = script_outputs.get(script_name, []) + is_running = script_name in running_processes + return jsonify({ + "running": is_running, + "output": output + }) + + +@app.route('/api/send-input/', methods=['POST']) +def api_send_input(script_name): + script_name = unquote(script_name) + if script_name not in running_processes: + return jsonify({"error": "脚本未运行"}), 400 + + proc = running_processes[script_name] + try: + proc.stdin.write(request.data + "\n") + proc.stdin.flush() + return jsonify({"msg": "输入已发送"}) + except Exception as e: + return jsonify({"error": str(e)}), 500 + + +if __name__ == '__main__': + load_configs() + app.run(host='0.0.0.0', port=5000, debug=False) \ No newline at end of file diff --git a/config.json b/config.json new file mode 100644 index 0000000..7db5356 --- /dev/null +++ b/config.json @@ -0,0 +1,16 @@ +{ + "3.bat": { + "script": "3.bat", + "mode": "interval", + "interval": "1", + "unit": "hours" + }, + "2.bat": { + "script": "2.bat", + "mode": "long-running" + }, + "1.bat": { + "script": "1.bat", + "mode": "long-running" + } +} \ No newline at end of file diff --git a/tasks/1.bat b/tasks/1.bat new file mode 100644 index 0000000..f627729 --- /dev/null +++ b/tasks/1.bat @@ -0,0 +1,25 @@ + +@echo off +chcp 65001 > nul +:: 脚本1: 显示基本系统信息 +title 系统信息报告 +color 0a +echo. +echo ============================= +echo 系统信息概览 +echo ============================= +echo. +echo 当前时间: %time% +echo 当前日期: %date% +echo 计算机名: %computername% +echo 用户名: %username% +echo 操作系统: %os% +echo. +echo 驱动器 C: 可用空间: +for /f "tokens=3" %%a in ('dir c:\ ^| find "可用字节"') do set "free=%%a" +echo %free% +echo. +echo ============================= +echo 报告生成于 %date% %time% +echo ============================= +pause \ No newline at end of file diff --git a/tasks/2.bat b/tasks/2.bat new file mode 100644 index 0000000..2a982fa --- /dev/null +++ b/tasks/2.bat @@ -0,0 +1,5 @@ +@echo off +:loop +set /p input=Enter something: +echo [%time%] User input: %input% +goto loop \ No newline at end of file diff --git a/tasks/3.bat b/tasks/3.bat new file mode 100644 index 0000000..716e496 --- /dev/null +++ b/tasks/3.bat @@ -0,0 +1,32 @@ +@echo off +chcp 65001 > nul +:: 脚本4: 网络诊断测试 +title 网络连接测试 +color 0e +echo 网络连接诊断工具 +echo ============================= + +echo 1. 测试本地回环 (127.0.0.1)... +ping -n 2 -w 100 127.0.0.1 > nul && echo [OK] 本地回环正常。 || echo [FAIL] 本地回环失败! + +echo 2. 测试网关连接... +for /f "tokens=2 delims=[]" %%a in ('route print ^| find " 0.0.0.0" ^| find "0.0.0.0"') do set gateway=%%a +if defined gateway ( + echo 网关地址: %gateway% + ping -n 2 -w 500 %gateway% > nul && echo [OK] 网关可达。 || echo [FAIL] 无法连接到网关! +) else ( + echo [ERROR] 无法获取网关地址。 +) + +echo 3. 测试 DNS 解析 (Google DNS: 8.8.8.8)... +nslookup google.com 8.8.8.8 > nul && echo [OK] DNS 解析成功。 || echo [FAIL] DNS 解析失败! + +echo 4. 测试外部连接 (www.baidu.com)... +ping -n 2 -w 1000 www.baidu.com > nul && echo [OK] 可访问外部网络。 || echo [FAIL] 无法访问外部网络! + +echo. +echo ============================= +echo 测试完成。 +echo 诊断时间: %time% +echo ============================= +pause \ No newline at end of file diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..04e8661 --- /dev/null +++ b/templates/index.html @@ -0,0 +1,289 @@ + + + + + + 脚本管理平台 + + + + + +

脚本管理平台

+ +
+ +
+
+
脚本列表
+ +
+
+
+ + +
+ +
+ + +
+
脚本配置
+
+
+ + +
+ +
+ + +
+ + + + +
+
+
+
+ + + + + + \ No newline at end of file