#!/bin/bash

HOSTNAME=.freeddns.com
LOGFILE=/var/log/sam_firewall.log

touch /var/log/sam_firewall.log
chmod 740 /var/log/sam_firewall.log
chown root:adm /var/log/sam_firewall.log

Current_IP=$(host $HOSTNAME | cut -f4 -d' ')

if [[ $(iptables --list | grep $Current_IP) = "" ]]; then
  if [[ $LOGFILE = "" ]] ; then
    iptables -I INPUT -i eth0 -s $Current_IP -j ACCEPT
  else
    Old_IP=$(cat $LOGFILE)
    if [[ $Old_IP != $Current_IP ]] ; then
      if [[ $(iptables --list | grep $Old_IP) = "" ]] ; then
        echo ''
      else
        iptables -D INPUT -i eth0 -s $Old_IP -j ACCEPT
      fi
    fi
    iptables -I INPUT -i eth0 -s $Current_IP -j ACCEPT
  fi
fi
echo $Current_IP > $LOGFILE

Add a code snippet to your website: www.paste.org