Update 01

This commit is contained in:
2025-11-05 23:00:19 +08:00
committed by GitHub
parent 357cf8481c
commit b6ad620cf7

696
01
View File

@@ -1,190 +1,299 @@
#!/bin/bash #!/bin/bash
# 服务器控制器脚本 - 监听端口5555 # 加强版服务器控制器脚本
# 支持: Debian, Ubuntu, CentOS, RHEL, Fedora, Alpine # 支持多客户端管理、日志记录、安全控制
SERVER_PORT=5555 SERVER_PORT=25555
LOG_FILE="/var/log/controller_server.log" LOG_FILE="/var/log/controller_server.log"
CLIENTS_FILE="/var/lib/controller_clients.txt" CLIENTS_FILE="/var/lib/controller_clients.txt"
SCRIPT_DIR="/opt/controller_scripts" 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' RED='\033[0;31m'
GREEN='\033[0;32m' GREEN='\033[0;32m'
YELLOW='\033[1;33m' YELLOW='\033[1;33m'
BLUE='\033[0;34m' 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() { 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() { print_color() {
echo -e "${2}${1}${NC}" echo -e "${2}${1}${NC}"
} }
# 检查命令是否存在 print_banner() {
check_command() { clear
if ! command -v "$1" &> /dev/null; then echo
return 1 print_color "╔══════════════════════════════════════════════════════════════╗" "$CYAN"
fi print_color "║ 加强版服务器控制器 v2.0 ║" "$BLUE"
return 0 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() { check_dependencies() {
if [[ $EUID -ne 0 ]]; then local deps=("nc" "awk" "grep" "sed")
print_color "错误: 此脚本需要root权限运行" "$RED" local missing=()
exit 1
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 fi
} }
# 安装依赖 # 创建管理脚本
install_dependencies() { create_management_scripts() {
local distro=$(grep -oP '(?<=^ID=).+' /etc/os-release | tr -d '"') # 批量更新脚本
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
print_color "检测到系统: $distro" "$BLUE" # 系统信息收集脚本
print_color "安装必要依赖..." "$YELLOW" cat > "$SCRIPT_DIR/collect_system_info.sh" << 'EOF'
#!/bin/bash
case $distro in echo "收集所有客户端系统信息..."
debian|ubuntu) while IFS='|' read -r serial ip hostname system last_seen; do
apt-get update if [[ -n "$serial" ]]; then
apt-get install -y netcat-traditional net-tools iproute2 echo "=== $hostname ($serial) ==="
;; echo "COMMAND:uname -a; free -h; df -h" | nc -w 3 "$ip" 5556
centos|rhel|fedora) echo "------------------------"
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 fi
done < /var/lib/controller_clients.txt
EOF
print_color "依赖安装完成!" "$GREEN" # 网络诊断脚本
} cat > "$SCRIPT_DIR/network_diagnosis.sh" << 'EOF'
#!/bin/bash
# 获取netcat命令 echo "网络诊断..."
get_nc_command() { while IFS='|' read -r serial ip hostname system last_seen; do
if check_command nc; then if [[ -n "$serial" ]]; then
echo "nc" echo "检查 $hostname 网络..."
elif check_command netcat; then echo "COMMAND:ip addr show; ping -c 2 8.8.8.8" | nc -w 3 "$ip" 5556
echo "netcat"
else
echo ""
fi fi
} done < /var/lib/controller_clients.txt
EOF
# 初始化环境 chmod +x "$SCRIPT_DIR"/*.sh
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"
} }
create_sample_scripts() { create_sample_scripts() {
# 关机脚本 # 基础管理脚本
cat > "$SCRIPT_DIR/shutdown.sh" << 'EOF' cat > "$SCRIPT_DIR/shutdown.sh" << 'EOF'
#!/bin/bash #!/bin/bash
echo "执行关机操作..." echo "执行关机操作..."
shutdown -h now shutdown -h now
EOF EOF
# 重启脚本
cat > "$SCRIPT_DIR/reboot.sh" << 'EOF' cat > "$SCRIPT_DIR/reboot.sh" << 'EOF'
#!/bin/bash #!/bin/bash
echo "执行重启操作..." echo "执行重启操作..."
reboot reboot
EOF EOF
# 服务重启脚本
cat > "$SCRIPT_DIR/restart_services.sh" << 'EOF' cat > "$SCRIPT_DIR/restart_services.sh" << 'EOF'
#!/bin/bash #!/bin/bash
echo "重启网络服务..." echo "重启系统服务..."
systemctl restart networking 2>/dev/null || systemctl restart network 2>/dev/null 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 systemctl restart ssh 2>/dev/null || systemctl restart sshd 2>/dev/null
EOF EOF
# 系统信息脚本 # 监控脚本
cat > "$SCRIPT_DIR/system_info.sh" << 'EOF' cat > "$SCRIPT_DIR/monitor_system.sh" << 'EOF'
#!/bin/bash #!/bin/bash
echo "=== 系统信息 ===" echo "=== 系统监控 ==="
echo "主机名: $(hostname)" echo "主机名: $(hostname)"
echo "系统: $(grep PRETTY_NAME /etc/os-release | cut -d= -f2 | tr -d '\"')" echo "上线时间: $(uptime)"
echo "内核: $(uname -r)"
echo "架构: $(uname -m)"
echo "上线时间: $(uptime -p)"
echo "内存使用:" echo "内存使用:"
free -h free -h
echo "磁盘使用:" echo "磁盘使用:"
df -h df -h
echo "CPU使用:"
top -bn1 | head -10
EOF EOF
# 更新系统脚本 # 安全脚本
cat > "$SCRIPT_DIR/update_system.sh" << 'EOF' cat > "$SCRIPT_DIR/security_check.sh" << 'EOF'
#!/bin/bash #!/bin/bash
echo "开始系统更新..." echo "=== 安全检查 ==="
if command -v apt-get &> /dev/null; then echo "登录用户:"
apt-get update && apt-get upgrade -y who
elif command -v yum &> /dev/null; then echo "失败登录:"
yum update -y lastb | head -10
elif command -v dnf &> /dev/null; then echo "SSH连接:"
dnf update -y netstat -tlnp | grep ssh
elif command -v apk &> /dev/null; then
apk update && apk upgrade
fi
echo "系统更新完成!"
EOF EOF
chmod +x "$SCRIPT_DIR"/*.sh 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() { show_clients() {
if [[ ! -s "$CLIENTS_FILE" ]]; then cleanup_expired_clients
print_color "没有已连接的客户端" "$YELLOW"
local count=$(get_client_count)
print_color "已连接客户端: $count" "$BLUE"
if [[ $count -eq 0 ]]; then
print_color "没有客户端连接" "$YELLOW"
return return
fi fi
print_color "已连接的客户端列表:" "$BLUE" print_color "┌────────────┬───────────────┬──────────────────┬─────────────────┬─────────────────────┐" "$CYAN"
print_color "序列号 | IP地址 | 主机名 | 最后在线" "$BLUE" print_color "序列号 IP地址 主机名 │ 系统 │ 最后在线 │" "$CYAN"
print_color "----------------------------------------------------" "$BLUE" print_color "├────────────┼───────────────┼──────────────────┼─────────────────┼─────────────────────┤" "$CYAN"
while IFS='|' read -r serial ip hostname system last_seen; do 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" done < "$CLIENTS_FILE"
print_color "└────────────┴───────────────┴──────────────────┴─────────────────┴─────────────────────┘" "$CYAN"
} }
# 向客户端发送命令 # 向客户端发送命令
send_command() { send_command() {
local serial=$1 local serial=$1
local command=$2 shift
local command="$*"
if [[ -z "$serial" || -z "$command" ]]; then if [[ -z "$serial" || -z "$command" ]]; then
print_color "用法: send <序列号> <命令>" "$RED" print_color "用法: send <序列号> <命令>" "$RED"
@@ -199,64 +308,63 @@ send_command() {
IFS='|' read -r serial ip hostname system last_seen <<< "$client_info" 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) echo "COMMAND:$command" | nc -w 5 "$ip" 5556
if [[ -z "$nc_cmd" ]]; then
print_color "错误: netcat 未安装" "$RED"
return
fi
echo "COMMAND:$command" | timeout 5 $nc_cmd "$ip" 5556
if [[ $? -eq 0 ]]; then if [[ $? -eq 0 ]]; then
log "命令发送成功: $serial -> $command"
print_color "命令发送成功!" "$GREEN" print_color "命令发送成功!" "$GREEN"
else else
log "命令发送失败: $serial -> $command"
print_color "命令发送失败!" "$RED" print_color "命令发送失败!" "$RED"
fi fi
} }
# 广播命令到所有客户端 # 广播命令到所有客户端
broadcast_command() { broadcast_command() {
local command=$1 local command="$*"
if [[ -z "$command" ]]; then if [[ -z "$command" ]]; then
print_color "用法: broadcast <命令>" "$RED" print_color "用法: broadcast <命令>" "$RED"
return return
fi fi
print_color "向所有客户端广播命令: $command" "$YELLOW" local count=0
print_color "向所有客户端广播命令..." "$YELLOW"
local nc_cmd=$(get_nc_command) print_color "命令: $command" "$PURPLE"
if [[ -z "$nc_cmd" ]]; then
print_color "错误: netcat 未安装" "$RED"
return
fi
while IFS='|' read -r serial ip hostname system last_seen; do while IFS='|' read -r serial ip hostname system last_seen; do
if [[ -n "$serial" && -n "$ip" ]]; then if [[ -n "$serial" ]]; then
echo "向 $serial ($hostname) 发送命令..." echo "向 $serial 发送命令..."
echo "COMMAND:$command" | timeout 3 $nc_cmd "$ip" 5556 echo "COMMAND:$command" | nc -w 3 "$ip" 5556 &
count=$((count + 1))
fi fi
done < "$CLIENTS_FILE" done < "$CLIENTS_FILE"
wait
log "广播命令完成: $command -> $count 个客户端"
print_color "广播完成! 共发送给 $count 个客户端" "$GREEN"
} }
# 执行本地脚本 # 执行脚本
execute_script() { execute_script() {
local serial=$1 local serial=$1
local script_name=$2 local script_name=$2
if [[ -z "$serial" || -z "$script_name" ]]; then if [[ -z "$serial" || -z "$script_name" ]]; then
print_color "用法: script <序列号> <脚本名>" "$RED" print_color "用法: script <序列号> <脚本名>" "$RED"
print_color "可用脚本:" "$BLUE" show_available_scripts
ls "$SCRIPT_DIR"/*.sh | xargs -n 1 basename
return return
fi fi
local script_path="$SCRIPT_DIR/$script_name" local script_path="$SCRIPT_DIR/$script_name"
if [[ ! -f "$script_path" ]]; then if [[ ! -f "$script_path" ]]; then
print_color "错误: 脚本 $script_name 不存在" "$RED" print_color "错误: 脚本 $script_name 不存在" "$RED"
show_available_scripts
return return
fi fi
@@ -268,50 +376,135 @@ execute_script() {
IFS='|' read -r serial ip hostname system last_seen <<< "$client_info" 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 echo "SCRIPT:$script_name" | nc -w 5 "$ip" 5556
print_color "错误: netcat 未安装" "$RED"
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 return
fi fi
# 发送脚本执行命令 local script_path="$SCRIPT_DIR/$script_name"
echo "SCRIPT:$script_name" | timeout 5 $nc_cmd "$ip" 5556 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() { start_server() {
local nc_cmd=$(get_nc_command) if is_server_running; then
if [[ -z "$nc_cmd" ]]; then print_color "服务器已经在运行 (PID: $(cat "$PID_FILE"))" "$YELLOW"
print_color "错误: netcat 未安装,无法启动服务器" "$RED" return
return 1
fi fi
print_color "启动控制器服务器在端口 $SERVER_PORT..." "$GREEN" print_color "启动服务器在端口 $SERVER_PORT ..." "$GREEN"
print_color "使用 netcat 命令: $nc_cmd" "$BLUE"
print_color "服务器日志: $LOG_FILE" "$BLUE" # 保存PID
print_color "客户端列表: $CLIENTS_FILE" "$BLUE" echo $$ > "$PID_FILE"
print_color "按 Ctrl+C 停止服务器" "$YELLOW"
# 设置信号处理
trap 'cleanup' INT TERM EXIT
log "服务器启动成功PID: $$"
while true; do while true; do
log "等待客户端连接..." log "等待客户端连接..."
$nc_cmd -l -p $SERVER_PORT -c ' nc -l -p $SERVER_PORT -c '
client_ip=$(echo $SSH_CLIENT | awk "{print \$1}") client_ip=$(echo $SSH_CLIENT | awk "{print \$1}")
if [[ -z "$client_ip" ]]; then if [[ -z "$client_ip" ]]; then
client_ip="unknown" client_ip="unknown"
fi fi
read -r data read -r data
timestamp=$(date "+%Y-%m-%d %H:%M:%S") timestamp=$(date "+%Y-%m-%d %H:%M:%S")
if echo "$data" | grep -q "HEARTBEAT|"; then if echo "$data" | grep -q "HEARTBEAT|"; then
IFS="|" read -r heartbeat serial hostname system <<< "$data" IFS="|" read -r heartbeat serial hostname system <<< "$data"
# 更新或添加客户端信息
# 更新客户端信息
grep -v "^$serial|" /var/lib/controller_clients.txt > /tmp/clients.tmp 2>/dev/null grep -v "^$serial|" /var/lib/controller_clients.txt > /tmp/clients.tmp 2>/dev/null
echo "$serial|$client_ip|$hostname|$system|$timestamp" >> /tmp/clients.tmp echo "$serial|$client_ip|$hostname|$system|$timestamp" >> /tmp/clients.tmp
mv /tmp/clients.tmp /var/lib/controller_clients.txt mv /tmp/clients.tmp /var/lib/controller_clients.txt
echo "ACK:OK" echo "ACK:OK"
echo "客户端 $serial 已连接 - $hostname" echo "客户端 $serial 已连接 - $hostname ($client_ip)"
else else
echo "ACK:UNKNOWN_COMMAND" echo "ACK:UNKNOWN_COMMAND"
fi fi
@@ -320,89 +513,147 @@ start_server() {
done done
} }
# 显示帮助 cleanup() {
show_help() { log "服务器停止"
print_color "服务器控制器命令:" "$BLUE" rm -f "$PID_FILE"
echo "list - 显示客户端列表" exit 0
echo "send - 向指定客户端发送命令"
echo "broadcast - 向所有客户端广播命令"
echo "script - 执行预定义脚本"
echo "scripts - 显示可用脚本"
echo "start - 启动服务器"
echo "stop - 停止服务器"
echo "status - 显示服务器状态"
echo "help - 显示此帮助"
} }
# 停止服务器 # 停止服务器
stop_server() { stop_server() {
pkill -f "nc -l -p $SERVER_PORT" if is_server_running; then
print_color "服务器已停止" "$GREEN" 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() { show_log() {
if pgrep -f "nc -l -p $SERVER_PORT" >/dev/null; then if [[ -f "$LOG_FILE" ]]; then
print_color "服务器运行中" "$GREEN" tail -20 "$LOG_FILE"
print_color "监听端口: $SERVER_PORT" "$BLUE"
else else
print_color "服务器未运行" "$RED" print_color "日志文件不存在" "$RED"
fi fi
}
if [[ -f "$CLIENTS_FILE" ]]; then # 显示统计信息
client_count=$(grep -c . "$CLIENTS_FILE" 2>/dev/null || echo "0") show_stats() {
print_color "已连接客户端: $client_count" "$BLUE" local total_clients=$(get_client_count)
fi local active_clients=0
local current_time=$(date +%s)
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() { main_menu() {
while true; do while true; do
print_color "=== 跨平台服务器控制器 ===" "$BLUE" print_banner
echo "1. 启动服务器"
echo "2. 显示客户端列表"
echo "3. 发送命令到客户端"
echo "4. 广播命令到所有客户端"
echo "5. 执行脚本"
echo "6. 显示可用脚本"
echo "7. 服务器状态"
echo "8. 停止服务器"
echo "9. 退出"
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 case $choice in
1) 1) start_server ;;
start_server 2) stop_server ;;
;; 3) show_clients ;;
2)
show_clients
;;
3)
read -p "输入序列号: " serial
read -p "输入命令: " command
send_command "$serial" "$command"
;;
4) 4)
read -p "输入广播命令: " command read -p "输入序列号: " serial
broadcast_command "$command" read -p "输入命令: " cmd
send_command "$serial" "$cmd"
;; ;;
5) 5)
read -p "输入广播命令: " cmd
broadcast_command "$cmd"
;;
6)
read -p "输入序列号: " serial read -p "输入序列号: " serial
read -p "输入脚本名: " script read -p "输入脚本名: " script
execute_script "$serial" "$script" execute_script "$serial" "$script"
;; ;;
6)
print_color "可用脚本:" "$BLUE"
ls "$SCRIPT_DIR"/*.sh 2>/dev/null | xargs -n 1 basename || echo "无可用脚本"
;;
7) 7)
show_status read -p "输入脚本名: " script
batch_execute_script "$script"
;; ;;
8) 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" print_color "再见!" "$GREEN"
exit 0 exit 0
;; ;;
@@ -413,28 +664,39 @@ main_menu() {
echo echo
read -p "按回车键继续..." read -p "按回车键继续..."
clear
done done
} }
# 脚本入口 # 命令行参数处理
case "${1:-}" in case "${1:-}" in
start) "start")
check_root check_dependencies
install_dependencies init_config
initialize
start_server start_server
;; ;;
stop) "stop")
stop_server stop_server
;; ;;
status) "restart")
show_status 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 check_dependencies
install_dependencies init_config
initialize
main_menu main_menu
;; ;;
esac esac