Skip to content

Instantly share code, notes, and snippets.

@andrewvc
Created May 13, 2010 21:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewvc/400476 to your computer and use it in GitHub Desktop.
Save andrewvc/400476 to your computer and use it in GitHub Desktop.
Minimalist iptables init script
#!/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
@cschell
Copy link

cschell commented Mar 23, 2012

great script, thank you very much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment