From b6ad620cf741bd4a5a72bd953c42e617726dda05 Mon Sep 17 00:00:00 2001 From: xzx3344521 Date: Wed, 5 Nov 2025 23:00:19 +0800 Subject: [PATCH] Update 01 --- 01 | 700 ++++++++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 481 insertions(+), 219 deletions(-) diff --git a/01 b/01 index eecf3da..aeaec50 100644 --- a/01 +++ b/01 @@ -1,190 +1,299 @@ #!/bin/bash -# 服务器控制器脚本 - 监听端口5555 -# 支持: Debian, Ubuntu, CentOS, RHEL, Fedora, Alpine +# 加强版服务器控制器脚本 +# 支持多客户端管理、日志记录、安全控制 -SERVER_PORT=5555 +SERVER_PORT=25555 LOG_FILE="/var/log/controller_server.log" CLIENTS_FILE="/var/lib/controller_clients.txt" SCRIPT_DIR="/opt/controller_scripts" +BACKUP_DIR="/var/backup/controller" +CONFIG_FILE="/etc/controller_server.conf" +PID_FILE="/var/run/controller_server.pid" # 颜色输出 RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' -NC='\033[0m' # No Color +PURPLE='\033[0;35m' +CYAN='\033[0;36m' +NC='\033[0m' + +# 初始化配置 +init_config() { + mkdir -p "$(dirname "$LOG_FILE")" + mkdir -p "$(dirname "$CLIENTS_FILE")" + mkdir -p "$SCRIPT_DIR" + mkdir -p "$BACKUP_DIR" + + # 创建配置文件 + if [[ ! -f "$CONFIG_FILE" ]]; then + cat > "$CONFIG_FILE" << EOF +# 服务器控制器配置 +SERVER_PORT=$SERVER_PORT +LOG_FILE="$LOG_FILE" +CLIENTS_FILE="$CLIENTS_FILE" +SCRIPT_DIR="$SCRIPT_DIR" +BACKUP_DIR="$BACKUP_DIR" +PID_FILE="$PID_FILE" +MAX_CLIENTS=1000 +HEARTBEAT_TIMEOUT=300 +ALLOWED_IPS="0.0.0.0/0" +ENABLE_LOGGING=true +EOF + fi + + source "$CONFIG_FILE" + + # 创建示例脚本 + create_sample_scripts + create_management_scripts + + touch "$LOG_FILE" + touch "$CLIENTS_FILE" + + chmod 600 "$CLIENTS_FILE" +} log() { - echo -e "$(date '+%Y-%m-%d %H:%M:%S') - $1" | tee -a "$LOG_FILE" + local timestamp=$(date '+%Y-%m-%d %H:%M:%S') + if [[ "$ENABLE_LOGGING" == "true" ]]; then + echo -e "[$timestamp] $1" | tee -a "$LOG_FILE" + else + echo -e "[$timestamp] $1" + fi } print_color() { echo -e "${2}${1}${NC}" } -# 检查命令是否存在 -check_command() { - if ! command -v "$1" &> /dev/null; then - return 1 - fi - return 0 +print_banner() { + clear + echo + print_color "╔══════════════════════════════════════════════════════════════╗" "$CYAN" + print_color "║ 加强版服务器控制器 v2.0 ║" "$BLUE" + print_color "║ Enhanced Server Controller v2.0 ║" "$BLUE" + print_color "╠══════════════════════════════════════════════════════════════╣" "$CYAN" + print_color "║ 端口: $SERVER_PORT | 客户端数: $(get_client_count) | 状态: $(server_status) ║" "$GREEN" + print_color "╚══════════════════════════════════════════════════════════════╝" "$CYAN" + echo } -# 检查root权限 -check_root() { - if [[ $EUID -ne 0 ]]; then - print_color "错误: 此脚本需要root权限运行" "$RED" - exit 1 +# 检查依赖 +check_dependencies() { + local deps=("nc" "awk" "grep" "sed") + local missing=() + + for dep in "${deps[@]}"; do + if ! command -v "$dep" &> /dev/null; then + missing+=("$dep") + fi + done + + if [[ ${#missing[@]} -gt 0 ]]; then + print_color "缺少依赖: ${missing[*]}" "$RED" + print_color "正在安装..." "$YELLOW" + + if command -v apt-get &> /dev/null; then + apt-get update && apt-get install -y netcat-traditional awk grep sed + elif command -v yum &> /dev/null; then + yum install -y nc awk grep sed + elif command -v dnf &> /dev/null; then + dnf install -y nc awk grep sed + elif command -v apk &> /dev/null; then + apk add netcat-openbsd awk grep sed + fi fi } -# 安装依赖 -install_dependencies() { - local distro=$(grep -oP '(?<=^ID=).+' /etc/os-release | tr -d '"') - - print_color "检测到系统: $distro" "$BLUE" - print_color "安装必要依赖..." "$YELLOW" - - case $distro in - debian|ubuntu) - apt-get update - apt-get install -y netcat-traditional net-tools iproute2 - ;; - centos|rhel|fedora) - if command -v dnf &> /dev/null; then - dnf install -y nc net-tools iproute2 - else - yum install -y nc net-tools iproute2 - fi - ;; - alpine) - apk add netcat-openbsd net-tools iproute2 - ;; - *) - log "未知发行版,尝试安装netcat" - if command -v apt-get &> /dev/null; then - apt-get update && apt-get install -y netcat-traditional - elif command -v yum &> /dev/null; then - yum install -y nc - elif command -v apk &> /dev/null; then - apk add netcat-openbsd - fi - ;; - esac - - # 检查netcat是否安装成功 - if ! check_command nc && ! check_command netcat; then - print_color "错误: 无法安装netcat,请手动安装" "$RED" - exit 1 - fi - - print_color "依赖安装完成!" "$GREEN" -} +# 创建管理脚本 +create_management_scripts() { + # 批量更新脚本 + cat > "$SCRIPT_DIR/batch_update.sh" << 'EOF' +#!/bin/bash +echo "开始批量更新所有客户端..." +for client in $(grep -oP '^[^|]+' /var/lib/controller_clients.txt); do + echo "更新客户端: $client" + echo "COMMAND:apt-get update && apt-get upgrade -y" | nc -w 3 $(get_client_ip $client) 5556 +done +echo "批量更新完成" +EOF -# 获取netcat命令 -get_nc_command() { - if check_command nc; then - echo "nc" - elif check_command netcat; then - echo "netcat" - else - echo "" + # 系统信息收集脚本 + cat > "$SCRIPT_DIR/collect_system_info.sh" << 'EOF' +#!/bin/bash +echo "收集所有客户端系统信息..." +while IFS='|' read -r serial ip hostname system last_seen; do + if [[ -n "$serial" ]]; then + echo "=== $hostname ($serial) ===" + echo "COMMAND:uname -a; free -h; df -h" | nc -w 3 "$ip" 5556 + echo "------------------------" fi -} +done < /var/lib/controller_clients.txt +EOF -# 初始化环境 -initialize() { - mkdir -p "$(dirname "$LOG_FILE")" - mkdir -p "$(dirname "$CLIENTS_FILE")" - mkdir -p "$SCRIPT_DIR" - - # 创建示例脚本 - create_sample_scripts - - touch "$LOG_FILE" - touch "$CLIENTS_FILE" - - print_color "初始化完成!" "$GREEN" + # 网络诊断脚本 + cat > "$SCRIPT_DIR/network_diagnosis.sh" << 'EOF' +#!/bin/bash +echo "网络诊断..." +while IFS='|' read -r serial ip hostname system last_seen; do + if [[ -n "$serial" ]]; then + echo "检查 $hostname 网络..." + echo "COMMAND:ip addr show; ping -c 2 8.8.8.8" | nc -w 3 "$ip" 5556 + fi +done < /var/lib/controller_clients.txt +EOF + + chmod +x "$SCRIPT_DIR"/*.sh } create_sample_scripts() { - # 关机脚本 + # 基础管理脚本 cat > "$SCRIPT_DIR/shutdown.sh" << 'EOF' #!/bin/bash echo "执行关机操作..." shutdown -h now EOF - # 重启脚本 cat > "$SCRIPT_DIR/reboot.sh" << 'EOF' #!/bin/bash echo "执行重启操作..." reboot EOF - # 服务重启脚本 cat > "$SCRIPT_DIR/restart_services.sh" << 'EOF' #!/bin/bash -echo "重启网络服务..." +echo "重启系统服务..." systemctl restart networking 2>/dev/null || systemctl restart network 2>/dev/null -echo "重启SSH服务..." systemctl restart ssh 2>/dev/null || systemctl restart sshd 2>/dev/null EOF - # 系统信息脚本 - cat > "$SCRIPT_DIR/system_info.sh" << 'EOF' + # 监控脚本 + cat > "$SCRIPT_DIR/monitor_system.sh" << 'EOF' #!/bin/bash -echo "=== 系统信息 ===" +echo "=== 系统监控 ===" echo "主机名: $(hostname)" -echo "系统: $(grep PRETTY_NAME /etc/os-release | cut -d= -f2 | tr -d '\"')" -echo "内核: $(uname -r)" -echo "架构: $(uname -m)" -echo "上线时间: $(uptime -p)" +echo "上线时间: $(uptime)" echo "内存使用:" free -h echo "磁盘使用:" df -h +echo "CPU使用:" +top -bn1 | head -10 EOF - # 更新系统脚本 - cat > "$SCRIPT_DIR/update_system.sh" << 'EOF' + # 安全脚本 + cat > "$SCRIPT_DIR/security_check.sh" << 'EOF' #!/bin/bash -echo "开始系统更新..." -if command -v apt-get &> /dev/null; then - apt-get update && apt-get upgrade -y -elif command -v yum &> /dev/null; then - yum update -y -elif command -v dnf &> /dev/null; then - dnf update -y -elif command -v apk &> /dev/null; then - apk update && apk upgrade -fi -echo "系统更新完成!" +echo "=== 安全检查 ===" +echo "登录用户:" +who +echo "失败登录:" +lastb | head -10 +echo "SSH连接:" +netstat -tlnp | grep ssh EOF chmod +x "$SCRIPT_DIR"/*.sh } +# 服务器状态 +server_status() { + if is_server_running; then + echo -e "${GREEN}运行中${NC}" + else + echo -e "${RED}未运行${NC}" + fi +} + +is_server_running() { + if [[ -f "$PID_FILE" ]]; then + local pid=$(cat "$PID_FILE") + if kill -0 "$pid" 2>/dev/null; then + return 0 + else + rm -f "$PID_FILE" + fi + fi + return 1 +} + +# 获取客户端数量 +get_client_count() { + if [[ -f "$CLIENTS_FILE" ]]; then + grep -c . "$CLIENTS_FILE" 2>/dev/null || echo "0" + else + echo "0" + fi +} + +# 获取客户端IP +get_client_ip() { + local serial=$1 + grep "^$serial|" "$CLIENTS_FILE" | cut -d'|' -f2 +} + +# 清理过期客户端 +cleanup_expired_clients() { + local current_time=$(date +%s) + local temp_file=$(mktemp) + + while IFS='|' read -r serial ip hostname system last_seen; do + if [[ -n "$last_seen" ]]; then + local client_time=$(date -d "$last_seen" +%s 2>/dev/null || echo "0") + local time_diff=$((current_time - client_time)) + + if [[ $time_diff -lt $HEARTBEAT_TIMEOUT ]]; then + echo "$serial|$ip|$hostname|$system|$last_seen" >> "$temp_file" + else + log "清理过期客户端: $serial ($hostname)" + fi + fi + done < "$CLIENTS_FILE" + + mv "$temp_file" "$CLIENTS_FILE" 2>/dev/null +} + +# 备份数据 +backup_data() { + local backup_file="$BACKUP_DIR/backup_$(date +%Y%m%d_%H%M%S).tar.gz" + tar -czf "$backup_file" "$CLIENTS_FILE" "$LOG_FILE" "$SCRIPT_DIR" 2>/dev/null + log "数据已备份到: $backup_file" +} + # 显示客户端列表 show_clients() { - if [[ ! -s "$CLIENTS_FILE" ]]; then - print_color "没有已连接的客户端" "$YELLOW" + cleanup_expired_clients + + local count=$(get_client_count) + print_color "已连接客户端: $count" "$BLUE" + + if [[ $count -eq 0 ]]; then + print_color "没有客户端连接" "$YELLOW" return fi - print_color "已连接的客户端列表:" "$BLUE" - print_color "序列号 | IP地址 | 主机名 | 最后在线" "$BLUE" - print_color "----------------------------------------------------" "$BLUE" + print_color "┌────────────┬───────────────┬──────────────────┬─────────────────┬─────────────────────┐" "$CYAN" + print_color "│ 序列号 │ IP地址 │ 主机名 │ 系统 │ 最后在线 │" "$CYAN" + print_color "├────────────┼───────────────┼──────────────────┼─────────────────┼─────────────────────┤" "$CYAN" + while IFS='|' read -r serial ip hostname system last_seen; do - printf "%-10s| %-13s| %-15s| %s\n" "$serial" "$ip" "$hostname" "$last_seen" + if [[ -n "$serial" ]]; then + printf "│ ${GREEN}%-10s${NC} │ ${YELLOW}%-13s${NC} │ ${BLUE}%-16s${NC} │ ${PURPLE}%-15s${NC} │ ${GREEN}%-19s${NC} │\n" \ + "$serial" "$ip" "$hostname" "$system" "$last_seen" + fi done < "$CLIENTS_FILE" + + print_color "└────────────┴───────────────┴──────────────────┴─────────────────┴─────────────────────┘" "$CYAN" } # 向客户端发送命令 send_command() { local serial=$1 - local command=$2 + shift + local command="$*" if [[ -z "$serial" || -z "$command" ]]; then print_color "用法: send <序列号> <命令>" "$RED" @@ -199,64 +308,63 @@ send_command() { IFS='|' read -r serial ip hostname system last_seen <<< "$client_info" - print_color "向客户端 $serial ($hostname) 发送命令: $command" "$GREEN" + print_color "向客户端 $serial 发送命令..." "$GREEN" + print_color "客户端: $hostname ($ip)" "$BLUE" + print_color "命令: $command" "$YELLOW" # 发送命令到客户端 - local nc_cmd=$(get_nc_command) - if [[ -z "$nc_cmd" ]]; then - print_color "错误: netcat 未安装" "$RED" - return - fi - - echo "COMMAND:$command" | timeout 5 $nc_cmd "$ip" 5556 + echo "COMMAND:$command" | nc -w 5 "$ip" 5556 if [[ $? -eq 0 ]]; then + log "命令发送成功: $serial -> $command" print_color "命令发送成功!" "$GREEN" else + log "命令发送失败: $serial -> $command" print_color "命令发送失败!" "$RED" fi } # 广播命令到所有客户端 broadcast_command() { - local command=$1 + local command="$*" if [[ -z "$command" ]]; then print_color "用法: broadcast <命令>" "$RED" return fi - print_color "向所有客户端广播命令: $command" "$YELLOW" - - local nc_cmd=$(get_nc_command) - if [[ -z "$nc_cmd" ]]; then - print_color "错误: netcat 未安装" "$RED" - return - fi + local count=0 + print_color "向所有客户端广播命令..." "$YELLOW" + print_color "命令: $command" "$PURPLE" while IFS='|' read -r serial ip hostname system last_seen; do - if [[ -n "$serial" && -n "$ip" ]]; then - echo "向 $serial ($hostname) 发送命令..." - echo "COMMAND:$command" | timeout 3 $nc_cmd "$ip" 5556 + if [[ -n "$serial" ]]; then + echo "向 $serial 发送命令..." + echo "COMMAND:$command" | nc -w 3 "$ip" 5556 & + count=$((count + 1)) fi done < "$CLIENTS_FILE" + + wait + log "广播命令完成: $command -> $count 个客户端" + print_color "广播完成! 共发送给 $count 个客户端" "$GREEN" } -# 执行本地脚本 +# 执行脚本 execute_script() { local serial=$1 local script_name=$2 if [[ -z "$serial" || -z "$script_name" ]]; then print_color "用法: script <序列号> <脚本名>" "$RED" - print_color "可用脚本:" "$BLUE" - ls "$SCRIPT_DIR"/*.sh | xargs -n 1 basename + show_available_scripts return fi local script_path="$SCRIPT_DIR/$script_name" if [[ ! -f "$script_path" ]]; then print_color "错误: 脚本 $script_name 不存在" "$RED" + show_available_scripts return fi @@ -268,50 +376,135 @@ execute_script() { IFS='|' read -r serial ip hostname system last_seen <<< "$client_info" - print_color "向客户端 $serial 发送脚本: $script_name" "$GREEN" + print_color "向客户端 $serial 发送脚本..." "$GREEN" + print_color "客户端: $hostname ($ip)" "$BLUE" + print_color "脚本: $script_name" "$YELLOW" - local nc_cmd=$(get_nc_command) - if [[ -z "$nc_cmd" ]]; then - print_color "错误: netcat 未安装" "$RED" + # 发送脚本执行命令 + echo "SCRIPT:$script_name" | nc -w 5 "$ip" 5556 + + if [[ $? -eq 0 ]]; then + log "脚本发送成功: $serial -> $script_name" + print_color "脚本发送成功!" "$GREEN" + else + log "脚本发送失败: $serial -> $script_name" + print_color "脚本发送失败!" "$RED" + fi +} + +# 显示可用脚本 +show_available_scripts() { + print_color "可用脚本:" "$BLUE" + echo + print_color "系统管理:" "$CYAN" + ls "$SCRIPT_DIR"/*.sh 2>/dev/null | xargs -n 1 basename | while read script; do + print_color " 📜 $script" "$GREEN" + done || print_color " 无可用脚本" "$YELLOW" +} + +# 批量执行脚本 +batch_execute_script() { + local script_name=$1 + + if [[ -z "$script_name" ]]; then + print_color "用法: batch <脚本名>" "$RED" + show_available_scripts return fi - # 发送脚本执行命令 - echo "SCRIPT:$script_name" | timeout 5 $nc_cmd "$ip" 5556 + local script_path="$SCRIPT_DIR/$script_name" + if [[ ! -f "$script_path" ]]; then + print_color "错误: 脚本 $script_name 不存在" "$RED" + return + fi + + local count=0 + print_color "批量执行脚本: $script_name" "$YELLOW" + + while IFS='|' read -r serial ip hostname system last_seen; do + if [[ -n "$serial" ]]; then + echo "向 $serial 发送脚本..." + echo "SCRIPT:$script_name" | nc -w 3 "$ip" 5556 & + count=$((count + 1)) + fi + done < "$CLIENTS_FILE" + + wait + log "批量执行完成: $script_name -> $count 个客户端" + print_color "批量执行完成! 共发送给 $count 个客户端" "$GREEN" +} + +# 查看客户端详情 +show_client_detail() { + local serial=$1 + + if [[ -z "$serial" ]]; then + print_color "用法: detail <序列号>" "$RED" + return + fi + + local client_info=$(grep "^$serial|" "$CLIENTS_FILE") + if [[ -z "$client_info" ]]; then + print_color "错误: 未找到序列号 $serial 的客户端" "$RED" + return + fi + + IFS='|' read -r serial ip hostname system last_seen <<< "$client_info" + + print_color "=== 客户端详情 ===" "$BLUE" + print_color "序列号: $serial" "$GREEN" + print_color "IP地址: $ip" "$YELLOW" + print_color "主机名: $hostname" "$CYAN" + print_color "系统信息: $system" "$PURPLE" + print_color "最后在线: $last_seen" "$GREEN" + + # 测试连接 + print_color "连接测试..." "$BLUE" + if ping -c 1 -W 1 "$ip" &> /dev/null; then + print_color "网络连接: 正常" "$GREEN" + else + print_color "网络连接: 失败" "$RED" + fi } # 启动服务器 start_server() { - local nc_cmd=$(get_nc_command) - if [[ -z "$nc_cmd" ]]; then - print_color "错误: netcat 未安装,无法启动服务器" "$RED" - return 1 + if is_server_running; then + print_color "服务器已经在运行 (PID: $(cat "$PID_FILE"))" "$YELLOW" + return fi - print_color "启动控制器服务器在端口 $SERVER_PORT..." "$GREEN" - print_color "使用 netcat 命令: $nc_cmd" "$BLUE" - print_color "服务器日志: $LOG_FILE" "$BLUE" - print_color "客户端列表: $CLIENTS_FILE" "$BLUE" - print_color "按 Ctrl+C 停止服务器" "$YELLOW" + print_color "启动服务器在端口 $SERVER_PORT ..." "$GREEN" + + # 保存PID + echo $$ > "$PID_FILE" + + # 设置信号处理 + trap 'cleanup' INT TERM EXIT + + log "服务器启动成功,PID: $$" while true; do log "等待客户端连接..." - $nc_cmd -l -p $SERVER_PORT -c ' + nc -l -p $SERVER_PORT -c ' client_ip=$(echo $SSH_CLIENT | awk "{print \$1}") if [[ -z "$client_ip" ]]; then client_ip="unknown" fi + read -r data timestamp=$(date "+%Y-%m-%d %H:%M:%S") if echo "$data" | grep -q "HEARTBEAT|"; then IFS="|" read -r heartbeat serial hostname system <<< "$data" - # 更新或添加客户端信息 + + # 更新客户端信息 grep -v "^$serial|" /var/lib/controller_clients.txt > /tmp/clients.tmp 2>/dev/null echo "$serial|$client_ip|$hostname|$system|$timestamp" >> /tmp/clients.tmp mv /tmp/clients.tmp /var/lib/controller_clients.txt + echo "ACK:OK" - echo "客户端 $serial 已连接 - $hostname" + echo "客户端 $serial 已连接 - $hostname ($client_ip)" else echo "ACK:UNKNOWN_COMMAND" fi @@ -320,89 +513,147 @@ start_server() { done } -# 显示帮助 -show_help() { - print_color "服务器控制器命令:" "$BLUE" - echo "list - 显示客户端列表" - echo "send - 向指定客户端发送命令" - echo "broadcast - 向所有客户端广播命令" - echo "script - 执行预定义脚本" - echo "scripts - 显示可用脚本" - echo "start - 启动服务器" - echo "stop - 停止服务器" - echo "status - 显示服务器状态" - echo "help - 显示此帮助" +cleanup() { + log "服务器停止" + rm -f "$PID_FILE" + exit 0 } # 停止服务器 stop_server() { - pkill -f "nc -l -p $SERVER_PORT" - print_color "服务器已停止" "$GREEN" + if is_server_running; then + local pid=$(cat "$PID_FILE") + print_color "停止服务器 (PID: $pid)..." "$GREEN" + kill "$pid" + sleep 2 + if is_server_running; then + kill -9 "$pid" + fi + print_color "服务器已停止" "$GREEN" + else + print_color "服务器未在运行" "$YELLOW" + fi } -# 显示服务器状态 -show_status() { - if pgrep -f "nc -l -p $SERVER_PORT" >/dev/null; then - print_color "服务器运行中" "$GREEN" - print_color "监听端口: $SERVER_PORT" "$BLUE" +# 查看服务器日志 +show_log() { + if [[ -f "$LOG_FILE" ]]; then + tail -20 "$LOG_FILE" else - print_color "服务器未运行" "$RED" + print_color "日志文件不存在" "$RED" fi +} + +# 显示统计信息 +show_stats() { + local total_clients=$(get_client_count) + local active_clients=0 + local current_time=$(date +%s) - if [[ -f "$CLIENTS_FILE" ]]; then - client_count=$(grep -c . "$CLIENTS_FILE" 2>/dev/null || echo "0") - print_color "已连接客户端: $client_count" "$BLUE" - fi + while IFS='|' read -r serial ip hostname system last_seen; do + if [[ -n "$last_seen" ]]; then + local client_time=$(date -d "$last_seen" +%s 2>/dev/null || echo "0") + local time_diff=$((current_time - client_time)) + if [[ $time_diff -lt 600 ]]; then # 10分钟内活跃 + active_clients=$((active_clients + 1)) + fi + fi + done < "$CLIENTS_FILE" + + print_color "=== 服务器统计 ===" "$BLUE" + print_color "总客户端数: $total_clients" "$GREEN" + print_color "活跃客户端: $active_clients" "$CYAN" + print_color "服务器运行: $(is_server_running && echo '是' || echo '否')" "$YELLOW" + print_color "启动时间: $(ps -p $(cat "$PID_FILE" 2>/dev/null) -o lstart= 2>/dev/null || echo '未知')" "$PURPLE" +} + +# 显示帮助 +show_help() { + print_color "可用命令:" "$BLUE" + echo + print_color "服务器管理:" "$CYAN" + echo " start - 启动服务器" + echo " stop - 停止服务器" + echo " restart - 重启服务器" + echo " status - 服务器状态" + echo " log - 查看日志" + echo " stats - 统计信息" + echo " backup - 备份数据" + + print_color "客户端管理:" "$CYAN" + echo " list - 显示客户端列表" + echo " detail - 查看客户端详情" + echo " send - 发送命令到客户端" + echo " broadcast - 广播命令到所有客户端" + echo " script - 执行脚本到客户端" + echo " batch - 批量执行脚本" + + print_color "脚本管理:" "$CYAN" + echo " scripts - 显示可用脚本" + + print_color "系统管理:" "$CYAN" + echo " help - 显示帮助" + echo " exit - 退出" } # 主菜单 main_menu() { while true; do - print_color "=== 跨平台服务器控制器 ===" "$BLUE" - echo "1. 启动服务器" - echo "2. 显示客户端列表" - echo "3. 发送命令到客户端" - echo "4. 广播命令到所有客户端" - echo "5. 执行脚本" - echo "6. 显示可用脚本" - echo "7. 服务器状态" - echo "8. 停止服务器" - echo "9. 退出" + print_banner - read -p "请选择操作 [1-9]: " choice + echo + print_color "请选择操作:" "$BLUE" + echo + print_color "1. 启动服务器" "$GREEN" + print_color "2. 停止服务器" "$RED" + print_color "3. 客户端列表" "$CYAN" + print_color "4. 发送命令" "$YELLOW" + print_color "5. 广播命令" "$PURPLE" + print_color "6. 执行脚本" "$BLUE" + print_color "7. 批量执行" "$CYAN" + print_color "8. 客户端详情" "$GREEN" + print_color "9. 查看日志" "$YELLOW" + print_color "10. 统计信息" "$PURPLE" + print_color "11. 备份数据" "$BLUE" + print_color "12. 显示脚本" "$CYAN" + print_color "13. 帮助" "$GREEN" + print_color "0. 退出" "$RED" + echo + + read -p "请输入选择 [0-13]: " choice case $choice in - 1) - start_server - ;; - 2) - show_clients - ;; - 3) - read -p "输入序列号: " serial - read -p "输入命令: " command - send_command "$serial" "$command" - ;; + 1) start_server ;; + 2) stop_server ;; + 3) show_clients ;; 4) - read -p "输入广播命令: " command - broadcast_command "$command" + read -p "输入序列号: " serial + read -p "输入命令: " cmd + send_command "$serial" "$cmd" ;; 5) + read -p "输入广播命令: " cmd + broadcast_command "$cmd" + ;; + 6) read -p "输入序列号: " serial read -p "输入脚本名: " script execute_script "$serial" "$script" ;; - 6) - print_color "可用脚本:" "$BLUE" - ls "$SCRIPT_DIR"/*.sh 2>/dev/null | xargs -n 1 basename || echo "无可用脚本" - ;; 7) - show_status + read -p "输入脚本名: " script + batch_execute_script "$script" ;; 8) - stop_server + read -p "输入序列号: " serial + show_client_detail "$serial" ;; - 9) + 9) show_log ;; + 10) show_stats ;; + 11) backup_data ;; + 12) show_available_scripts ;; + 13) show_help ;; + 0) print_color "再见!" "$GREEN" exit 0 ;; @@ -413,28 +664,39 @@ main_menu() { echo read -p "按回车键继续..." - clear done } -# 脚本入口 +# 命令行参数处理 case "${1:-}" in - start) - check_root - install_dependencies - initialize + "start") + check_dependencies + init_config start_server ;; - stop) + "stop") stop_server ;; - status) - show_status + "restart") + stop_server + sleep 2 + check_dependencies + init_config + start_server + ;; + "status") + if is_server_running; then + print_color "服务器运行中 (PID: $(cat "$PID_FILE"))" "$GREEN" + else + print_color "服务器未运行" "$RED" + fi + ;; + "log") + show_log ;; *) - check_root - install_dependencies - initialize + check_dependencies + init_config main_menu ;; esac