shell脚本简单配置iptables

post by rocdk890 / 2016-2-7 16:51 Sunday linux技术
  要放假了,今天实在是闲得蛋疼,本来只想写个配置iptables的脚本的,没想到被自己搞得这么复杂了,还是分享出来给大家,有兴趣的可以自己再改改.
  ps:本脚本只支持centos5和centos6,不支持centos 7,并且在脚本里也限制了.
脚本内容:
cat /root/soft_shell/setiptables.sh  
#!/bin/sh
# By rocdk890
# https://blog.slogra.com
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
夜空- 本站版权
1、本站所有主题由该文章作者发表,该文章作者与夜空享有文章相关版权
2、其他单位或个人使用、转载或引用本文时必须同时征得该文章作者和夜空的同意
3、本帖部分内容转载自其它媒体,但并不代表本站赞同其观点和对其真实性负责
4、如本帖侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意
5、原文链接:blog.slogra.com/post-633.html

标签: iptables 配置 linux shell

评论: