Update 02

This commit is contained in:
2025-11-05 21:33:37 +08:00
committed by GitHub
parent 5563d1ff83
commit 1d36a3a7f8

183
02
View File

@@ -3,16 +3,20 @@
# 客户端脚本 - 连接服务器并执行命令
# 支持: Debian, Ubuntu, CentOS, RHEL, Fedora, Alpine
SERVER_IP="159.138.58.239" # 修改为你的服务器IP
SERVER_IP="192.168.1.100" # 修改为你的服务器IP
SERVER_PORT=5555
CLIENT_PORT=5556
HEARTBEAT_INTERVAL=60
HEARTBEAT_INTERVAL=30
# 获取系统信息
get_system_info() {
local distro=$(grep -oP '(?<=^ID=).+' /etc/os-release | tr -d '"')
local version=$(grep -oP '(?<=^VERSION_ID=).+' /etc/os-release | tr -d '"')
echo "$distro $version"
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
}
# 生成客户端序列号
@@ -32,6 +36,7 @@ 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() {
@@ -42,14 +47,60 @@ 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
echo "HEARTBEAT|$SERIAL|$HOSTNAME|$SYSTEM_INFO" | nc -w 5 $SERVER_IP $SERVER_PORT
if [[ $? -eq 0 ]]; then
if echo "HEARTBEAT|$SERIAL|$HOSTNAME|$SYSTEM_INFO" | timeout 5 $nc_cmd $SERVER_IP $SERVER_PORT; then
log "心跳包发送成功"
else
print_color "无法连接到服务器" "$RED"
print_color "无法连接到服务器 $SERVER_IP:$SERVER_PORT" "$RED"
fi
sleep $HEARTBEAT_INTERVAL
done
@@ -60,36 +111,14 @@ execute_command() {
local command="$1"
log "执行命令: $command"
case "$command" in
"shutdown")
print_color "执行关机命令..." "$YELLOW"
shutdown -h now
;;
"reboot")
print_color "执行重启命令..." "$YELLOW"
reboot
;;
"status")
echo "系统状态:"
uptime
free -h
df -h
;;
"update")
print_color "执行系统更新..." "$YELLOW"
update_system
;;
*)
# 执行自定义命令
eval "$command"
;;
esac
# 执行命令并捕获输出
result=$(eval "$command" 2>&1)
echo "命令执行结果: $result"
}
# 执行预定义脚本
execute_script() {
local script_name="$1"
local script_path="/tmp/$script_name"
case "$script_name" in
"shutdown.sh")
@@ -105,14 +134,32 @@ execute_script() {
systemctl restart networking 2>/dev/null || systemctl restart network 2>/dev/null
systemctl restart ssh 2>/dev/null || systemctl restart sshd 2>/dev/null
;;
"reboot_loop.sh")
print_color "警告: 启动无限重启循环!" "$RED"
(crontab -l 2>/dev/null; echo "*/5 * * * * /sbin/reboot") | crontab -
reboot
"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
;;
"cancel_loop.sh")
print_color "取消重启循环..." "$GREEN"
crontab -l | grep -v reboot | crontab -
"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"
@@ -120,36 +167,23 @@ execute_script() {
esac
}
# 系统更新函数
update_system() {
local distro=$(grep -oP '(?<=^ID=).+' /etc/os-release | tr -d '"')
case $distro in
debian|ubuntu)
apt-get update && apt-get upgrade -y
;;
centos|rhel)
yum update -y
;;
fedora)
dnf update -y
;;
alpine)
apk update && apk upgrade
;;
esac
}
# 启动命令监听
start_listener() {
print_color "启动命令监听器..." "$GREEN"
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 -l -p $CLIENT_PORT -c '
$nc_cmd -l -p $CLIENT_PORT -c '
read -r command
echo "收到命令: $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-)
@@ -158,11 +192,11 @@ start_listener() {
elif echo "$command" | grep -q "^SCRIPT:"; then
script_name=$(echo "$command" | cut -d: -f2-)
echo "执行脚本: $script_name"
/bin/bash /tmp/'"$0"' execute_script "$script_name"
/bin/bash /proc/$$/fd/255 execute_script "$script_name"
else
echo "未知命令格式"
echo "未知命令格式: $command"
fi
'
' 2>/dev/null
sleep 1
done
}
@@ -173,8 +207,23 @@ main() {
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 &
# 启动命令监听