iptables交互配置脚本【Linux运维之道之脚本案例】

整个脚本是利用控制流(循环语句)实现一个交互的效果,再利用判断和read来实现配置

虽然有200多行代码,细看其实结构很清晰明了,一看就懂!

不过,为了能拿上台面加了很多字体颜色,不止是为了交互体验,也为了让关键字或者提示更加醒目。

同样可以用此结构来实现其他service的配置,个人感觉交互式的配置更直观、更简洁明了,我写了几个类似的交互配置的脚本都经常在工作中使用

代码奉上,后面附个运行截图:

#!/bin/bash
while [ "1" == "1" ]
do
clear
echo "———————-menu————————"
echo -e "\033[49;32;1m(1) 重启\033[49;35;1miptables\033[0m "
echo -e "\033[49;32;1m(2) 添加\033[49;35;1miptables\033[49;32;1m规则\033[0m "
echo -e "\033[49;32;1m(3) 删除\033[49;35;1miptables\033[49;32;1m规则\033[0m "
echo -e "\033[49;32;1m(4) 关闭\033[49;35;1miptables\033[0m "
echo -e "\033[49;32;1m(5) 保存\033[49;35;1miptables\033[49;32;1m规则(输完ACL后要记得保存和查看)\033[0m "
echo -e "\033[49;32;1m(6) 查看\033[49;35;1miptables\033[49;32;1m状态\033[0m "
echo -e "\033[49;32;1m(7) 添加\033[49;35;1miptables\033[49;32;1m控制列表\033[0m "
echo -e "\033[49;32;1m(0) 退出脚本\033[0m "
echo "————————————————————"
echo -en "\033[49;31;1m 请输入数字0-7:  \033[0m"
read num
if [[ "${num}" =~ "^$" ]];
then
echo -e "\033[49;31;5m 请输入0-7中的数字!或者Ctrl+C退出! \033[0m"
else
if [[ "${num}" =~ "^[a-zA-Z]+$" ]];
then
echo -e "\033[49;31;5m 请输入0-7中的数字!或者Ctrl+C退出! \033[0m"
else
#if [ ${num} -lt 0 -o ${num} -gt 7 ]
if [[ "${num}" =~ "[8-9]" ]];
then
echo -e "\033[49;31;5m 请输入0-7中的数字!或者Ctrl+C退出! \033[0m"
else
if [ "${num}" == "1" ]
then
service iptables restart&
else
if [ "${num}" == "2" ]
#######################################################
then
while [ "1" == "1" ]
do
clear
echo "———————-add ACL———————"
echo -e "\033[49;32;1m(1) 针对源\033[49;35;1mIP\033[49;32;1m放行添加\033[0m "
echo -e "\033[49;32;1m(2) 针对服务器\033[49;35;1m端口\033[49;32;1m放行添加 \033[0m "
echo -e "\033[49;32;1m(3) 针对有\033[49;35;1mIP和端口\033[49;32;1m的规则添加\033[49;35;1m(这里要参数IP和端口 例如:1.1.1.1/255.255.0.0 80)\033[0m "
echo -e "\033[49;32;1m(4) 自定义完整语句添加 \033[0m "
echo -e "\033[49;32;1m(0) 返回上一级 \033[0m "
echo "——————————————————————"
echo -en "\033[49;31;1m 请输入数字0-4: \033[0m"
read aclnum
if [[ "${aclnum}" =~ "^$" ]];
then
echo -e "\033[49;31;5m 请输入0-4中的数字!或者Ctrl+C退出! \033[0m"
else
if [[ "${aclnum}" =~ "^[a-zA-Z]+$" ]];
then
echo -e "\033[49;31;5m 请输入0-4中的数字!或者Ctrl+C退出! \033[0m"
else
if [[ "${aclnum}" =~ "[5-9]" ]];
then
echo -e "\033[49;31;5m 请输入0-4中的数字!或者Ctrl+C退出! \033[0m"
elif [ "${aclnum}" == "1" ]
then
read ip
iptables -A INPUT -s ${ip} -p tcp -j ACCEPT
service iptables save
elif [ "${aclnum}" == "2" ]
then
read port
iptables -A INPUT -p tcp -s 0/0 --dport ${port} -j ACCEPT
service iptables save
elif [ "${aclnum}" == "3" ]
then
read ip port
iptables -A INPUT -p tcp -s ${ip} --dport ${port} -j ACCEPT
service iptables save
elif [ "${aclnum}" == "4" ]
then
read addacl
`${addacl}`
service iptables save
else
break
fi
fi
fi
echo -n "是否想继续添加,回车或Y继续,按N返回上一级!: [y/n]:"
read contine
if [ "${contine}" == "n" -o "${contine}" == "N" ]
then
break
fi
done
#######################################################
else
if [ "${num}" == "3" ]
then
while [ "1" == "1" ]
do
clear
echo "———————delete ACL———————-"
echo -e "\033[49;32;1m(1) 针对源\033[49;35;1mIP\033[49;32;1m删除\033[0m "
echo -e "\033[49;32;1m(2) 针对服务器\033[49;35;1m端口\033[49;32;1m删除 \033[0m "
echo -e "\033[49;32;1m(3) 针对有\033[49;35;1mIP和端口\033[49;32;1m的规则删除\033[49;35;1m(这里要参数IP和端口 例如:1.1.1.1/255.255.0.0 80)\033[0m "
echo -e "\033[49;32;1m(4) 自定义完整语句删除 \033[0m "
echo -e "\033[49;32;1m(0) 返回上一级 \033[0m "
echo "————————————————-"
echo -en "\033[49;31;1m 请输入数字0-4: \033[0m"
read aclnum
if [[ "${aclnum}" =~ "^$" ]];
then
echo -e "\033[49;31;5m 请输入0-4中的数字!或者Ctrl+C退出! \033[0m"
else
if [[ "${aclnum}" =~ "^[a-zA-Z]+$" ]];
then
echo -e "\033[49;31;5m 请输入0-4中的数字!或者Ctrl+C退出! \033[0m"
else
if [[ "${aclnum}" =~ "[5-9]" ]];
then
echo -e "\033[49;31;5m 请输入0-4中的数字!或者Ctrl+C退出! \033[0m"
elif [ "${aclnum}" == "1" ]
then
read ip
iptables -D INPUT -s ${ip} -p tcp  -j ACCEPT
service iptables save
elif [ "${aclnum}" == "2" ]
then
read port
iptables -D INPUT -p tcp -s 0/0 --dport ${port} -j ACCEPT
service iptables save
elif [ "${aclnum}" == "3" ]
then
read ip port
iptables -D INPUT -p tcp -s ${ip} --dport ${port} -j ACCEPT
service iptables save
elif [ "${aclnum}" == "4" ]
then
read deleteacl
`${deleteacl}`
service iptables save
else
break
fi
fi
fi
echo -n "是否想继续删除,回车或Y继续,按N返回上一级!: [y/n]:"
read contine
if [ "${contine}" == "n" -o "${contine}" == "N" ]
then
break
fi
done
###################################################################
else
if [ "${num}" == "4" ]
then
echo -e "`service iptables stop&` "
else
if [ "${num}" == "5" ]
then
echo -e "`service iptables save&`"
else
if [ "${num}" == "6" ]
then
echo -e "`service iptables status&`"
else
##################################################################
if [ "${num}" == "7" ]
then
while [ "1" == "1" ]
do
clear
echo "———————list ACL———————-"
echo -e "\033[49;32;1m(1) 看当前正在使用的规则集 \033[0m "
echo -e "\033[49;32;1m(2) 查看每个策略或每条规则、每条链的简单流量统计\033[0m "
echo -e "\033[49;32;1m(3) 查看NAT表 \033[0m "
echo -e "\033[49;32;1m(4) 自定义查看 \033[0m "
echo -e "\033[49;32;1m(0) 退回上一级\033[0m "
echo "————————————————-"
echo -en "\033[49;31;1m 请输入数字0-4: \033[0m"
read aclnum
if [[ "${aclnum}" =~ "^$" ]];
then
echo -e "\033[49;31;5m 请输入0-4中的数字!或者Ctrl+C退出! \033[0m"
else
if [[ "${aclnum}" =~ "^[a-zA-Z]+$" ]];
then
echo -e "\033[49;31;5m 请输入0-4中的数字!或者Ctrl+C退出! \033[0m"
else
if [[ "${aclnum}" =~ "[5-9]" ]];
then
echo -e "\033[49;31;5m 请输入0-4中的数字!或者Ctrl+C退出! \033[0m"
elif [ "${aclnum}" == "1" ]
then
iptables -L
elif [ "${aclnum}" == "2" ]
then
iptables -L -n -v
elif [ "${aclnum}" == "3" ]
then
iptables -L -t nat
elif [ "${aclnum}" == "4" ]
then
read listacl
`${listacl}`
else
break
fi
fi
fi
echo -n "是否想继续查看,回车或Y继续,按N返回上一级!: [y/n]:"
read contine
if [ "${contine}" == "n" -o "${contine}" == "N" ]
then
break
fi
done
################################################
else
exit
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
echo -n "按回车或者Y返回上一级,按N退出程序![y/n]:"
read contine
if [ "${contine}" == "n" -o "${contine}" == "N" ]
then
exit
fi
done

iptables交互配置脚本【Linux运维之道之脚本案例】

iptables交互配置脚本【Linux运维之道之脚本案例】

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/3ea70788f7af08e93c6fd2db09f164ec.html