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

來源:互聯網
上載者:User

以前的指令碼是用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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.