Skip to content

Commit c0f9ef5

Browse files
author
huhuhang
committed
优化 Nmap 实践环境设置脚本,增强服务启动信息并更新注释
1 parent b0112bd commit c0f9ef5

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed

setup-redis-logging/setup.sh

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#!/bin/bash
2+
3+
# Script must be run with root privileges
4+
if [ "$EUID" -ne 0 ]; then
5+
echo "Please run this script with root privileges: sudo $0"
6+
exit 1
7+
fi
8+
9+
# Define variables
10+
LOG_DIR="/var/log/redis"
11+
LOG_FILE="$LOG_DIR/redis_command_history.log"
12+
TIMESTAMP=$(date '+%Y-%m-%d %H:%M:%S')
13+
14+
# Step 1: Check if Redis is installed
15+
if ! command -v redis-server &> /dev/null; then
16+
echo "Redis is not installed, installing now..."
17+
apt-get update
18+
apt-get install -y redis-server
19+
if [ $? -ne 0 ]; then
20+
echo "Redis installation failed, please check network or package manager configuration."
21+
exit 1
22+
fi
23+
else
24+
echo "Redis is already installed, skipping installation."
25+
fi
26+
27+
# Step 2: Replace redis-cli with a logging wrapper script
28+
echo "Replacing redis-cli with a logging-enabled wrapper script..."
29+
if [ -f "/usr/bin/redis-cli" ]; then
30+
mv /usr/bin/redis-cli /usr/bin/redis-cli-original
31+
fi
32+
33+
# Create the wrapper script for redis-cli
34+
cat << 'EOF' > /usr/bin/redis-cli
35+
#!/bin/bash
36+
LOG_DIR="/var/log/redis"
37+
LOG_FILE="$LOG_DIR/redis_command_history.log"
38+
TIMESTAMP=$(date '+%Y-%m-%d %H:%M:%S')
39+
if [ ! -d "$LOG_DIR" ]; then
40+
mkdir -p "$LOG_DIR"
41+
touch "$LOG_FILE"
42+
chown labex:labex "$LOG_FILE" # Owner: labex, Group: labex
43+
chmod 666 "$LOG_FILE" # rw-rw-rw- permissions for all users
44+
echo "[$TIMESTAMP] Redis command history log created." >> "$LOG_FILE"
45+
fi
46+
if [ -t 0 ]; then
47+
TEMP_FILE="/tmp/redis_session_$$.log"
48+
/usr/bin/script -q -c "/usr/bin/redis-cli-original" "$TEMP_FILE"
49+
grep -v '^OK\|^"\|^([integer]' "$TEMP_FILE" | grep -v '^$' | while read -r line; do
50+
echo "[$TIMESTAMP] $line" >> "$LOG_FILE"
51+
done
52+
rm -f "$TEMP_FILE"
53+
else
54+
echo "[$TIMESTAMP] $@" >> "$LOG_FILE"
55+
/usr/bin/redis-cli-original "$@"
56+
fi
57+
EOF
58+
59+
chmod +x /usr/bin/redis-cli
60+
echo "redis-cli wrapper script created successfully."
61+
62+
# Step 3: Create log file and set permissions
63+
echo "Creating log file and setting broad read/write permissions..."
64+
if [ ! -d "$LOG_DIR" ]; then
65+
mkdir -p "$LOG_DIR"
66+
fi
67+
touch "$LOG_FILE"
68+
# Set ownership to labex user and group
69+
chown labex:labex "$LOG_FILE"
70+
# Set permissions to 666 (read/write for all)
71+
chmod 666 "$LOG_FILE"
72+
echo "[$TIMESTAMP] Redis command history log created by setup script." >> "$LOG_FILE"
73+
74+
# Ensure the log directory is accessible
75+
chmod 755 "$LOG_DIR"
76+
77+
echo "Configuration completed!"
78+
echo "Users mái now use redis-cli normally, and all commands will be logged to $LOG_FILE."
79+
echo "The labex user and all other users have read/write access to the log file."

0 commit comments

Comments
 (0)