Skip to content

Instantly share code, notes, and snippets.

@pacmac
Created December 18, 2019 09:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pacmac/5e0337f43f62f65f09c3c4ab483a1d18 to your computer and use it in GitHub Desktop.
Save pacmac/5e0337f43f62f65f09c3c4ab483a1d18 to your computer and use it in GitHub Desktop.
#!/bin/sh
HOSTNAME="my.domain.com"
LOGFILE=$HOME/ufw.log
Current_IP=$(host $HOSTNAME | head -n1 | cut -f4 -d ' ')
if [ ! -f $LOGFILE ]; then
/usr/sbin/ufw insert 1 allow from $Current_IP to any port 22 proto tcp
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 $Old_IP
/usr/sbin/ufw insert 1 allow from $Current_IP
echo $Current_IP > $LOGFILE
echo iptables have been updated
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment