Skip to content

Instantly share code, notes, and snippets.

@geekteq
Created March 21, 2017 14:24
Show Gist options
  • Select an option

  • Save geekteq/930ebccf43fbd600b7a3cf708ae615ba to your computer and use it in GitHub Desktop.

Select an option

Save geekteq/930ebccf43fbd600b7a3cf708ae615ba to your computer and use it in GitHub Desktop.
update dynamic IP in UFW ssh access
#!/bin/bash
HOSTNAME=dynamic.domain.com
LOGFILE=$HOME/ufw.log
Current_IP=$(host $HOSTNAME | head -n1 | cut -f4 -d ' ')
if [ ! -f $LOGFILE ]; then
/usr/sbin/ufw 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 to any port 22 proto tcp
/usr/sbin/ufw allow from $Current_IP to any port 22 proto tcp
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