服务器运维Linux
2025-04-03 11:37阅读:135评论:0
Linux VPS一键屏蔽指定国家所有的IP访问
说明:对于屏蔽指定国家所有的IP的手动教程已经讲了,查看:Linux VPS使用ipset快速屏蔽指定国家的IP访问,虽然步骤很简单,但为了更白的小白,博主写了个一键脚本,这里分享下。 提示:据一些同学需求,博主也发了个白名单教程,查看:使用ipset设置防火墙端口白名单,只让指定国家访问。 使用
说明:对于屏蔽指定国家所有的IP的手动教程已经讲了,查看:Linux VPS使用ipset快速屏蔽指定国家的IP访问,虽然步骤很简单,但为了更白的小白,博主写了个一键脚本,这里分享下。
- 提示:据一些同学需求,博主也发了个白名单教程,查看:__PROTECTED_2__。
- 本脚本适用于CentOS、Debian、Ubuntu等常用系统。
- 使用root运行以下命令:
1
2
3
4
wget https://www.moerats.com/usr/shell/block-ips.shchmod +x block-ips.sh./block-ips.sh - 封禁ip时会要求你输入国家代码,代码查看:点击进入。记住所填参数均为小写字母。比如JAPAN (JP),我们就输入jp这个参数。
shell
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#! /bin/bash#Block-IPs-from-countries#Github:https://github.com/iiiiiii1/Block-IPs-from-countries#Blog:https://www.moerats.com/ Green="\033[32m"Font="\033[0m" #root权限root_need(){ if [[ $EUID -ne 0 ]]; then echo "Error:This script must be run as root!" 1>&2 exit 1 fi} #封禁ipblock_ipset(){check_ipset#添加ipset规则echo -e "${Green}请输入需要封禁的国家代码,如cn(中国),注意字母为小写!${Font}"read -p "请输入国家代码:" GEOIPecho -e "${Green}正在下载IPs data...${Font}"#wget -P /tmp http://www.ipdeny.com/ipblocks/data/countries/$GEOIP.zone 2> /dev/null#检查下载是否成功 if [ -f "/tmp/"$GEOIP".zone" ]; then echo -e "${Green}IPs data下载成功!${Font}" else echo -e "${Green}下载失败,请检查你的输入!${Font}" echo -e "${Green}代码查看地址:http://www.ipdeny.com/ipblocks/data/countries/${Font}" exit 1 fi#创建规则ipset -N $GEOIP hash:netfor i in $(cat /tmp/$GEOIP.zone ); do ipset -A $GEOIP $i; donerm -f /tmp/$GEOIP.zoneecho -e "${Green}规则添加成功,即将开始封禁ip!${Font}"#开始封禁iptables -I INPUT -p tcp -m set --match-set "$GEOIP" src -j DROPiptables -I INPUT -p udp -m set --match-set "$GEOIP" src -j DROPecho -e "${Green}所指定国家($GEOIP)的ip封禁成功!${Font}"} #解封ipunblock_ipset(){echo -e "${Green}请输入需要解封的国家代码,如cn(中国),注意字母为小写!${Font}"read -p "请输入国家代码:" GEOIP#判断是否有此国家的规则lookuplist=`ipset list | grep "Name:" | grep "$GEOIP"` if [ -n "$lookuplist" ]; then iptables -D INPUT -p tcp -m set --match-set "$GEOIP" src -j DROP iptables -D INPUT -p udp -m set --match-set "$GEOIP" src -j DROP ipset destroy $GEOIP echo -e "${Green}所指定国家($GEOIP)的ip解封成功,并删除其对应的规则!${Font}" else echo -e "${Green}解封失败,请确认你所输入的国家是否在封禁列表内!${Font}" exit 1 fi} #查看封禁列表block_list(){ iptables -L | grep match-set} #检查系统版本check_release(){ if [ -f /etc/redhat-release ]; then release="centos" elif cat /etc/issue | grep -Eqi "debian"; then release="debian" elif cat /etc/issue | grep -Eqi "ubuntu"; then release="ubuntu" elif cat /etc/issue | grep -Eqi "centos|red hat|redhat"; then release="centos" elif cat /proc/version | grep -Eqi "debian"; then release="debian" elif cat /proc/version | grep -Eqi "ubuntu"; then release="ubuntu" elif cat /proc/version | grep -Eqi "centos|red hat|redhat"; then release="centos" fi} #检查ipset是否安装check_ipset(){ if [ -f /sbin/ipset ]; then echo -e "${Green}检测到ipset已存在,并跳过安装步骤!${Font}" elif [ "${release}" == "centos" ]; then yum -y install ipset else apt-get -y install ipset fi} #开始菜单main(){root_needcheck_releaseclearecho -e "———————————————————————————————————————"echo -e "${Green}Linux VPS一键屏蔽指定国家所有的IP访问${Font}"echo -e "${Green}1、封禁ip${Font}"echo -e "${Green}2、解封iP${Font}"echo -e "${Green}3、查看封禁列表${Font}"echo -e "———————————————————————————————————————"read -p "请输入数字 [1-3]:" numcase "$num" in 1) block_ipset ;; 2) unblock_ipset ;; 3) block_list ;; *) clear echo -e "${Green}请输入正确数字 [1-3]${Font}" sleep 2s main ;; esac}main 转载:https://www.moerats.com/archives/585/