踢出脚本
[root@ubuntu24-13:~]
login_ip=$(who | grep -m1 "$USER" | awk '{print $5}' | tr -d '()')
[ -z "$login_ip" ] && exit 0
geo=$(curl -s --connect-timeout 2 "http://ip-api.com/json/${login_ip}?lang=zh-CN")
country=$(echo "$geo" | grep '"country"' | cut -d '"' -f4)
region=$(echo "$geo" | grep '"regionName"' | cut -d '"' -f4)
city=$(echo "$geo" | grep '"city"' | cut -d '"' -f4)
ALLOW_REGIONS=("Beijing" "Shanghai" "Guangdong")
ALLOW_CITIES=("Beijing" "Shanghai" "Guangzhou" "Shenzhen")
is_allowed_region=false
is_allowed_city=false
for r in "${ALLOW_REGIONS[@]}"; do
if [[ "$region" == "$r" ]]; then
is_allowed_region=true
break
fi
done
for c in "${ALLOW_CITIES[@]}"; do
if [[ "$city" == "$c" ]]; then
is_allowed_city=true
break
fi
done
if [[ "$is_allowed_region" != "true" || "$is_allowed_city" != "true" ]]; then
echo "你的IP ${login_ip} 来自 ${country}-${region}-${city},不在允许区域,连接已被拒绝!"
sleep 1
pkill -KILL -t $(who am i | awk '{print $2}')
fi