Shell脚本实现防止国外ip访问服务器_linux shell_脚本之家
这篇文章主要介绍了Shell脚本实现防止国外ip访问服务器,本文通过分析使用IP地址库,然后把国外IP指导入iptables并禁止访问实现,需要的朋友可以参考下
客户的要求,不想让外国的ip来访问服务器,本来要让机房在上端路由解决,但是那些人不鸟你!!!~~~~
这样的话,只能自己用iptables解决了~~~~~~~~~
关于 ip库 大家可以去 http://www.ipdeny.com/ipblocks/ 下载, 这里有各个国家的~~~
复制代码 代码如下:
#下载ip库
#wget http://rfyiamcool.googlecode.com/files/allip.txt
wget http://www.ipdeny.com/ipblocks/data/countries/cn.zone
#把分号去掉
sed -i 's/;$//' allip.txt
#把ip端调出来
for i in `cat cn.zone`
do
echo "iptables -A INPUT -s $i -j ACCEPT" >>iptables.sh
done
cat iptables.sh
#wget http://rfyiamcool.googlecode.com/files/allip.txt
wget http://www.ipdeny.com/ipblocks/data/countries/cn.zone
#把分号去掉
sed -i 's/;$//' allip.txt
#把ip端调出来
for i in `cat cn.zone`
do
echo "iptables -A INPUT -s $i -j ACCEPT" >>iptables.sh
done
cat iptables.sh
把这个脚本做下优化,在iptables.sh前面加上下面的命令!
#!/bin/bash
ziji="222.173.13.5"
iptables -F
iptables -X
iptables -Z
iptables -t nat -F
iptables -t mangle -F
modprobe iptable_nat
modprobe ip_nat_ftp
modprobe ip_nat_irc
modprobe ip_conntrack
modprobe ip_conntrack_ftp
modprobe ip_conntrack_irc
modprobe ipt_limit
modprobe ipt_recent ip_list_tot=16384
modprobe ip_conntrack hashsize=16384
echo "1024 63000" > /proc/sys/net/ipv4/ip_local_port_range
echo "1" > /proc/sys/net/ipv4/tcp_tw_recycle
echo "1" > /proc/sys/net/ipv4/tcp_tw_reuse
echo "1" > /proc/sys/net/ipv4/icmp_echo_ignore_broadcasts
echo "1" >/proc/sys/net/ipv4/conf/all/log_martians
echo "1" >/proc/sys/net/ipv4/tcp_syncookies
echo "2" >/proc/sys/net/ipv4/tcp_synack_retries
echo "5000" >/proc/sys/net/ipv4/tcp_max_syn_backlog
echo "3" >/proc/sys/net/ipv4/tcp_syn_retries
echo "1" > /proc/sys/net/ipv4/tcp_tw_reuse
echo "1" > /proc/sys/net/ipv4/tcp_tw_recycle
echo "30" > /proc/sys/net/ipv4/tcp_fin_timeout
echo "1800" > /proc/sys/net/ipv4/tcp_keepalive_time
echo "1" > /proc/sys/net/ipv4/ip_forward
iptables -P INPUT DROP
iptables -P FORWARD ACCEPT
iptables -P OUTPUT ACCEPT
/sbin/iptables -t nat -P PREROUTING ACCEPT
/sbin/iptables -t nat -P POSTROUTING ACCEPT
iptables -A INPUT -s $ziji -j ACCEPT
iptables -A INPUT -i lo -j ACCEPT
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A INPUT -p tcp ! --syn -m state --state NEW -j DROP
ziji="222.173.13.5"
iptables -F
iptables -X
iptables -Z
iptables -t nat -F
iptables -t mangle -F
modprobe iptable_nat
modprobe ip_nat_ftp
modprobe ip_nat_irc
modprobe ip_conntrack
modprobe ip_conntrack_ftp
modprobe ip_conntrack_irc
modprobe ipt_limit
modprobe ipt_recent ip_list_tot=16384
modprobe ip_conntrack hashsize=16384
echo "1024 63000" > /proc/sys/net/ipv4/ip_local_port_range
echo "1" > /proc/sys/net/ipv4/tcp_tw_recycle
echo "1" > /proc/sys/net/ipv4/tcp_tw_reuse
echo "1" > /proc/sys/net/ipv4/icmp_echo_ignore_broadcasts
echo "1" >/proc/sys/net/ipv4/conf/all/log_martians
echo "1" >/proc/sys/net/ipv4/tcp_syncookies
echo "2" >/proc/sys/net/ipv4/tcp_synack_retries
echo "5000" >/proc/sys/net/ipv4/tcp_max_syn_backlog
echo "3" >/proc/sys/net/ipv4/tcp_syn_retries
echo "1" > /proc/sys/net/ipv4/tcp_tw_reuse
echo "1" > /proc/sys/net/ipv4/tcp_tw_recycle
echo "30" > /proc/sys/net/ipv4/tcp_fin_timeout
echo "1800" > /proc/sys/net/ipv4/tcp_keepalive_time
echo "1" > /proc/sys/net/ipv4/ip_forward
iptables -P INPUT DROP
iptables -P FORWARD ACCEPT
iptables -P OUTPUT ACCEPT
/sbin/iptables -t nat -P PREROUTING ACCEPT
/sbin/iptables -t nat -P POSTROUTING ACCEPT
iptables -A INPUT -s $ziji -j ACCEPT
iptables -A INPUT -i lo -j ACCEPT
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A INPUT -p tcp ! --syn -m state --state NEW -j DROP
下面是效果图~~~~~~~~~~~~~~~~(刚才把主机关机了,大家国内也是ping不了的 O(∩_∩)O~)