Created
December 27, 2017 10:26
-
-
Save xyzkab/9abb9c1372f31dbbaaace63ef5b2e2bc to your computer and use it in GitHub Desktop.
A quick find if your rule in iptables is already added/not, assume if you have a lot of rules in your iptables
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# well people must be wonder, why? | |
# for me it's necessary if you have a lot of rules in your iptables and/combine with other command | |
# ps: this will only work if you use `comment` module. eg; iptables -t nat -A OUTPUT -m comment --comment | |
# well for what is worth maybe its just to give you an idea lolz | |
# | |
mode=$1 | |
rule=$2 | |
function usage { | |
echo "Usage: $0 [mode(filter|nat|mangle|raw|security)] [rule]" | |
echo "Example: $0 nat myrule" | |
exit 0 | |
} | |
function tableRulesExist?() { | |
rules=`iptables -t $1 -L | grep -oP '(?<=\/\*\s).*(?=\*)'` | |
if [ -n "$(echo "$rules" | grep "$2")" ]; then | |
echo "Rule '$rule' on table '$1' is already-exist" | |
exist=0 | |
else | |
echo "Rule '$rule' on table '$1' is not-yet-exist" | |
exist=1 | |
fi | |
} | |
if [ -z $mode ] || [ -z $rule ]; then | |
usage | |
fi | |
case $mode in | |
"filter") | |
tableRulesExist? filter $rule | |
exit $exist | |
;; | |
"nat") | |
tableRulesExist? nat $rule | |
exit $exist | |
;; | |
"mangle") | |
tableRulesExist? mangle $rule | |
exit $exist | |
;; | |
"raw") | |
tableRulesExist? raw $rule | |
exit $exist | |
;; | |
"security") | |
tableRulesExist? security $rule | |
exit $exist | |
;; | |
*) | |
usage | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment