789 lines
24 KiB
Bash
789 lines
24 KiB
Bash
#!/bin/bash
|
|
|
|
# 稳定版实时命令监控脚本 - 带离线IP库支持
|
|
# 版本: 3.2
|
|
|
|
set -e
|
|
|
|
### 配置区域 ###
|
|
LOG_DIR="/root/command_monitor_logs"
|
|
MAX_LOG_SIZE="1M"
|
|
MAX_LOG_FILES=50
|
|
LOG_ROTATE_INTERVAL=1800
|
|
MEMORY_LIMIT="512M"
|
|
CPU_LIMIT=90
|
|
CHECK_INTERVAL=300
|
|
BACKUP_DAYS=7
|
|
CLEANUP_INTERVAL=3600
|
|
|
|
### 颜色定义 ###
|
|
RED='\033[0;31m'
|
|
GREEN='\033[0;32m'
|
|
YELLOW='\033[1;33m'
|
|
BLUE='\033[0;34m'
|
|
PURPLE='\033[0;35m'
|
|
CYAN='\033[0;36m'
|
|
NC='\033[0m'
|
|
|
|
### 全局变量 ###
|
|
SCRIPT_PID=$$
|
|
MONITOR_PID=""
|
|
LAST_CLEANUP=0
|
|
LAST_ROTATION=0
|
|
CURRENT_LOG=""
|
|
DAEMON_MODE=false
|
|
|
|
# 简单离线IP库 - 中国常见IP段
|
|
declare -A IP_LOCAL_DB=(
|
|
["1."]="中国-APNIC"
|
|
["14."]="中国-APNIC"
|
|
["27."]="中国-APNIC"
|
|
["36."]="中国-电信"
|
|
["39."]="中国-电信"
|
|
["42."]="中国-电信"
|
|
["49."]="中国-电信"
|
|
["58."]="中国-电信"
|
|
["59."]="中国-电信"
|
|
["60."]="中国-电信"
|
|
["61."]="中国-电信"
|
|
["106."]="中国-电信"
|
|
["110."]="中国-电信"
|
|
["111."]="中国-电信"
|
|
["112."]="中国-电信"
|
|
["113."]="中国-电信"
|
|
["114."]="中国-电信"
|
|
["115."]="中国-电信"
|
|
["116."]="中国-电信"
|
|
["117."]="中国-电信"
|
|
["118."]="中国-电信"
|
|
["119."]="中国-电信"
|
|
["120."]="中国-电信"
|
|
["121."]="中国-电信"
|
|
["122."]="中国-电信"
|
|
["123."]="中国-电信"
|
|
["124."]="中国-电信"
|
|
["125."]="中国-电信"
|
|
["139."]="中国-电信"
|
|
["140."]="中国-电信"
|
|
["171."]="中国-电信"
|
|
["175."]="中国-电信"
|
|
["180."]="中国-电信"
|
|
["182."]="中国-电信"
|
|
["183."]="中国-电信"
|
|
["202."]="中国-电信"
|
|
["203."]="中国-电信"
|
|
["210."]="中国-电信"
|
|
["218."]="中国-电信"
|
|
["219."]="中国-电信"
|
|
["220."]="中国-电信"
|
|
["221."]="中国-电信"
|
|
["222."]="中国-电信"
|
|
["223."]="中国-电信"
|
|
["14.16."]="中国-广东-电信"
|
|
["14.17."]="中国-广东-电信"
|
|
["14.18."]="中国-广东-电信"
|
|
["14.19."]="中国-广东-电信"
|
|
["27.16."]="中国-湖北-电信"
|
|
["27.17."]="中国-湖北-电信"
|
|
["27.18."]="中国-湖北-电信"
|
|
["27.19."]="中国-湖北-电信"
|
|
["36.32."]="中国-浙江-电信"
|
|
["36.33."]="中国-浙江-电信"
|
|
["36.34."]="中国-浙江-电信"
|
|
["36.35."]="中国-浙江-电信"
|
|
["36.36."]="中国-浙江-电信"
|
|
["36.37."]="中国-浙江-电信"
|
|
["36.38."]="中国-浙江-电信"
|
|
["36.39."]="中国-浙江-电信"
|
|
["39.128."]="中国-云南-电信"
|
|
["39.129."]="中国-云南-电信"
|
|
["39.130."]="中国-云南-电信"
|
|
["39.131."]="中国-云南-电信"
|
|
["42.48."]="中国-湖南-电信"
|
|
["42.49."]="中国-湖南-电信"
|
|
["42.50."]="中国-湖南-电信"
|
|
["42.51."]="中国-湖南-电信"
|
|
["42.52."]="中国-湖南-电信"
|
|
["42.53."]="中国-湖南-电信"
|
|
["42.54."]="中国-湖南-电信"
|
|
["42.55."]="中国-湖南-电信"
|
|
["42.56."]="中国-湖南-电信"
|
|
["42.57."]="中国-湖南-电信"
|
|
["42.58."]="中国-湖南-电信"
|
|
["42.59."]="中国-湖南-电信"
|
|
["42.60."]="中国-湖南-电信"
|
|
["42.61."]="中国-湖南-电信"
|
|
["42.62."]="中国-湖南-电信"
|
|
["42.63."]="中国-湖南-电信"
|
|
["49.64."]="中国-江苏-电信"
|
|
["49.65."]="中国-江苏-电信"
|
|
["49.66."]="中国-江苏-电信"
|
|
["49.67."]="中国-江苏-电信"
|
|
["49.68."]="中国-江苏-电信"
|
|
["49.69."]="中国-江苏-电信"
|
|
["49.70."]="中国-江苏-电信"
|
|
["49.71."]="中国-江苏-电信"
|
|
["49.72."]="中国-江苏-电信"
|
|
["49.73."]="中国-江苏-电信"
|
|
["49.74."]="中国-江苏-电信"
|
|
["49.75."]="中国-江苏-电信"
|
|
["49.76."]="中国-江苏-电信"
|
|
["49.77."]="中国-江苏-电信"
|
|
["49.78."]="中国-江苏-电信"
|
|
["49.79."]="中国-江苏-电信"
|
|
["49.80."]="中国-江苏-电信"
|
|
["49.81."]="中国-江苏-电信"
|
|
["49.82."]="中国-江苏-电信"
|
|
["49.83."]="中国-江苏-电信"
|
|
["49.84."]="中国-江苏-电信"
|
|
["49.85."]="中国-江苏-电信"
|
|
["49.86."]="中国-江苏-电信"
|
|
["49.87."]="中国-江苏-电信"
|
|
["49.88."]="中国-江苏-电信"
|
|
["49.89."]="中国-江苏-电信"
|
|
["49.90."]="中国-江苏-电信"
|
|
["49.91."]="中国-江苏-电信"
|
|
["49.92."]="中国-江苏-电信"
|
|
["49.93."]="中国-江苏-电信"
|
|
["49.94."]="中国-江苏-电信"
|
|
["49.95."]="中国-江苏-电信"
|
|
["49.96."]="中国-江苏-电信"
|
|
["49.97."]="中国-江苏-电信"
|
|
["49.98."]="中国-江苏-电信"
|
|
["49.99."]="中国-江苏-电信"
|
|
["58.16."]="中国-贵州-电信"
|
|
["58.17."]="中国-贵州-电信"
|
|
["58.18."]="中国-贵州-电信"
|
|
["58.19."]="中国-贵州-电信"
|
|
["58.20."]="中国-湖南-电信"
|
|
["58.21."]="中国-湖南-电信"
|
|
["58.22."]="中国-湖南-电信"
|
|
["58.23."]="中国-湖南-电信"
|
|
["58.24."]="中国-江苏-电信"
|
|
["58.25."]="中国-上海-电信"
|
|
["58.26."]="中国-福建-电信"
|
|
["58.27."]="中国-福建-电信"
|
|
["58.28."]="中国-福建-电信"
|
|
["58.29."]="中国-福建-电信"
|
|
["58.30."]="中国-上海-电信"
|
|
["58.31."]="中国-上海-电信"
|
|
["58.32."]="中国-上海-电信"
|
|
["58.33."]="中国-上海-电信"
|
|
["58.34."]="中国-上海-电信"
|
|
["58.35."]="中国-上海-电信"
|
|
["58.36."]="中国-上海-电信"
|
|
["58.37."]="中国-上海-电信"
|
|
["58.38."]="中国-上海-电信"
|
|
["58.39."]="中国-上海-电信"
|
|
["58.40."]="中国-上海-电信"
|
|
["58.41."]="中国-上海-电信"
|
|
["58.42."]="中国-上海-电信"
|
|
["58.43."]="中国-上海-电信"
|
|
["58.44."]="中国-上海-电信"
|
|
["58.45."]="中国-上海-电信"
|
|
["58.46."]="中国-上海-电信"
|
|
["58.47."]="中国-上海-电信"
|
|
["58.48."]="中国-上海-电信"
|
|
["58.49."]="中国-上海-电信"
|
|
["58.50."]="中国-上海-电信"
|
|
["58.51."]="中国-上海-电信"
|
|
["58.52."]="中国-上海-电信"
|
|
["58.53."]="中国-上海-电信"
|
|
["58.54."]="中国-上海-电信"
|
|
["58.55."]="中国-上海-电信"
|
|
["58.56."]="中国-上海-电信"
|
|
["58.57."]="中国-上海-电信"
|
|
["58.58."]="中国-上海-电信"
|
|
["58.59."]="中国-上海-电信"
|
|
["58.60."]="中国-广东-电信"
|
|
["58.61."]="中国-广东-电信"
|
|
["58.62."]="中国-广东-电信"
|
|
["58.63."]="中国-广东-电信"
|
|
["59.32."]="中国-广东-电信"
|
|
["59.33."]="中国-广东-电信"
|
|
["59.34."]="中国-广东-电信"
|
|
["59.35."]="中国-广东-电信"
|
|
["59.36."]="中国-广东-电信"
|
|
["59.37."]="中国-广东-电信"
|
|
["59.38."]="中国-广东-电信"
|
|
["59.39."]="中国-广东-电信"
|
|
["59.40."]="中国-广东-电信"
|
|
["59.41."]="中国-广东-电信"
|
|
["59.42."]="中国-广东-电信"
|
|
["59.43."]="中国-广东-电信"
|
|
["59.44."]="中国-广东-电信"
|
|
["59.45."]="中国-广东-电信"
|
|
["59.46."]="中国-广东-电信"
|
|
["59.47."]="中国-广东-电信"
|
|
["59.48."]="中国-广东-电信"
|
|
["59.49."]="中国-广东-电信"
|
|
["59.50."]="中国-广东-电信"
|
|
["59.51."]="中国-广东-电信"
|
|
["59.52."]="中国-广东-电信"
|
|
["59.53."]="中国-广东-电信"
|
|
["59.54."]="中国-广东-电信"
|
|
["59.55."]="中国-广东-电信"
|
|
["59.56."]="中国-广东-电信"
|
|
["59.57."]="中国-广东-电信"
|
|
["59.58."]="中国-广东-电信"
|
|
["59.59."]="中国-广东-电信"
|
|
["59.60."]="中国-广东-电信"
|
|
["59.61."]="中国-广东-电信"
|
|
["59.62."]="中国-广东-电信"
|
|
["59.63."]="中国-广东-电信"
|
|
["59.64."]="中国-广东-电信"
|
|
["59.65."]="中国-广东-电信"
|
|
["59.66."]="中国-广东-电信"
|
|
["59.67."]="中国-广东-电信"
|
|
["59.68."]="中国-广东-电信"
|
|
["59.69."]="中国-广东-电信"
|
|
["59.70."]="中国-广东-电信"
|
|
["59.71."]="中国-广东-电信"
|
|
["59.72."]="中国-广东-电信"
|
|
["59.73."]="中国-广东-电信"
|
|
["59.74."]="中国-广东-电信"
|
|
["59.75."]="中国-广东-电信"
|
|
["59.76."]="中国-广东-电信"
|
|
["59.77."]="中国-广东-电信"
|
|
["59.78."]="中国-广东-电信"
|
|
["59.79."]="中国-广东-电信"
|
|
["59.80."]="中国-广东-电信"
|
|
["59.81."]="中国-广东-电信"
|
|
["59.82."]="中国-广东-电信"
|
|
["59.83."]="中国-广东-电信"
|
|
["59.84."]="中国-广东-电信"
|
|
["59.85."]="中国-广东-电信"
|
|
["59.86."]="中国-广东-电信"
|
|
["59.87."]="中国-广东-电信"
|
|
["59.88."]="中国-广东-电信"
|
|
["59.89."]="中国-广东-电信"
|
|
["59.90."]="中国-广东-电信"
|
|
["59.91."]="中国-广东-电信"
|
|
["59.92."]="中国-广东-电信"
|
|
["59.93."]="中国-广东-电信"
|
|
["59.94."]="中国-广东-电信"
|
|
["59.95."]="中国-广东-电信"
|
|
["59.96."]="中国-广东-电信"
|
|
["59.97."]="中国-广东-电信"
|
|
["59.98."]="中国-广东-电信"
|
|
["59.99."]="中国-广东-电信"
|
|
["113.194."]="中国-江西-电信"
|
|
)
|
|
|
|
# 获取时间戳
|
|
timestamp() {
|
|
date '+%Y-%m-%d %H:%M:%S'
|
|
}
|
|
|
|
# 日志函数
|
|
log_message() {
|
|
local level="$1"
|
|
local message="$2"
|
|
local color="$GREEN"
|
|
|
|
case "$level" in
|
|
"ERROR") color="$RED" ;;
|
|
"WARN") color="$YELLOW" ;;
|
|
"INFO") color="$BLUE" ;;
|
|
"SUCCESS") color="$GREEN" ;;
|
|
"COMMAND") color="$CYAN" ;;
|
|
esac
|
|
|
|
if [ "$DAEMON_MODE" = true ]; then
|
|
echo -e "${color}[$(timestamp)] [$level] $message${NC}" >> "$CURRENT_LOG"
|
|
else
|
|
echo -e "${color}[$(timestamp)] [$level] $message${NC}" | tee -a "$CURRENT_LOG"
|
|
fi
|
|
}
|
|
|
|
# 离线IP地理位置查询
|
|
get_ip_location_offline() {
|
|
local ip="$1"
|
|
|
|
# 检查内网IP
|
|
if [[ "$ip" == "192.168."* ]] || [[ "$ip" == "10."* ]] || [[ "$ip" == "172."* ]]; then
|
|
echo "内网IP"
|
|
return 0
|
|
fi
|
|
|
|
if [[ "$ip" == "127.0.0.1" ]] || [[ "$ip" == "localhost" ]] || [[ "$ip" == "unknown" ]]; then
|
|
echo "本机"
|
|
return 0
|
|
fi
|
|
|
|
# 在离线数据库中查找
|
|
for prefix in "${!IP_LOCAL_DB[@]}"; do
|
|
if [[ "$ip" == "$prefix"* ]]; then
|
|
echo "${IP_LOCAL_DB[$prefix]}"
|
|
return 0
|
|
fi
|
|
done
|
|
|
|
# 根据IP段判断国家
|
|
local first_octet=$(echo "$ip" | cut -d. -f1)
|
|
case $first_octet in
|
|
1|14|27|36|39|42|49|58|59|60|61|106|110|111|112|113|114|115|116|117|118|119|120|121|122|123|124|125|139|140|171|175|180|182|183|202|203|210|218|219|220|221|222|223)
|
|
echo "中国-未知地区"
|
|
;;
|
|
8|12|13|23|24|32|34|35|37|38|40|45|47|50|52|54|63|64|65|66|67|68|69|70|71|72|73|74|75|76|96|97|98|99|104|107|108|128|129|130|131|132|134|135|136|137|138|142|143|144|146|147|148|149|150|152|153|155|156|157|158|159|160|161|162|164|165|166|167|168|169|170|172|173|174|176|184|192|198|199|200|202|203|204|205|206|207|208|209)
|
|
echo "美国"
|
|
;;
|
|
5|31|37|46|62|77|78|79|80|81|82|83|84|85|86|87|88|89|90|91|92|93|94|95)
|
|
echo "欧洲"
|
|
;;
|
|
103|110|111|112|113|114|115|116|117|118|119|120|121|122|123|124|125|126|133|180|210|211|218|219|220|221)
|
|
echo "亚太地区"
|
|
;;
|
|
*)
|
|
echo "未知位置"
|
|
;;
|
|
esac
|
|
}
|
|
|
|
# 在线IP地理位置查询
|
|
get_ip_location_online() {
|
|
local ip="$1"
|
|
|
|
# 方法1: ip-api.com (最可靠)
|
|
local location_info=$(curl -s -m 3 "http://ip-api.com/json/$ip?fields=status,country,regionName,city,isp" 2>/dev/null || true)
|
|
if [ -n "$location_info" ] && echo "$location_info" | grep -q '"status":"success"'; then
|
|
local country=$(echo "$location_info" | grep -o '"country":"[^"]*"' | cut -d'"' -f4)
|
|
local region=$(echo "$location_info" | grep -o '"regionName":"[^"]*"' | cut -d'"' -f4)
|
|
local city=$(echo "$location_info" | grep -o '"city":"[^"]*"' | cut -d'"' -f4)
|
|
local isp=$(echo "$location_info" | grep -o '"isp":"[^"]*"' | cut -d'"' -f4)
|
|
|
|
if [ -n "$country" ]; then
|
|
local result="$country"
|
|
[ -n "$region" ] && result="$result-$region"
|
|
[ -n "$city" ] && result="$result-$city"
|
|
[ -n "$isp" ] && result="$result($isp)"
|
|
echo "$result"
|
|
return 0
|
|
fi
|
|
fi
|
|
|
|
# 方法2: 使用ipapi.co
|
|
location_info=$(curl -s -m 3 "https://ipapi.co/$ip/json/" 2>/dev/null || true)
|
|
if [ -n "$location_info" ]; then
|
|
local country=$(echo "$location_info" | grep -o '"country_name":"[^"]*"' | cut -d'"' -f4)
|
|
local city=$(echo "$location_info" | grep -o '"city":"[^"]*"' | cut -d'"' -f4)
|
|
local isp=$(echo "$location_info" | grep -o '"org":"[^"]*"' | cut -d'"' -f4)
|
|
|
|
if [ -n "$country" ]; then
|
|
local result="$country"
|
|
[ -n "$city" ] && result="$result-$city"
|
|
[ -n "$isp" ] && result="$result($isp)"
|
|
echo "$result"
|
|
return 0
|
|
fi
|
|
fi
|
|
|
|
return 1
|
|
}
|
|
|
|
# 主IP地理位置查询函数
|
|
get_ip_location() {
|
|
local ip="$1"
|
|
|
|
# 先尝试在线查询
|
|
if command -v curl &> /dev/null; then
|
|
local online_result=$(get_ip_location_online "$ip")
|
|
if [ -n "$online_result" ] && [ "$online_result" != "null" ]; then
|
|
echo "$online_result"
|
|
return 0
|
|
fi
|
|
fi
|
|
|
|
# 在线查询失败,使用离线数据库
|
|
local offline_result=$(get_ip_location_offline "$ip")
|
|
echo "$offline_result"
|
|
}
|
|
|
|
# 获取城市名称
|
|
get_city_name() {
|
|
local ip="$1"
|
|
local location=$(get_ip_location "$ip")
|
|
|
|
# 从位置信息中提取城市名
|
|
if [[ "$location" == *"-"* ]]; then
|
|
local city=$(echo "$location" | awk -F'-' '{print $NF}' | sed 's/(.*//')
|
|
if [ -n "$city" ] && [ "$city" != "未知位置" ] && [ "$city" != "未知地区" ]; then
|
|
echo "$city" | sed 's/[^a-zA-Z0-9\u4e00-\u9fff]/_/g'
|
|
else
|
|
echo "unknown"
|
|
fi
|
|
else
|
|
echo "unknown"
|
|
fi
|
|
}
|
|
|
|
# 获取客户端IP
|
|
get_client_ip() {
|
|
local ip="unknown"
|
|
|
|
if [ -n "$SSH_CLIENT" ]; then
|
|
ip=$(echo "$SSH_CLIENT" | awk '{print $1}')
|
|
elif [ -n "$SSH_CONNECTION" ]; then
|
|
ip=$(echo "$SSH_CONNECTION" | awk '{print $1}')
|
|
else
|
|
ip=$(who -m 2>/dev/null | awk '{print $5}' | sed 's/[()]//g' | head -1)
|
|
if [[ "$ip" == ":0" ]] || [[ "$ip" == ":1" ]] || [[ -z "$ip" ]]; then
|
|
ip="localhost"
|
|
fi
|
|
fi
|
|
|
|
echo "$ip"
|
|
}
|
|
|
|
# 生成唯一日志文件名
|
|
generate_log_filename() {
|
|
local client_ip=$(get_client_ip)
|
|
local city_name=$(get_city_name "$client_ip")
|
|
local log_date=$(date '+%Y%m%d_%H%M%S')
|
|
local counter=1
|
|
local base_name=""
|
|
|
|
if [ "$city_name" != "unknown" ]; then
|
|
base_name="monitor_${client_ip//./_}_${city_name}_${log_date}"
|
|
else
|
|
base_name="monitor_${client_ip//./_}_${log_date}"
|
|
fi
|
|
|
|
local log_file="$LOG_DIR/${base_name}.log"
|
|
|
|
while [ -f "$log_file" ]; do
|
|
log_file="$LOG_DIR/${base_name}_${counter}.log"
|
|
counter=$((counter + 1))
|
|
if [ $counter -gt 100 ]; then
|
|
break
|
|
fi
|
|
done
|
|
|
|
echo "$log_file"
|
|
}
|
|
|
|
# 智能日志轮转检查
|
|
check_log_rotation() {
|
|
local current_time=$(date +%s)
|
|
|
|
if [ $((current_time - LAST_ROTATION)) -ge $LOG_ROTATE_INTERVAL ]; then
|
|
log_rotation "time"
|
|
return 0
|
|
fi
|
|
|
|
if [ -f "$CURRENT_LOG" ]; then
|
|
local log_size=$(stat -c%s "$CURRENT_LOG" 2>/dev/null || echo 0)
|
|
if [ "$log_size" -gt 1048576 ]; then
|
|
log_rotation "size"
|
|
return 0
|
|
fi
|
|
fi
|
|
|
|
return 1
|
|
}
|
|
|
|
# 初始化日志系统
|
|
init_log_system() {
|
|
mkdir -p "$LOG_DIR"
|
|
|
|
CURRENT_LOG=$(generate_log_filename)
|
|
LATEST_LOG="$LOG_DIR/latest.log"
|
|
|
|
ln -sf "$CURRENT_LOG" "$LATEST_LOG" 2>/dev/null || true
|
|
|
|
LAST_ROTATION=$(date +%s)
|
|
|
|
log_message "INFO" "监控脚本启动 - PID: $$"
|
|
|
|
local client_ip=$(get_client_ip)
|
|
log_message "INFO" "客户端IP: $client_ip"
|
|
|
|
local location_info=$(get_ip_location "$client_ip")
|
|
log_message "INFO" "地理位置: $location_info"
|
|
log_message "INFO" "日志文件: $CURRENT_LOG"
|
|
log_message "INFO" "日志轮转: ${LOG_ROTATE_INTERVAL}秒或${MAX_LOG_SIZE}"
|
|
|
|
# 显示查询方式
|
|
if [[ "$location_info" == *"离线"* ]] || [[ "$location_info" == *"未知"* ]]; then
|
|
log_message "WARN" "使用离线IP数据库查询"
|
|
else
|
|
log_message "SUCCESS" "在线IP查询成功"
|
|
fi
|
|
}
|
|
|
|
# 日志轮转
|
|
log_rotation() {
|
|
local reason="$1"
|
|
log_message "INFO" "执行日志轮转 - 原因: $reason"
|
|
|
|
if [ -f "$CURRENT_LOG" ]; then
|
|
local log_size=$(du -h "$CURRENT_LOG" 2>/dev/null | cut -f1 || echo "未知")
|
|
log_message "INFO" "原日志文件大小: $log_size"
|
|
fi
|
|
|
|
init_log_system
|
|
log_message "INFO" "新日志文件已创建"
|
|
}
|
|
|
|
# 主监控函数
|
|
start_main_monitor() {
|
|
log_message "INFO" "启动主监控进程..."
|
|
|
|
declare -A file_sizes
|
|
declare -A last_commands
|
|
|
|
for user_dir in /home/* /root; do
|
|
if [ -d "$user_dir" ]; then
|
|
local user=$(basename "$user_dir")
|
|
local history_file="$user_dir/.bash_history"
|
|
if [ -f "$history_file" ]; then
|
|
file_sizes["$user"]=$(stat -c%s "$history_file" 2>/dev/null || echo 0)
|
|
last_commands["$user"]=""
|
|
else
|
|
file_sizes["$user"]=0
|
|
last_commands["$user"]=""
|
|
fi
|
|
fi
|
|
done
|
|
|
|
while true; do
|
|
for user_dir in /home/* /root; do
|
|
if [ -d "$user_dir" ]; then
|
|
local user=$(basename "$user_dir")
|
|
local history_file="$user_dir/.bash_history"
|
|
|
|
if [ -f "$history_file" ]; then
|
|
local current_size=$(stat -c%s "$history_file" 2>/dev/null || echo 0)
|
|
local last_size=${file_sizes["$user"]}
|
|
|
|
if [ "$current_size" -gt "$last_size" ]; then
|
|
local new_content=$(tail -c +$((last_size + 1)) "$history_file" 2>/dev/null | tr -d '\0')
|
|
|
|
if [ -n "$new_content" ]; then
|
|
while IFS= read -r line; do
|
|
line=$(echo "$line" | sed 's/^[ \t]*//;s/[ \t]*$//')
|
|
|
|
if [ -n "$line" ] && [ "${#line}" -gt 1 ] && \
|
|
[[ ! "$line" =~ ^(ls|cd|pwd|ll|la|history|exit|clear|echo|date|whoami)$ ]] && \
|
|
[[ ! "$line" =~ ^#[0-9]+$ ]]; then
|
|
|
|
local client_ip=$(get_client_ip)
|
|
local location_info=$(get_ip_location "$client_ip")
|
|
|
|
log_message "COMMAND" "用户: $user | 命令: $line | 来源: $client_ip [$location_info]"
|
|
last_commands["$user"]="$line"
|
|
fi
|
|
done <<< "$new_content"
|
|
fi
|
|
|
|
file_sizes["$user"]=$current_size
|
|
fi
|
|
fi
|
|
fi
|
|
done
|
|
sleep 1
|
|
done
|
|
}
|
|
|
|
# 后台运行监控
|
|
start_background_monitor() {
|
|
echo -e "${GREEN}启动后台监控服务...${NC}"
|
|
|
|
if [ -f "/tmp/command_monitor.pid" ]; then
|
|
local old_pid=$(cat "/tmp/command_monitor.pid" 2>/dev/null)
|
|
if ps -p "$old_pid" >/dev/null 2>&1; then
|
|
echo -e "${YELLOW}监控服务已经在运行 (PID: $old_pid)${NC}"
|
|
echo -e "停止现有服务: ${RED}kill $old_pid${NC}"
|
|
return 1
|
|
fi
|
|
fi
|
|
|
|
(
|
|
setsid >/dev/null 2>&1
|
|
DAEMON_MODE=true
|
|
init_log_system
|
|
configure_realtime_history
|
|
|
|
log_message "INFO" "后台监控进程启动 - PID: $$"
|
|
echo $$ > "/tmp/command_monitor.pid"
|
|
|
|
# 启动资源监控(简化版)
|
|
(
|
|
while true; do
|
|
sleep 60
|
|
check_log_rotation
|
|
done
|
|
) &
|
|
|
|
start_main_monitor
|
|
|
|
rm -f "/tmp/command_monitor.pid"
|
|
|
|
) >/dev/null 2>&1 &
|
|
|
|
local main_pid=$!
|
|
sleep 2
|
|
|
|
if ps -p $main_pid >/dev/null 2>&1; then
|
|
echo -e "${GREEN}✓ 后台监控已启动!${NC}"
|
|
echo -e "主进程PID: $main_pid"
|
|
echo -e "日志文件: $LATEST_LOG"
|
|
echo -e "查看日志: ${GREEN}tail -f $LATEST_LOG${NC}"
|
|
echo -e "停止监控: ${RED}kill $main_pid${NC}"
|
|
echo -e "状态检查: ${BLUE}$0 -s${NC}"
|
|
else
|
|
echo -e "${RED}✗ 后台监控启动失败${NC}"
|
|
return 1
|
|
fi
|
|
}
|
|
|
|
# 配置实时history
|
|
configure_realtime_history() {
|
|
log_message "INFO" "配置实时命令记录..."
|
|
|
|
for user_dir in /home/* /root; do
|
|
if [ -d "$user_dir" ]; then
|
|
local user=$(basename "$user_dir")
|
|
local bashrc="$user_dir/.bashrc"
|
|
|
|
if [ -f "$bashrc" ]; then
|
|
if ! grep -q "REAL_TIME_HISTORY" "$bashrc"; then
|
|
cat >> "$bashrc" << 'EOF'
|
|
|
|
# REAL_TIME_HISTORY - 实时命令记录配置
|
|
export PROMPT_COMMAND='history -a; history -c; history -r'
|
|
export HISTTIMEFORMAT='%F %T '
|
|
export HISTSIZE=10000
|
|
export HISTFILESIZE=20000
|
|
shopt -s histappend
|
|
export HISTCONTROL=ignoredups:erasedups
|
|
|
|
EOF
|
|
log_message "SUCCESS" "已为用户 $user 配置实时命令记录"
|
|
else
|
|
log_message "INFO" "用户 $user 已配置实时记录"
|
|
fi
|
|
fi
|
|
fi
|
|
done
|
|
}
|
|
|
|
# 显示使用说明
|
|
show_usage() {
|
|
echo -e "${GREEN}实时命令监控系统 v3.2${NC}"
|
|
echo "用法: $0 [选项]"
|
|
echo
|
|
echo "选项:"
|
|
echo " -d, --daemon 后台运行模式"
|
|
echo " -s, --status 查看监控状态"
|
|
echo " -k, --kill 停止监控进程"
|
|
echo " -h, --help 显示此帮助信息"
|
|
echo
|
|
echo "特性:"
|
|
echo " - 离线IP数据库支持"
|
|
echo " - 智能日志轮转"
|
|
echo " - 唯一日志文件名"
|
|
echo
|
|
echo "示例:"
|
|
echo " $0 -d # 后台运行监控"
|
|
echo " $0 -s # 查看监控状态"
|
|
}
|
|
|
|
# 查看监控状态
|
|
check_monitor_status() {
|
|
local pid_file="/tmp/command_monitor.pid"
|
|
local main_pid=""
|
|
|
|
if [ -f "$pid_file" ]; then
|
|
main_pid=$(cat "$pid_file" 2>/dev/null)
|
|
fi
|
|
|
|
if [ -z "$main_pid" ] || ! ps -p "$main_pid" >/dev/null 2>&1; then
|
|
echo -e "${RED}✗ 监控服务未运行${NC}"
|
|
return 1
|
|
fi
|
|
|
|
echo -e "${GREEN}✓ 监控服务运行中${NC}"
|
|
echo "主进程PID: $main_pid"
|
|
echo "日志目录: $LOG_DIR"
|
|
|
|
if [ -f "$LATEST_LOG" ]; then
|
|
local log_size=$(du -h "$LATEST_LOG" 2>/dev/null | cut -f1 || echo "未知")
|
|
echo "当前日志大小: $log_size"
|
|
echo
|
|
echo -e "${YELLOW}最近3条记录:${NC}"
|
|
tail -3 "$LATEST_LOG" 2>/dev/null | while read line; do
|
|
echo " $line"
|
|
done || echo " 无法读取日志"
|
|
fi
|
|
}
|
|
|
|
# 停止监控进程
|
|
stop_monitor() {
|
|
local pid_file="/tmp/command_monitor.pid"
|
|
local main_pid=""
|
|
|
|
if [ -f "$pid_file" ]; then
|
|
main_pid=$(cat "$pid_file")
|
|
rm -f "$pid_file"
|
|
fi
|
|
|
|
if [ -z "$main_pid" ]; then
|
|
main_pid=$(pgrep -f "command_monitor" | head -1)
|
|
fi
|
|
|
|
if [ -z "$main_pid" ]; then
|
|
echo -e "${YELLOW}没有找到运行的监控进程${NC}"
|
|
return 0
|
|
fi
|
|
|
|
echo -e "${YELLOW}正在停止监控进程 (PID: $main_pid)...${NC}"
|
|
kill "$main_pid" 2>/dev/null || true
|
|
sleep 2
|
|
|
|
if ps -p "$main_pid" >/dev/null 2>&1; then
|
|
echo -e "${RED}强制停止监控进程...${NC}"
|
|
kill -9 "$main_pid" 2>/dev/null || true
|
|
fi
|
|
|
|
pkill -f "monitor_resources" 2>/dev/null || true
|
|
rm -f "$pid_file" 2>/dev/null
|
|
|
|
echo -e "${GREEN}✓ 监控进程已停止${NC}"
|
|
}
|
|
|
|
### 主程序 ###
|
|
main() {
|
|
local command="${1:-}"
|
|
|
|
case "$command" in
|
|
-d|--daemon)
|
|
start_background_monitor
|
|
;;
|
|
-s|--status)
|
|
check_monitor_status
|
|
;;
|
|
-k|--kill)
|
|
stop_monitor
|
|
;;
|
|
-h|--help|"")
|
|
show_usage
|
|
;;
|
|
*)
|
|
echo -e "${RED}未知选项: $command${NC}"
|
|
show_usage
|
|
return 1
|
|
;;
|
|
esac
|
|
}
|
|
|
|
# 直接运行模式
|
|
if [ $# -eq 0 ]; then
|
|
echo -e "${YELLOW}前台运行模式 (Ctrl+C 停止)${NC}"
|
|
init_log_system
|
|
configure_realtime_history
|
|
start_main_monitor
|
|
else
|
|
main "$1"
|
|
fi
|