Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,18 +85,18 @@
请使用 `root` 或具有 `sudo` 权限的用户执行:

```bash
/bin/bash <(wget -qO - [https://raw.githubusercontent.com/SuperNG6/linux-setup.sh/main/server-setup.sh](https://raw.githubusercontent.com/SuperNG6/linux-setup.sh/main/server-setup.sh))
````
/bin/bash <(wget -qO - https://raw.githubusercontent.com/SuperNG6/linux-setup.sh/main/server-setup.sh)
```

**备用链接与国内加速:**

```bash
# 短链接
/bin/bash <(wget -qO - bit.ly/ls-set)
/bin/bash <(wget -qO - [https://tinyurl.com/server-setup](https://tinyurl.com/server-setup))
/bin/bash <(wget -qO - https://tinyurl.com/server-setup)

# 国内服务器专用加速
/bin/bash <(wget -qO - [https://mirror.ghproxy.com/https://raw.githubusercontent.com/SuperNG6/linux-setup.sh/main/server-setup.sh](https://mirror.ghproxy.com/https://raw.githubusercontent.com/SuperNG6/linux-setup.sh/main/server-setup.sh))
/bin/bash <(wget -qO - https://mirror.ghproxy.com/https://raw.githubusercontent.com/SuperNG6/linux-setup.sh/main/server-setup.sh)
```

启动后,根据菜单提示输入数字选择相应功能即可。
Expand Down
21 changes: 19 additions & 2 deletions add_docker_tools.sh
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,28 @@ alias dc="bash ${tools_dir}/dc.sh"
alias dcs="bash ${tools_dir}/dcstats.sh"
alias dcps="bash ${tools_dir}/dcps.sh"
alias dcip="bash ${tools_dir}/dcip.sh"
alias dlogs="bash ${tools_dir}/dlogs.sh"
alias dclogs="bash ${tools_dir}/dclogs.sh"
alias dr="bash ${tools_dir}/drestart.sh"
alias dcr="bash ${tools_dir}/dcrestart.sh"
alias dexec="bash ${tools_dir}/dexec.sh"

unalias dlogs dexec 2>/dev/null

dlogs() {
bash "${tools_dir}/dlogs.sh" "\$@"
}

dexec() {
bash "${tools_dir}/dexec.sh" "\$@"
}

_docker_tools_container_names() {
local current="\${COMP_WORDS[COMP_CWORD]}"
local containers
containers=\$(docker ps --format '{{.Names}}' 2>/dev/null)
mapfile -t COMPREPLY < <(compgen -W "\$containers" -- "\$current")
}

complete -F _docker_tools_container_names dlogs dexec
EOF

# --- 使用循环下载其余的脚本,并进行错误检查 ---
Expand Down
26 changes: 16 additions & 10 deletions configure_fail2ban.sh
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ check_fail2ban_available() {

# 检测并修复日志配置
setup_logging() {
local os_type=$(get_os_info)
local os_type
os_type=$(get_os_info)

echo "🔍 正在检查系统日志配置..."

Expand Down Expand Up @@ -118,8 +119,9 @@ setup_logging() {

# 检测 SSH 日志文件路径
detect_ssh_log_path() {
local os_type=$(get_os_info)
local os_type
local possible_logs=()
os_type=$(get_os_info)

case $os_type in
"Debian/Ubuntu")
Expand All @@ -146,7 +148,8 @@ detect_ssh_log_path() {

# 安装 Fail2ban
install_fail2ban() {
local os_type=$(get_os_info)
local os_type
os_type=$(get_os_info)

echo "正在检查 Fail2ban 是否已安装..."

Expand Down Expand Up @@ -244,16 +247,19 @@ generate_fail2ban_config() {
local ban_time_hours=$3

local ban_time_seconds=$((ban_time_hours * 3600))
local jail_local="/etc/fail2ban/jail.local"
local jail_local="/etc/fail2ban/jail.d/sshd-linux-setup.local"
local custom_comment="# SSH protection configured by fail2ban.sh script"
local log_path=$(detect_ssh_log_path)
local os_type=$(get_os_info)
local log_path
log_path=$(detect_ssh_log_path)
Comment on lines 249 to +253

echo "🔧 正在生成 Fail2ban 配置..."

# 备份现有配置(如果存在)
mkdir -p /etc/fail2ban/jail.d

# 只备份脚本管理的配置(如果存在),避免覆盖用户自己的 jail.local。
if [ -f "$jail_local" ]; then
local backup_file="${jail_local}.bak.$(date +%Y%m%d_%H%M%S)"
local backup_file
backup_file="${jail_local}.bak.$(date +%Y%m%d_%H%M%S)"
echo "📦 备份现有配置到: $backup_file"
cp "$jail_local" "$backup_file"
fi
Expand Down Expand Up @@ -399,7 +405,7 @@ verify_configuration() {
echo " • 请确保您的 IP 地址不会被误封"
echo " • 可以使用 'fail2ban-client status sshd' 查看状态"
echo " • 可以使用 'fail2ban-client unban IP地址' 解封特定IP"
echo " • 配置文件位置: /etc/fail2ban/jail.local"
echo " • 配置文件位置: /etc/fail2ban/jail.d/sshd-linux-setup.local"
echo " • 查看日志: journalctl -u fail2ban -f"
}

Expand Down Expand Up @@ -988,4 +994,4 @@ if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
main_loop
;;
esac
fi
fi
9 changes: 3 additions & 6 deletions docker_tools/dc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,10 @@ check_docker_compose_version
# 文件夹选择
select_docker_compose_dir

# 构建命令,将所有传入的参数添加到$compose_cmd后
full_command="$compose_cmd ${@}"

# 执行构建的命令
eval $full_command
# 执行 docker compose 命令,保留参数边界,避免 eval 注入风险。
run_docker_compose "$@"

# 检查命令是否执行成功
if [ $? -ne 0 ]; then
exit 1
fi
fi
1 change: 1 addition & 0 deletions docker_tools/dclogs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ check_docker_compose_version
select_docker_compose_dir

# 显示docker-compose信息
# shellcheck disable=SC2154 # selected_folder/compose_cmd 由 docker_utils.sh 设置
echo "正在检查 $selected_folder 的 $compose_cmd 容器信息..."
$compose_cmd logs -f -n 10

Expand Down
1 change: 1 addition & 0 deletions docker_tools/dcps.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ check_docker_compose_version
select_docker_compose_dir

# 显示docker-compose信息
# shellcheck disable=SC2154 # selected_folder/compose_cmd 由 docker_utils.sh 设置
echo "正在检查 $selected_folder 的 $compose_cmd 容器信息..."
$compose_cmd ps

Expand Down
3 changes: 2 additions & 1 deletion docker_tools/dcrestart.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ while getopts "rR" opt; do
;;
\? )
echo "用法: $0 [-r] [-R]"
# shellcheck disable=SC2154 # compose_cmd 由 docker_utils.sh 设置
echo " -r: 快速重启 (默认, 使用 $compose_cmd restart)"
echo " -R: 完全重建 (使用 $compose_cmd down 和 up)"
exit 1
Expand All @@ -46,4 +47,4 @@ if [ $? -eq 0 ]; then
else
echo "重启容器时出错。"
exit 1
fi
fi
1 change: 1 addition & 0 deletions docker_tools/dcstats.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ check_docker_compose_version
select_docker_compose_dir

# 显示$compose_cmd状态
# shellcheck disable=SC2154 # selected_folder/compose_cmd 由 docker_utils.sh 设置
echo "正在检查 $selected_folder 的 $compose_cmd 容器状态..."
$compose_cmd stats

Expand Down
6 changes: 3 additions & 3 deletions docker_tools/dexec.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd)"
# 引用docker_utils脚本
source "${SCRIPT_DIR}/docker_utils.sh"

# 选择容器并获取容器名
container=$(select_container "请选择要进入的容器:")
# 选择容器并获取容器名;支持命令行参数传入编号或容器名。
container=$(container_arg_or_select "$1" "请选择要进入的容器:") || exit 1

echo "正在尝试进入容器 ${container}..."

Expand All @@ -31,4 +31,4 @@ fi
# 检查上一个 exec 命令的退出状态码
if [ $? -ne 0 ]; then
echo "已退出容器 ${container}."
fi
fi
4 changes: 2 additions & 2 deletions docker_tools/dlogs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd)"
# 引用docker_utils脚本
source "${SCRIPT_DIR}/docker_utils.sh"

# 选择容器
container=$(select_container "请选择要查看日志的容器:")
# 选择容器;支持命令行参数传入编号或容器名。
container=$(container_arg_or_select "$1" "请选择要查看日志的容器:") || exit 1

# 执行docker命令查看日志
docker logs -f -n10 "${container}"
21 changes: 19 additions & 2 deletions docker_tools/docker_aliases.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,25 @@ alias dc="bash /root/.docker_tools/dc.sh"
alias dcs="bash /root/.docker_tools/dcstats.sh"
alias dcps="bash /root/.docker_tools/dcps.sh"
alias dcip="bash /root/.docker_tools/dcip.sh"
alias dlogs="bash /root/.docker_tools/dlogs.sh"
alias dclogs="bash /root/.docker_tools/dclogs.sh"
alias dr="bash /root/.docker_tools/drestart.sh"
alias dcr="bash /root/.docker_tools/dcrestart.sh"
alias dexec="bash /root/.docker_tools/dexec.sh"

unalias dlogs dexec 2>/dev/null

dlogs() {
bash "/root/.docker_tools/dlogs.sh" "$@"
}

dexec() {
bash "/root/.docker_tools/dexec.sh" "$@"
}

_docker_tools_container_names() {
local current="${COMP_WORDS[COMP_CWORD]}"
local containers
containers=$(docker ps --format '{{.Names}}' 2>/dev/null)
mapfile -t COMPREPLY < <(compgen -W "$containers" -- "$current")
}

complete -F _docker_tools_container_names dlogs dexec
71 changes: 67 additions & 4 deletions docker_tools/docker_utils.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ check_docker_compose_version() {
fi
}

run_docker_compose() {
if [ "$compose_cmd" = "docker compose" ]; then
docker compose "$@"
else
docker-compose "$@"
Comment on lines +17 to +21
fi
}

# 函数:选择Docker Compose目录
select_docker_compose_dir() {
# 检查当前目录是否包含docker-compose.yml或docker-compose.yaml
Expand Down Expand Up @@ -68,7 +76,8 @@ select_docker_compose_dir() {
select_container() {
local prompt="$1"
local selected_container=""
local containers=($(docker ps --format "{{.Names}}"))
local containers=()
mapfile -t containers < <(docker ps --format "{{.Names}}")

if [ ${#containers[@]} -eq 0 ]; then
echo "没有正在运行的Docker容器。" >&2
Expand All @@ -95,10 +104,18 @@ select_container() {
else
echo "输入的数字无效,请输入 1 到 ${#containers[@]} 之间的数字。" >&2
fi
elif [[ " ${containers[@]} " =~ " ${input} " ]]; then
selected_container="$input"
break
else
for container in "${containers[@]}"; do
if [ "$container" = "$input" ]; then
selected_container="$input"
break
fi
done

if [ -n "$selected_container" ]; then
break
fi

echo "无效的输入。请从下面的列表中选择。" >&2
echo "可用的容器:" >&2
for i in "${!containers[@]}"; do
Expand All @@ -110,3 +127,49 @@ select_container() {

echo "$selected_container"
}

resolve_container() {
local input="$1"
local containers=()
mapfile -t containers < <(docker ps --format "{{.Names}}")

if [ ${#containers[@]} -eq 0 ]; then
echo "没有正在运行的Docker容器。" >&2
return 1
fi

if [[ "$input" =~ ^[0-9]+$ ]]; then
if ((input > 0 && input <= ${#containers[@]})); then
echo "${containers[$((input-1))]}"
return 0
else
echo "输入的数字无效,请输入 1 到 ${#containers[@]} 之间的数字。" >&2
return 1
fi
fi

for container in "${containers[@]}"; do
if [ "$container" = "$input" ]; then
echo "$container"
return 0
fi
done

echo "未找到正在运行的容器: $input" >&2
echo "可用的容器:" >&2
for i in "${!containers[@]}"; do
echo "$((i + 1)). ${containers[$i]}" >&2
done
return 1
}

container_arg_or_select() {
local input="$1"
local prompt="$2"

if [ -n "$input" ]; then
resolve_container "$input"
else
select_container "$prompt"
fi
}
2 changes: 1 addition & 1 deletion manage_xanmod_kernel.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ install_xanmod() {
debug_echo() {
if [[ "$DEBUG" == "true" ]]; then
# 使用 >&2 将调试信息输出到标准错误,不影响管道中的正常输出
echo "DEBUG: $@" >&2
echo "DEBUG: $*" >&2
fi
}
# --- 调试模式设置结束 ---
Expand Down
Loading