generate_nginx_logs.sh
#!/bin/bash
# 生成随机公网IP地址
generate_fake_public_ip() {
echo "$((RANDOM % 223 + 1)).$((RANDOM % 256)).$((RANDOM % 256)).$((RANDOM % 256))"
}
# 生成随机日期和时间
generate_random_datetime() {
start_timestamp=$(date -d "2022-01-01" +%s)
end_timestamp=$(date -d "2022-12-31" +%s)
random_timestamp=$((start_timestamp + RANDOM % (end_timestamp - start_timestamp)))
date -d "@$random_timestamp" +"%d/%b/%Y:%H:%M:%S %z"
}
# 生成随机HTTP请求方法
generate_random_http_method() {
methods=("GET" "POST" "PUT" "DELETE")
echo ${methods[$((RANDOM % ${#methods[@]}))]}
}
# 生成随机URL路径
generate_random_url_path() {
paths=("homepage" "about" "products" "services" "contact" "blog" "login" "signup" "dashboard" "profile")
weights=(20 10 10 10 5 5 3 3 2 2) # 路径对应的权重,数字越大权重越高
total_weight=0
for weight in "${weights[@]}"; do
total_weight=$((total_weight + weight))
done
rand=$((RANDOM % total_weight))
cumulative_weight=0
index=0
for i in "${!paths[@]}"; do
cumulative_weight=$((cumulative_weight + weights[i]))
if [ $rand -lt $cumulative_weight ]; then
index=$i
break
fi
done
echo "${paths[$index]}"
}
# 生成随机HTTP状态码
generate_random_status_code() {
status_codes=("200" "200" "200" "200" "200" "301" "301" "404" "500")
echo ${status_codes[$((RANDOM % ${#status_codes[@]}))]}
}
# 生成随机设备类型
generate_random_device_type() {
device_types=("Desktop" "Desktop" "Desktop" "Mobile" "Mobile" "Tablet" "Smart TV" "Game Console")
echo ${device_types[$((RANDOM % ${#device_types[@]}))]}
}
# 生成随机用户代理
generate_random_user_agent() {
device_type=$(generate_random_device_type)
case $device_type in
"Desktop")
user_agents=("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:85.0) Gecko/20100101 Firefox/85.0" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Firefox/85.0")
;;
"Mobile")
user_agents=("Mozilla/5.0 (iPhone; CPU iPhone OS 14_4_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0.3 Mobile/15E148 Safari/604.1" "Mozilla/5.0 (Linux; Android 11; SM-G960U) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.181 Mobile Safari/537.36" "Mozilla/5.0 (Linux; Android 11; SM-A515F) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.181 Safari/537.36")
;;
"Tablet")
user_agents=("Mozilla/5.0 (iPad; CPU OS 14_4_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0.3 Mobile/15E148 Safari/604.1" "Mozilla/5.0 (Linux; Android 11; SM-T500) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.181 Safari/537.36" "Mozilla/5.0 (Linux; Android 11; SM-P615) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.181 Safari/537.36")
;;
"Smart TV")
user_agents=("Mozilla/5.0 (SMART-TV; Linux; Tizen 6.0) AppleWebKit/537.36 (KHTML, like Gecko) SamsungBrowser/13.0 TV Safari/537.36" "Mozilla/5.0 (SMART-TV; Linux; WebKit) AppleWebKit/538.16 (KHTML, like Gecko) PhilipsTV/3.0 TV Safari/538.16" "Mozilla/5.0 (SMART-TV; Linux; Tizen 5.0) AppleWebKit/537.36 (KHTML, like Gecko) SamsungBrowser/12.0 TV Safari/537.36")
;;
"Game Console")
user_agents=("Mozilla/5.0 (PlayStation 5 8.00) AppleWebKit/605.1.15 (KHTML, like Gecko)" "Mozilla/5.0 (Nintendo Switch; Switch) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.1 NintendoBrowser/5.1.0.15139")
;;
*)
user_agents=("Unknown")
;;
esac
echo ${user_agents[$((RANDOM % ${#user_agents[@]}))]}
}
# 生成随机文件请求大小(字节)
generate_random_file_size() {
file_sizes=("1024" "1024" "1024" "2048" "2048" "3072" "4096")
echo ${file_sizes[$((RANDOM % ${#file_sizes[@]}))]}
}
# 生成一行访问日志
generate_log_entry() {
ip=$(generate_fake_public_ip)
datetime=$(generate_random_datetime)
method=$(generate_random_http_method)
path=$(generate_random_url_path)
status=$(generate_random_status_code)
user_agent=$(generate_random_user_agent)
file_size=$(generate_random_file_size)
echo "$ip - - [$datetime] \"$method $path HTTP/1.0\" $status $file_size \"$user_agent\""
}
# 从命令行参数获取生成的数量
if [[ $# -eq 1 ]]; then
num_logs=$1
else
echo "请提供生成的数量参数"
exit 1
fi
# 生成访问日志
for ((i=0; i<num_logs; i++)); do
log_entry=$(generate_log_entry)
echo $log_entry >> nginx.log
done
欢迎来撩 : 汇总all