Skip to content

Instantly share code, notes, and snippets.

@cmbuckley
Created December 15, 2019 17:20
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 cmbuckley/cb56055c7741a226286ecb0becb503c9 to your computer and use it in GitHub Desktop.
Save cmbuckley/cb56055c7741a226286ecb0becb503c9 to your computer and use it in GitHub Desktop.
#!/bin/bash
HOSTNAME=your.dns.here
UFW=/usr/sbin/ufw
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root"
exit 1
fi
new_ip=$(dig +short $HOSTNAME)
old_ip=$($UFW status | grep $HOSTNAME | head -n1 | tr -s ' ' | cut -f3 -d ' ')
if [ "$new_ip" = "$old_ip" ] ; then
echo IP address has not changed
else
if [ -n "$old_ip" ] ; then
$UFW delete allow from $old_ip to any
fi
$UFW allow from $new_ip to any comment $HOSTNAME
echo firewall has been updated
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment