Created
April 1, 2024 12:01
-
-
Save cpuwolf/c0810c29127ae73de0f2802510ec2351 to your computer and use it in GitHub Desktop.
Dynamic update DNS inside UFW
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
#SET THE FOLLOWING | |
HOSTNAME=xxx.com | |
SSH_PORT=27017 | |
WIREGUARD_PORT=27017 | |
#IF IT DOES NOT WORK, AT LEAST ON UBUNTU INSTALL, bind-utils to get the host command | |
#Create a cron /15 * * * * root bash /path/to/dynamicdnsupdater.sh | |
if [[ $EUID -ne 0 ]]; then | |
echo "This script must be run as root" | |
exit 1 | |
fi | |
new_ip=$(host $HOSTNAME | head -n1 | cut -f4 -d ' ') | |
old_ip=$(/usr/sbin/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 | |
/usr/sbin/ufw delete allow from $old_ip to any port $SSH_PORT | |
/usr/sbin/ufw delete allow from $old_ip to any port $WIREGUARD_PORT | |
fi | |
/usr/sbin/ufw allow from $new_ip to any port $SSH_PORT comment $HOSTNAME | |
/usr/sbin/ufw allow from $new_ip to any port $WIREGUARD_PORT comment $HOSTNAME | |
echo iptables have been updated | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment