#!/bin/bash # 客户端脚本 - 连接服务器并执行命令 # 支持: Debian, Ubuntu, CentOS, RHEL, Fedora, Alpine SERVER_IP="159.138.58.239" # 修改为你的服务器IP SERVER_PORT=5555 CLIENT_PORT=5556 HEARTBEAT_INTERVAL=30 # 获取系统信息 get_system_info() { if [[ -f /etc/os-release ]]; then local distro=$(grep -oP '(?<=^ID=).+' /etc/os-release | tr -d '"') local version=$(grep -oP '(?<=^VERSION_ID=).+' /etc/os-release | tr -d '"') echo "$distro $version" else echo "unknown" fi } # 生成客户端序列号 generate_serial() { if [[ -f /etc/machine-id ]]; then cat /etc/machine-id | md5sum | cut -c1-8 else date +%s | sha256sum | base64 | head -c 8 fi } SERIAL=$(generate_serial) HOSTNAME=$(hostname) SYSTEM_INFO=$(get_system_info) # 颜色输出 RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' log() { echo -e "$(date '+%Y-%m-%d %H:%M:%S') - $1" } print_color() { echo -e "${2}${1}${NC}" } # 检查命令是否存在 check_command() { if ! command -v "$1" &> /dev/null; then return 1 fi return 0 } # 安装依赖 install_client_deps() { local distro=$(grep -oP '(?<=^ID=).+' /etc/os-release | tr -d '"') case $distro in debian|ubuntu) apt-get update apt-get install -y netcat-traditional ;; centos|rhel|fedora) if command -v dnf &> /dev/null; then dnf install -y nc else yum install -y nc fi ;; alpine) apk add netcat-openbsd ;; esac } # 获取netcat命令 get_nc_command() { if check_command nc; then echo "nc" elif check_command netcat; then echo "netcat" else echo "" fi } # 发送心跳包 send_heartbeat() { local nc_cmd=$(get_nc_command) if [[ -z "$nc_cmd" ]]; then print_color "错误: netcat 未安装" "$RED" return 1 fi while true; do if echo "HEARTBEAT|$SERIAL|$HOSTNAME|$SYSTEM_INFO" | timeout 5 $nc_cmd $SERVER_IP $SERVER_PORT; then log "心跳包发送成功" else print_color "无法连接到服务器 $SERVER_IP:$SERVER_PORT" "$RED" fi sleep $HEARTBEAT_INTERVAL done } # 执行命令 execute_command() { local command="$1" log "执行命令: $command" # 执行命令并捕获输出 result=$(eval "$command" 2>&1) echo "命令执行结果: $result" } # 执行预定义脚本 execute_script() { local script_name="$1" case "$script_name" in "shutdown.sh") print_color "执行关机脚本..." "$RED" shutdown -h now ;; "reboot.sh") print_color "执行重启脚本..." "$YELLOW" reboot ;; "restart_services.sh") print_color "重启系统服务..." "$YELLOW" systemctl restart networking 2>/dev/null || systemctl restart network 2>/dev/null systemctl restart ssh 2>/dev/null || systemctl restart sshd 2>/dev/null ;; "system_info.sh") print_color "收集系统信息..." "$GREEN" echo "=== 系统信息 ===" echo "主机名: $(hostname)" echo "序列号: $SERIAL" echo "系统: $SYSTEM_INFO" echo "内核: $(uname -r)" echo "架构: $(uname -m)" echo "上线时间: $(uptime -p)" echo "内存使用:" free -h echo "磁盘使用:" df -h ;; "update_system.sh") print_color "开始系统更新..." "$YELLOW" 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 "系统更新完成!" ;; *) print_color "未知脚本: $script_name" "$RED" ;; esac } # 启动命令监听 start_listener() { local nc_cmd=$(get_nc_command) if [[ -z "$nc_cmd" ]]; then print_color "错误: netcat 未安装,无法启动监听器" "$RED" return 1 fi print_color "启动命令监听器在端口 $CLIENT_PORT..." "$GREEN" print_color "客户端信息: $SERIAL - $HOSTNAME - $SYSTEM_INFO" "$BLUE" while true; do log "等待命令..." $nc_cmd -l -p $CLIENT_PORT -c ' read -r command timestamp=$(date "+%Y-%m-%d %H:%M:%S") echo "$timestamp - 收到命令: $command" if echo "$command" | grep -q "^COMMAND:"; then cmd=$(echo "$command" | cut -d: -f2-) echo "执行命令: $cmd" eval "$cmd" elif echo "$command" | grep -q "^SCRIPT:"; then script_name=$(echo "$command" | cut -d: -f2-) echo "执行脚本: $script_name" /bin/bash /proc/$$/fd/255 execute_script "$script_name" else echo "未知命令格式: $command" fi ' 2>/dev/null sleep 1 done } # 主函数 main() { print_color "=== 控制器客户端 ===" "$BLUE" print_color "序列号: $SERIAL" "$GREEN" print_color "主机名: $HOSTNAME" "$GREEN" print_color "系统: $SYSTEM_INFO" "$GREEN" print_color "服务器: $SERVER_IP:$SERVER_PORT" "$BLUE" # 检查netcat local nc_cmd=$(get_nc_command) if [[ -z "$nc_cmd" ]]; then print_color "安装netcat依赖..." "$YELLOW" install_client_deps nc_cmd=$(get_nc_command) if [[ -z "$nc_cmd" ]]; then print_color "错误: 无法安装netcat,请手动安装" "$RED" exit 1 fi fi print_color "使用 netcat 命令: $nc_cmd" "$GREEN" # 启动心跳包发送(后台进程) send_heartbeat & # 启动命令监听 start_listener } # 脚本入口 case "${1:-}" in "execute_script") execute_script "$2" ;; *) main ;; esac