整個腳本是利用控制流(循環語句)實現一個交互的效果,再利用判斷和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 |