Last active
December 26, 2017 13:32
-
-
Save eXtrem0us/fc8aaeb3f6432695b244298e1e3008bc to your computer and use it in GitHub Desktop.
Allowing TCP/UDP ports through CSF Firewall easily
This file contains hidden or 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 | |
###### | |
#$1<--->Method:::TCP_IN,TCP_OUT,UDP_IN,UDP_OUT | |
#$2<--->Port Number | |
[ -z "$2" ] && echo "Usage: addportcsf.sh Method PortNumber" && exit 1 | |
csfconfpath="/etc/csf/csf.conf" | |
[ ! -w "$csfconfpath" ] && echo "CSF config file could not be modified or not found." && exit 1 | |
portlist="$(grep "\ *$1.*\"$" $csfconfpath|cut -d\" -f2)" | |
occurance=false | |
for i in $(echo $portlist|tr , "\n") | |
do | |
[ "$i" == "$2" ] && echo "found existing port $i,skipping" && occurance=true && break | |
done | |
if [ "$occurance" == "false" ] | |
then | |
echo "appending the port $2 to method $1" | |
portlist+=",$2" | |
finalrule="$1 = \"$portlist\"" | |
echo $finalrule | |
sed -i "s/\ *$1.*\"$/$finalrule/g" $csfconfpath | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment