Created
May 13, 2010 21:23
-
-
Save andrewvc/400476 to your computer and use it in GitHub Desktop.
Minimalist iptables init script
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 | |
#Simple iptables init script. | |
#Config | |
iptables_path="/sbin" | |
iptables="$iptables_path/iptables" | |
iptables_save="$iptables_path/iptables-save" | |
iptables_restore="$iptables_path/iptables-restore" | |
iptables_rules="/etc/iptables_rules" | |
#Check for needed files and permissions | |
req_binaries=($iptables $iptables_save $iptables_restore) | |
for e in ${req_binaries[@]}; do | |
if [ ! -x $e ]; then | |
echo "Could not find required binary located at '$e'" >&2 | |
exit 1 | |
fi | |
done | |
if [ ! -f $iptables_rules ]; then | |
echo "Firewall rules file not found, creating empty rules file at '$iptables_rules'" >&2 | |
touch $iptables_rules | |
fi | |
#Some Helpers | |
function clear_rules() { | |
echo "Clearing existing rules" | |
$iptables -F | |
} | |
#Do what I'm told | |
case $1 in | |
stop) | |
clear_rules | |
;; | |
save) | |
echo "Saving current iptables configuration to '$iptables_rules'" | |
$iptables_save > $iptables_rules | |
;; | |
status) | |
$iptables -L | |
;; | |
start | restart | force-reload) | |
clear_rules | |
echo "Loading iptables rules from '$iptables_rules'" | |
$iptables_restore < $iptables_rules | |
;; | |
*) | |
echo "Invalid/No argument specified. Try (start|stop|restart|status|save)." | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
great script, thank you very much!