Skip to content

Instantly share code, notes, and snippets.

@jasonbronson
Created November 18, 2021 21:05
Show Gist options
  • Save jasonbronson/d93830bd59d6935be5eab17d55ced990 to your computer and use it in GitHub Desktop.
Save jasonbronson/d93830bd59d6935be5eab17d55ced990 to your computer and use it in GitHub Desktop.
Update ip address bash script for UFW firewall
#!/bin/bash
#allow a dyndns name
PATH="/usr/sbin:/usr/bin"
HOSTNAME=home.bronson.dev
LOGFILE=/tmp/updateip.log
Current_IP=$(host $HOSTNAME | cut -f4 -d' ')
touch $LOGFILE
if [ $LOGFILE = "" ] ; then
ufw allow from $Current_IP
echo $Current_IP > $LOGFILE
else
Old_IP=$(cat $LOGFILE)
# if [ "$Current_IP" = "$Old_IP" ] ; then
# echo IP address has not changed
# else
yes y |ufw delete $(ufw status numbered |(grep $Old_IP|awk -F"[][]" '{print $2}'))
ufw 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