Shell實現的iptables管理指令碼分享

Shell實現的iptables管理指令碼分享
1 Star2 Stars3 Stars4 Stars5 Stars 給文章打分!
Loading...

以前的指令碼是用save模式,現在把命令附加到一個檔案裡面,這樣的話,可以方便的二次修改什麼的
指令碼基本是這樣的,大家可以跟自己的情況再次修改~  增加功能什麼的。

複製程式碼 程式碼如下:
#!/bin/bash
while true
do
clear
echo “———————-menu———————-“
echo “(1) service iptables restart”
echo “(2) iptables add”
echo “(3) iptables delete”
echo “(4) iptables stop”
echo “(5) iptables save(不推薦使用這種模式)”
echo “(6) iptables status”
echo “(7) iptables ACL list”
echo “(0) exit”
echo “會在當前的目錄下生成一個fw.sh檔案”
echo “————————————————-“
echo -n “enter you chose[0-7]:”
read num
if [ ${num} -lt 0 -o ${num} -gt 7 ]
    then
      echo “this is not between 0-7”
else
   if [ “${num}” == “1” ]
      then
      service iptables restart&
else
   if [ “${num}” == “2” ]
#######################################################
       then
          while [ “1” == “1” ]
          do
          clear
          echo “———————-add ACL———————-“
          echo “(1) 針對源IP放行新增”
          echo “(2) 針對伺服器埠放行新增”
          echo “(3) 針對有埠和服務的ACL新增(這裡要引數IP和埠 例如 0/0 80)”
          echo “(4) 自定義新增”
          echo “(5) 退回上一級”
          echo “————————————————-“
          echo -n “enter you chose[0-4]:”
          read aclnum
          if [ “${aclnum}” == “1” ]
             then
             read ip
             iptables -A INPUT -s ${ip} -p tcp –dport 22 -j ACCEPT
         echo “iptables -A INPUT -s ${ip} -p tcp –dport 22 -j ACCEPT” >>fw.sh
#            service iptables save
          elif [ “${aclnum}” == “2” ]
             then
             rad ip
             iptables -A INPUT -p tcp –dport ${IP}  -j ACCEPT
         echo “iptables -A INPUT -p tcp –dport ${IP}  -j ACCEPT” >>fw.sh
#            service iptables save
          elif [ “${aclnum}” == “3” ]
             then
             read ip port
             iptables -A INPUT -p tcp -s ${ip} –dport ${port} -j ACCEPT
             echo “iptables -A INPUT -p tcp -s ${ip} –dport ${port} -j ACCEPT” >>fw.sh
#            service iptables save
          elif [ “${aclnum}” == “4” ]
             then
             read addacl
             `${addacl}`
             service iptables save
          else
             break
          fi
          echo -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 “(1) 針對源ip刪除”
          echo “(2) 針對埠刪除”
          echo “(3) 針對有埠和服務的ACL刪除”
          echo “(4) 自定義刪除”
          echo “(5) 退回上一級”
          echo “————————————————-“
          echo -n “enter you chose[0-5]:”
          read aclnum
          if [ “${aclnum}” == “1” ]
             then
             read ip
             iptables -D INPUT -s ${ip} -p tcp –dport 22 -j ACCEPT
             echo “iptables -D INPUT -s ${ip} -p tcp –dport 22 -j ACCEPT” >>fw.sh
#            service iptables save
          elif [ “${aclnum}” == “2” ]
             then
             read port
             iptables -D INPUT -p tcp –dport ${port}  -j ACCEPT
             echo “iptables -D INPUT -p tcp –dport ${port}  -j ACCEPT” >>fw.sh
#            service iptables save
          elif [ “${aclnum}” == “3” ]
             then
             read ip port
             iptables -D INPUT -p tcp -s ${ip} –dport ${port} -j ACCEPT
         echo “iptables -D INPUT -p tcp -s ${ip} –dport ${port} -j ACCEPT” >>fw.sh
#            service iptables save
          elif [ “${aclnum}” == “4” ]
             then
             read deleteacl
             `${deleteacl}`
             service iptables save
          else
             break
          fi
          echo -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 “(1) 檢視當前正在使用的規則集”
       echo “(2) 檢視每個策略或每條規則、每條鏈的簡單流量統計”
       echo “(3) 檢視NAT表”
       echo “(4) 自定義檢視”
       echo “(5) 退回上一級”
       echo “————————————————-“
       echo -n “enter you chose[0-5]:”
       read aclnum
       if [ “${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
       echo -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
echo -n “Do you contine [y/n]:”
read contine
if [ “${contine}” == “n” -o “${contine}” == “N” ]
   then
   exit
fi
done

您可能感興趣的文章:

Shell監控iptables執行狀態Shell指令碼實現監控iptables規則是否被修改Shell指令碼實現監控iptables執行狀態Shell指令碼配合iptables遮蔽來自某個國家的IP訪問CentOS 7 下使用 iptables

相關文章

前端開發 最新文章