linux中使用shell脚本配置iptables防火墙

作者:袖梨 2022-11-14

要放假了,今天实在是闲得蛋疼,本来只想写个配置iptables的脚本的,没想到被自己搞得这么复杂了,还是分享出来给大家,有兴趣的可以自己再改改.
ps:本脚本只支持centos5和centos6,不支持centos 7,并且在脚本里也限制了.
脚本内容:

#!/bin/sh
# By rocdk890
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
export PATH

#Begin
start(){
sshport=`netstat -ntlp|grep sshd |awk -F: '{if($4!="")print $4}'`

# Require root to run this script.
echo " check user......"
if [[ $(whoami) != root ]];then
echo "Please run this script as root !"
exit 1
fi
echo " check lsb_release....."
chlsb=`rpm -qa|grep redhat-lsb|wc -l`
if [ $chlsb -ge 1 ];then
echo " has installed redhat-lsb."
else
echo " no found. install redhat-lsb......"
yum -y install redhat-lsb
fi
#===============================================================================
#this script is only for CentOS 5 and CentOS 6
#check the OS
#===============================================================================
version=`lsb_release -r |awk '{print substr($2,1,1)}'`
if [ $version != 5 ] || [ $version != 6 ];then
echo "the platform is ok !"
else
echo "this script is only for CentOS 5 or CentOS 6 !"
exit 1
fi

#iptables setting
echo "Iptables Setting..."
/sbin/iptables -P INPUT ACCEPT
#清空所有规则
/sbin/iptables -F
/sbin/iptables -X
/sbin/iptables -Z
/sbin/iptables -F -t nat
/sbin/iptables -X -t nat
/sbin/iptables -Z -t nat
/sbin/iptables -X -t mangle
#允许来自于lo接口的数据包,通过127.0.0.1访问本地服务
/sbin/iptables -A INPUT -i lo -j ACCEPT
#允许dns和ntp的数据通过
/sbin/iptables -A INPUT -p udp -m udp --sport 53 -j ACCEPT
/sbin/iptables -A INPUT -p udp -m udp --sport 123 -j ACCEPT
#限制对内部封包的发送速度
/sbin/iptables -A INPUT -f -m limit --limit 100/sec --limit-burst 100 -j ACCEPT
/sbin/iptables -A INPUT -p tcp -m tcp --tcp-flags SYN,RST,ACK SYN -m limit --limit 20/sec --limit-burst 200 -j ACCEPT
#/sbin/iptables -A INPUT -p icmp -m limit --limit 12/min --limit-burst 5 -j DROP
#限制建立联机的转发速度
/sbin/iptables -A FORWARD -f -m limit --limit 100/sec --limit-burst 100 -j ACCEPT
/sbin/iptables -A FORWARD -p tcp -m tcp --tcp-flags SYN,RST,ACK SYN -m limit --limit 20/sec --limit-burst 200 -j ACCEPT
#开放ssh端口
/sbin/iptables -A INPUT -p tcp --dport $sshport -j ACCEPT
#开放web服务默认端口80
/sbin/iptables -A INPUT -p tcp --dport 80 -j ACCEPT
#开放https服务默认端口443
#/sbin/iptables -A INPUT -p tcp --dport 443 -j ACCEPT
#开放指定ip,指定端口
#/sbin/iptables -A INPUT -p tcp -s 100.240.143.33 --dport 8080 -j ACCEPT
#允许ping
/sbin/iptables -A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT
#允许TCP建立连接,接受成功状态握手
/sbin/iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
/sbin/iptables -A INPUT -m state --state INVALID,NEW -j DROP
#屏蔽规则外请求
/sbin/iptables -P INPUT DROP
/sbin/iptables -P FORWARD DROP

BADIPS="`curl -s http://feeds.dshield.org/block.txt | awk '/^[1-9]/ {print $1 "/" $3}'`"
if [ "$BADIPS" ];then
for ip in $BADIPS
do
iptables -I INPUT -s $ip -j DROP
done
fi
}

stop(){
echo "Cleaning your Iptables:..."
iptables -F
iptables -X
iptables -Z
iptables -P INPUT ACCEPT
iptables -P OUTPUT ACCEPT
/etc/init.d/iptables stop
if [ "$?" == "0" ];then
echo "Done!"
fi
}

save(){
echo "Save your Iptables:..."
/etc/init.d/iptables save
/etc/init.d/iptables restart
}

case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop && start
;;
save)
start && save
;;
*)
echo $"Usage: $0 {start|stop|restart|save}"
exit 1
esac
exit 0
#End

相关文章

精彩推荐