#!/bin/bash
HOSTNAME=yourhost.ddns.net
LOGFILE=$HOME/ufw.$HOSTNAME.log
Current_IP=$(host $HOSTNAME | head -n1 | cut -f4 -d ' ')
if [ ! -f $LOGFILE ]; then
/usr/sbin/ufw allow from $Current_IP to any port 2222
/usr/sbin/ufw allow from $Current_IP to any port 9090
/usr/sbin/ufw allow from $Current_IP to any port 8443
echo $Current_IP > $LOGFILE
else
Old_IP=$(cat $LOGFILE)
if [ "$Current_IP" = "$Old_IP" ] ; then
echo IP address has not changed
else
/usr/sbin/ufw delete allow from $Current_IP to any port 2222
/usr/sbin/ufw delete allow from $Current_IP to any port 9090
/usr/sbin/ufw delete allow from $Current_IP to any port 8443
/usr/sbin/ufw allow from $Current_IP to any port 2222
/usr/sbin/ufw allow from $Current_IP to any port 9090
/usr/sbin/ufw allow from $Current_IP to any port 8443
echo $Current_IP > $LOGFILE
echo iptables have been updated
fi
fi