#!/bin/bash # 检查参数数量是否正确 if [ $# -ne 1 ]; then echo "用法: $0 <IP地址>" exit 1 fi ip="$1" # 验证IP地址格式(严格模式) if [[ ! $ip =~ ^((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])$ ]]; then echo "错误:无效的IPv4地址格式" exit 1 fi # 检查root权限 if [ "$(id -u)" != "0" ]; then echo "错误:此脚本需要root权限执行,请使用sudo运行" exit 1 fi # 检查fail2ban-client是否存在 if ! command -v fail2ban-client &> /dev/null; then echo "错误:fail2ban-client未找到,请先安装fail2ban" exit 1 fi # 获取所有jail列表 jails=$(fail2ban-client status | awk -F':' '/Jail list:/ {gsub(/,/, " ", $2); print $2}' | tr -s ' ') # 解封IP地址 unban_success=0 for jail in $jails; do if fail2ban-client set "$jail" unbanip "$ip" &> /dev/null; then echo "[成功] 已从 $jail 解封IP: $ip" unban_success=1 fi done # 结果反馈 if [ $unban_success -eq 1 ]; then echo "解封操作已完成" else echo "警告:未找到任何有效的jail配置,请检查fail2ban状态" exit 1 fi
将以上脚本另存为
unlockip.sh
sudo bash unlockip.sh 1.1.1.1