Ok, here’s a wee script I threw together to help myself and any other sys admins who have to make a lot of edits to the iptables firewall in Red Hat / CentOS. It basically opens up the /etc/sysconfig/iptables file to allow you to make your edit(s) and then when you close the editor, it’ll ask you if you want to apply the changes straight away, or apply them immediately to allow for testing, then unload the iptables module after a set amount of time (2 minutes as defined on line 2) in case anything broke.
#!/bin/bash
TESTING_MINS=2
vim /etc/sysconfig/iptables
clear
QUESTION1="Do you want to restart the firewall now? (hit 't' to test for $TESTING_MINS min(s)) [y/n/t] "
echo -n $QUESTION1
a=""
while test -z "$a"
do
read -n1 a
echo ""
case "$a" in
Y|y)
echo -e "Restarting...\n\n"
/sbin/service iptables restart
;;
N|n)
exit 0
;;
T|t)
echo -e "Time is now `date +%H:%M` -firewall service will be stopped at `date +%H:%M -d "+$TESTING_MINS min"`\nIf your test was successful, you will need to manually start the service again by running:\nservice iptables start"
echo "/sbin/service iptables stop &> /dev/null" | at now + $TESTING_MINS min &> /dev/null
echo ""
/sbin/service iptables restart
;;
*)
a=""
echo -n $QUESTION1
;;
esac
done
P.S. Any scripts I write and publish here are © Rob Freeman and released under the GPL unless otherwise stated.