As the new colleagues are not very familiar with the script, to facilitate the management of iptables on the server, they decided to write a simple script, so that colleagues who are not very familiar with the script can easily manage the script. The following script is just a loop! If you do not know where to write well, or have a better idea or writing style, contact me !!!
[Root @ localhost ~] # Cat iptables. sh
#! /Bin/bash
While ["1" = "1"]
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 (remember to save and view the ACL after it is entered )"
Echo "(6) iptables status"
Echo "(7) iptables ACL list"
Echo "(0) exit"
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) allow source IP addresses"
Echo "(2) Add for server port access"
Echo "(3) add an ACL for a port or service (here the parameter IP address and port such as 0/0 80 are required )"
Echo "(4) custom Add"
Echo "(5) return to the upper level"
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
Service iptables save
Elif ["$ {aclnum}" = "2"]
Then
Rad ip
Iptables-a input-p tcp -- dport $ {IP}-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
Echo-n "do you want to continue adding: [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) Delete source ip addresses"
Echo "(2) Delete ports"
Echo "(3) delete an ACL with ports and services"
Echo "(4) custom deletion"
Echo "(5) return to the upper level"
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
Service iptables save
Elif ["$ {aclnum}" = "2"]
Then
Read port
Iptables-d input-p tcp -- 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
Echo-n "do you want to continue adding: [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) view the currently used Rule Set"
Echo "(2) view simple traffic statistics for each policy or rule and chain"
Echo "(3) view NAT table"
Echo "(4) custom View"
Echo "(5) return to the upper level"
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 "do you want to continue adding: [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