linux 脚本自动添加防火墙规则

#!/bin/bash
while true
do
	echo "
              1,放行端口
      	      2,封锁端口
              3,放行IP
              4,封锁IP"
	read -ep "请输入您要操作编号:" bh
	case $bh in
	1)
	read -ep "请输入放行端口:" port
	echo "正在放行端口......"
	iptables -I INPUT -p tcp --dport $port -j ACCEPT
	iptables -I INPUT -p udp --dport $port -j ACCEPT
	if [ $? -eq 0 ];then
		echo "端口放行成功!"
		echo "端口放行规则添加成功!"
	else
		echo "端口放行失败!"
	fi
	;;
	2)
	read -ep "请输入封锁端口:" port
	echo "正在封锁端口......"
	iptables -I INPUT -p tcp --dport $port -j REJECT
	iptables -I INPUT -p udp --dport $port -j REJECT
	if [ $? -eq 0 ];then
                echo "端口封锁成功!"
        else
                echo "端口封锁失败!"
        fi
	;;
	3)
	read -ep "请输入放行IP:" IP
	echo "正在放行IP......"
	iptables -I INPUT -s $IP -j ACCEPT
	if [ $? -eq 0 ];then
                echo "IP放行成功!"
		echo "IP放行规则添加成功!"
        else
                echo "IP放行失败!"
        fi
	;;
	4)
	read -ep "请输入封锁IP:" IP
	echo "正在封锁IP......"	
	iptables -I INPUT -s $IP -j REJECT
	if [ $? -eq 0 ];then
                echo "IP封锁成功!"
        else
                echo "IP封锁失败!"
        fi
	;;
	*)
	echo "输入有误,退出脚本"
	exit
	;;
	esac
	read -ep "是否继续输入(y/n)" next
	if [ $next = n ];then
		echo "退出脚本!"
		exit
	fi
done

你可能感兴趣的:(shell,linux)