Get the App
SLTechnology News&Howtos  ›  Development  › 

A tutorial on the methods of managing scripts in iptables implemented by Shell

Shulou Source: shulou.com Published: 2022-06-03 09:19:48 11月11日 Update

This article mainly explains the "Shell implementation of iptables management script method tutorial", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn the "Shell implementation of iptables management script method tutorial" it!

The code is as follows:

#! / 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 (not recommended)"

Echo "(6) iptables status"

Echo "(7) iptables ACL list"

Echo "(0) exit"

Echo "generates a fw.sh file in the current directory"

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) add for source IP release"

Echo "(2) release add for server port"

Echo "(3) add for ACL with ports and services (here you want the parameter IP and port such as 0Unip 080)"

Echo "(4) Custom add"

Echo "(5) return to the next level"

Echo "- -"

Echo-n "enter you chose [0-4]:"

Read aclnum

If ["${aclnum}" = = "1"]

Then

Read ip

Iptables-An INPUT-s ${ip}-p tcp-- dport 22-j ACCEPT

Echo "iptables-An INPUT-s ${ip}-p tcp-- dport 22-j ACCEPT" > > fw.sh

# service iptables save

Elif ["${aclnum}" = = "2"]

Then

Rad ip

Iptables-An INPUT-p tcp-- dport ${IP}-j ACCEPT

Echo "iptables-An INPUT-p tcp-- dport ${IP}-j ACCEPT" > > fw.sh

# service iptables save

Elif ["${aclnum}" = = "3"]

Then

Read ip port

Iptables-An INPUT-p tcp-s ${ip}-- dport ${port}-j ACCEPT

Echo "iptables-An 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 "do you want to continue to add: [yOrganization]:"

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 for source ip"

Echo "(2) Delete for Port"

Echo "(3) deletion for ACL with ports and services"

Echo "(4) Custom deletion"

Echo "(5) return to the next 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

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 "do you want to continue to add: [yOrganization]:"

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 rule set currently in use"

Echo "(2) View simple traffic statistics for each policy or rule or chain"

Echo "(3) View NAT Table"

Echo "(4) Custom View"

Echo "(5) return to the next 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 to add: [yOrganization]:"

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 [yzone]:"

Read contine

If ["${contine}" = = "n"-o "${contine}" = = "N"]

Then

Exit

Fi

Done

Thank you for reading, the above is the content of the "Shell implementation of iptables management script method tutorial", after the study of this article, I believe you have a deeper understanding of the Shell implementation of iptables management script method tutorial, the specific use of the need for you to practice and verify. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Ports tutorials methods scripts management learning services content rules code parameters that is ideas situations files articles more servers patterns Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Microsoft vpn Xiaomi MariaDB