整个脚本是利用控制流(循环语句)实现一个交互的效果,再利用判断和read来实现配置
虽然有200多行代码,细看其实结构很清晰明了,一看就懂!
不过,为了能拿上台面加了很多字体颜色,不止是为了交互体验,也为了让关键字或者提示更加醒目。
同样可以用此结构来实现其他service的配置,个人感觉交互式的配置更直观、更简洁明了,我写了几个类似的交互配置的脚本都经常在工作中使用
代码奉上,后面附个运行截图:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 | #!/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 |