`

用bash脚本来配置 iptables防火墙

 
阅读更多
#!/bin/bash
##############USMAN AKRAM "FA05-BTN-005" (~*Lucky*~) BTN-6########
######COMSATS INSTITUTE OF INFORMATION TECHNOLOGY - ABBOTTABAD####
echo -e "****************Welcome*************"
###############################IPTABLE SERVICES PROGRAM BEGINS HERE###############################
checkstatus()
 {
  opt_checkstatus=1
 while [ $opt_checkstatus != 7 ]
      do
       clear
  #echo -e "/nChoose the Option Bellow!!!/n
  echo -e "/n/t*****Note: Save your Iptables before stop/Restart the iptables Services*****/n"
  echo -e "   1. Save the iptables/n
   2. Status of Iptables/n
   3. Start iptables Services/n
   4. Stop iptables Services/n
   5. Restart iptable Services/n
   6. Flush iptables (**Use Carefully_it will remove all the rules from iptables**)/n
   7. Go back to Main Menu"
  read opt_checkstatus
  case $opt_checkstatus in
   1) echo -e "*******************************************************/n" 
               /etc/init.d/iptables save 
      echo -e "/n*******************************************************/n"
    echo -e "Press Enter key to Continue..."
    read temp;;
   2) echo -e "*******************************************************/n"
               /etc/init.d/iptables status 
      echo -e "*******************************************************"
                                echo -e "Press Enter key to Continue..."
                                     read temp;;
   3) echo -e "*******************************************************/n"  
               /etc/init.d/iptables start 
      echo -e "*******************************************************/n"
                                 echo -e "Press Enter key to Continue..."
                                       read temp;;
   
   4) echo -e "*******************************************************/n"
               /etc/init.d/iptables stop
      echo -e "*******************************************************/n"
                                echo -e "Press Enter key to Continue..."
                                     read temp;;
     
             5) echo -e "*******************************************************/n"
                      /etc/init.d/iptables restart 
      echo -e "*******************************************************/n"
                                echo -e "Press Enter key to Continue..."
                                     read temp;;
   6) iptables -F 
   echo -e "*******************************************************"
   echo -e "All the Rules from the Iptables are Flushed!!!"
   echo -e "*******************************************************/n"
                                echo -e "Press Enter key to Continue..."
                                 read temp;;
   7) main;;
   *) echo -e "Wrong Option Selected!!!"
  esac
 done
 }
###############################BUILD FIREWALL PROGRAM BEGINS FROM HERE############################### 
buildfirewall()
 {
  ###############Getting the Chain############
  echo -e "Using Which Chain of Filter Table?/n
  1. INPUT
  2. OUTPUT
  3. Forward"
  read opt_ch
  case $opt_ch in
   1) chain="INPUT" ;;
   2) chain="OUTPUT" ;;
   3) chain="FORWARD" ;;
   *) echo -e "Wrong Option Selected!!!"
  esac
 
  #########Getting Source IP Address##########
  #Label
   
  echo -e "
  1. Firewall using Single Source IP/n
  2. Firewall using Source Subnet/n
  3. Firewall using for All Source Networks/n"
  read opt_ip
   
  case $opt_ip in
   1) echo -e "/nPlease Enter the IP Address of the Source"
   read ip_source ;;
   2) echo -e "/nPlease Enter the Source Subnet (e.g 192.168.10.0/24)"
   read ip_source ;;
   3) ip_source="0/0" ;;
   #4) ip_source = "NULL" ;;
   *) echo -e "Wrong Option Selected"
  esac
  #########Getting Destination IP Address##########
   echo -e "
  1. Firewall using Single Destination IP/n
                2. Firewall using Destination Subnet/n
         3. Firewall using for All Destination Networks/n"
  
     read opt_ip
              case $opt_ip in
        1) echo -e "/nPlease Enter the IP Address of the Destination"
                     read ip_dest ;;
               2) echo -e "/nPlease Enter the Destination Subnet (e.g 192.168.10.0/24)"
                     read ip_dest ;;
               3) ip_dest="0/0" ;;
        #4) ip_dest = "NULL" ;;
               *) echo -e "Wrong Option Selected"
       esac
       ###############Getting the Protocol#############
       echo -e "
       1. Block All Traffic of TCP
       2. Block Specific TCP Service
       3. Block Specific Port
       4. Using no Protocol"
       read proto_ch
       case $proto_ch in
        1) proto=TCP ;;
        2) echo -e "Enter the TCP Service Name: (CAPITAL LETTERS!!!)"
       read proto ;;
        3) echo -e "Enter the Port Name: (CAPITAL LETTERS!!!)" 
       read proto ;;
        4) proto="NULL" ;;
        *) echo -e "Wrong option Selected!!!"
       esac
 
       #############What to do With Rule############# 
       echo -e "What to do with Rule?
       1. Accept the Packet
       2. Reject the Packet
       3. Drop the Packet
       4. Create Log"
       read rule_ch
       case $rule_ch in 
        1) rule="ACCEPT" ;;
        2) rule="REJECT" ;;
        3) rule="DROP" ;;
        4) rule="LOG" ;;
       esac
###################Generating the Rule####################
echo -e "/n/tPress Enter key to Generate the Complete Rule!!!"
read temp
echo -e "The Generated Rule is /n"
if [ $proto == "NULL" ]; then
 echo -e "/niptables -A $chain -s $ip_source -d $ip_dest -j $rule/n"
 gen=1
else
 echo -e "/niptables -A $chain -s $ip_source -d $ip_dest -p $proto -j $rule/n"
 gen=2
fi 
echo -e "/n/tDo you want to Enter the Above rule to the IPTABLES? Yes=1 , No=2"
read yesno
if [ $yesno == 1 ] && [ $gen == 1 ]; then
 iptables -A $chain -s $ip_source -d $ip_dest -j $rule
else if [ $yesno == 1 ] && [ $gen == 2 ]; then
 iptables -A $chain -s $ip_source -d $ip_dest -p $proto -j $rule         
   
else if [ $yesno == 2 ]; then
 
 main
fi
fi
fi
}
      
main()
{
 ROOT_UID=0
 if [ $UID == $ROOT_UID ];
 then
 clear
 opt_main=1
 while [ $opt_main != 4 ]
 do
echo -e "//////////////////////////////////////n" 
#############Check Whether the iptables installed or not############ 
 echo -e "/t*****Main Menu*****/n
 1. Check Iptables Package/n
 2. Iptables Services/n
 3. Build Your Firewall with Iptables/n
 4. Exit"
 read opt_main
 case $opt_main in
  1) echo -e "******************************"
    rpm -q iptables 
     echo -e "******************************" ;;
  2) checkstatus ;;
  3) buildfirewall ;;
  4) exit 0 ;;
  *) echo -e "Wrong option Selected!!!"
 esac
done
else
 echo -e "You Must be the ROOT to Perfom this Task!!!"
fi
}
main
exit 0
分享到:
评论

相关推荐

    一键配置CentOS iptables防火墙的Shell脚本分享

    手里几台VPS配置iptables太繁琐,看到了朱哥的LNMP脚本里有一个自动配置iptables防火墙的脚本,借来改了一下,给需要的人用; 只提供常用端口的设置,如果你有特殊需求只需自行添加或减少相应的端口即可; 使用方法...

    firewall:使用 iptables 在 Linux 中生成防火墙的 bash 脚本集。 基于

    防火墙使用 iptables 在 Linux 上管理防火墙的脚本。标准用法您只需要运行一个脚本来管理防火墙,但您可以向其传递各种命令。 它们通常应按显示的顺序运行。 请注意,所有命令都需要以 root 权限执行(例如,通过...

    CentOS7 Docker防火墙的简单配置教程

    CentOS7 Docker防火墙的简单配置 ...创建 iptables 配置脚本 cat >> /usr/local/bin/fired.sh <<'EOF' #!/bin/bash iptables -F iptables -X iptables -Z iptables -P INPUT DROP iptables -P OUTPUT ACCEPT i

    firewall-rules:快速而肮脏的 IPtables 防火墙脚本

    我用 bash 写了一个快速而肮脏的防火墙脚本 您需要修改 4 行: tcp_ports_in=() tcp_ports_out=() udp_ports_in=() udp_ports_out() 语法: tcp_ports_in=(80 443) 端口 IN 是需要打开服务器上的服务才能访问的...

    shell 学习脚本

    一些bash shell学习的脚本。iptables 防火墙设置脚本,脚本使用技巧等。

    iptables-webserver:用于独立网络服务器或反向代理的样板 iptables 防火墙规则

    可以用作需要时调用的 bash 脚本或初始化脚本。目标:独立主机Solowall 适用于独立的 Web 服务器或 HTTP 代理。 不是路由器,不是网关,不是您的本地 PC特征通过拒绝任何未明确允许的内容来保护主机设置有很多选择...

    Fran FireWall fwiptables Generator:f-iptables:Fran FireWall iptables 生成器-开源

    Fran iptables 是一个简单的 FireWall iptables 脚本,用于在 linux 中配置防火墙,简单且免费,在命令行环境、CLI 环境和 GUI 环境中。 [程序名称] Fran FireWall fwiptables Generator [程序说明] 一个脚本在bash ...

    Shell监控iptables运行状态

    最近在调试服务器的iptables,自己做了个定时关iptables,但晚上回家很少开电脑,所以就没法去启动iptables,当然你可能会说,为什么不取消定时关闭iptables,我只能说个人的环境不一样,需求也就不一样. 脚本内容: 代码...

    debian-server:完整的DebianUbuntu Web应用服务器安装

    其中包括用于NGINX,MariaDB,MongoDB,MySQL,PHP 5/7,Redis,CouchDB,Fail2Ban,Monit,XtraBackup,OpenSSL,创建和设置bash环境以及使用iptables的防火墙的脚本。 一切都是可选的! NGINX还包括对...

    Fran FireWall fwiptables Generator:Fran FireWall iptables Generator: fwiptables script linux-开源

    Fran iptables 是一个简单的 FireWall iptables 脚本,用于在 linux 中配置防火墙,简单且免费,在命令行环境、CLI 环境和 GUI 环境中。 [程序名称] Fran FireWall fwiptables Generator [程序说明] 用于 iptables ...

    Firewall-Rules:防火墙规则解析器

    此脚本将允许您为基于 iptables 的防火墙编写“格式化英语”规则,而无需编写 iptables 脚本。 任何阅读规则文件的人都应该能够立即知道它的作用,而无需了解 iptables。 为什么? 在最近将本机 IPv6 连接添加到我...

    firewallmy.systems:卷曲http

    firewall-my-system - 即时...生成的防火墙实际上是设置这些防火墙的 shell 脚本,因此可以随意将此命令的输出通过管道传输到 bash 或其他东西中。 示例:生成打开 TCP 22、80 和 UDP 5000 的 iptables 防火墙脚本 f

    高级java笔试题-puppet-example:puppet示例项目,在现实世界中运行,管理300多个虚拟机

    filter:iptables防火墙 nat:端口转发 gateway:网关(iptables实现) haproxy:负载均衡器 hhvm:Facebook开源出来的高性能PHP引擎 hosts:/etc/hosts配置工具 inifile:ini配置文件操作工具(第三方开源) ...

    i.NET – QoS and security for Linux-开源

    i.NET – Linux 的 QoS 和安全性是一个开源项目,可提供易于配置的防火墙和流量管理。 它基于 Linux 操作系统,带有专门准备的内核、iproute2、iptables 和 bash 脚本进行配置。

    tor-relay-install:Tor继电器自动安装器

    这个简单的bash脚本将自动安装和配置Tor中继。 该脚本是为仅在Ubuntu上进行测试而设计的! 该脚本将: 将正式的Tor仓库添加到您的apt源码中 安装tor,deb.torproject.org-keyring,iptables和iptables-persistent...

    rcbrasil-firewall-开源

    这是用perl和bash编写的将防火墙和经过身份验证的网关脚本转换为iptables和squid的规则。

    MossadScripts-开源

    一系列BASH脚本,可控制您的服务器,例如“ Iptables”(防火墙脚本),系统完整性检查,检查过程,内核脚本MRTG,FloodRate,Adduser(用于外壳盒)等。

Global site tag (gtag.js) - Google Analytics