#!/bin/bash updateIPTable(){ grep $1 $2 if [ $? -ne 0 ] then echo "-A INPUT -s $1 -p tcp --dport 3306 -j ACCEPT" >> $2 fi } restartIPTablesService(){ service iptables restart } updateHostConfig() { echo $2 > $1.iptable } removeOldIPFromRules(){ grep -v $1 $2 > iptables.tmp mv iptables.tmp $2 } changeIP(){ IPTABLE_CONFIG_FILE="/etc/sysconfig/iptables" HOST_LOOKUP=$1 touch $HOST_LOOKUP.iptable NEW_IP=`nslookup $HOST_LOOKUP | tail -2 | head -1 |cut -d' ' -f2` OLD_IP=`cat $HOST_LOOKUP.iptable` echo $NEW_IP echo $OLD_IP if [ "x$NEW_IP" != "x$OLD_IP" ] then updateHostConfig $HOST_LOOKUP $NEW_IP if [ "x$OLD_IP" != "x" ] then removeOldIPFromRules $OLD_IP $IPTABLE_CONFIG_FILE fi updateIPTable $NEW_IP $IPTABLE_CONFIG_FILE touch dynamic_ip_changed fi } main(){ HOSTS="wardy.is-a-geek.org dorwardvillaruz.com" for HOST in $HOSTS do changeIP $HOST done if [ -e dynamic_ip_changed ] then restartIPTablesService rm dynamic_ip_changed fi } main