Last active
April 11, 2024 07:34
-
-
Save scotticles/143d67588924841deec873c26354044e to your computer and use it in GitHub Desktop.
Dynamically update UFW with a dynamic dns for access to your home network or device with wireguard and ssh.
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=mydyndns.com | |
SSH_PORT=22 | |
WIREGUARD_PORT=5246 | |
#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 | |
/user/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