Posts Tagged ‘ bash

Firewall editing script

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.

Yum restore script

Ok, so I’m about to do a reinstallation of Fedora (I made a boo-boo which would be easier to fix by a re-install than go through manually and try to fix). I have backups, but not of the lib folders etc, I do however have backups of the uber important folders e.g. /etc /home /root etc. Anyway, I want to do a minimum-fuss reinstall, and decided to write a wee script which would take a list of all my currently installed packages and make a nice simple exectuable bash script so that once I’ve set up the repos on the new system I can simply execute this script, it’ll install all the packages I have now, then I can just restore my /etc/directory on top and hey-presto, I’ll be back to a nicely functioning system.

So without further ado:

#!/bin/bash

INSTALLED=/tmp/yum-installed

LIST=/tmp/list

EXECUTABLE=/home/rob/yum

USER=rob

GROUP=$USER

UNATTENDED_INSTALL=yes #yes/no

cat /dev/null > $EXECUTABLE

sudo yum clean all;sudo yum list installed | awk -F' ' '{print $1}' | sed 1d | sed 1d | sed s/.i386// &> $INSTALLED

exec 3< $INSTALLED

while read <&3

do

echo -n "$REPLY " >> $LIST

done

exec 3>&-

# Create the yum installation script file

if [ $UNATTENDED_INSTALL = "yes" ]

then

OPTION="-y"

fi

echo "#!/bin/bash" >> $EXECUTABLE

echo -n "$REPLY " >> $LIST

echo "sudo yum clean all;sudo yum install $OPTION `cat $LIST`" >> $EXECUTABLE

# Set correct perms

chmod +x $EXECUTABLE

chown $USER.$GROUP $EXECUTABLE

# Cleanup

rm -f $LIST

rm -f $INSTALLED

Obviously edit the vars at the top to suit your setup etc - then just sudo (or execute as root) the $EXECUTABLE