Skip to content

Instantly share code, notes, and snippets.

@pacmac
Last active July 3, 2020 11:14
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 pacmac/41651c4f19c4760ebb20b5de0ee0476f to your computer and use it in GitHub Desktop.
Save pacmac/41651c4f19c4760ebb20b5de0ee0476f to your computer and use it in GitHub Desktop.
#!/bin/bash
## bash <(curl -Ls https://gist.github.com/pacmac/41651c4f19c4760ebb20b5de0ee0476f/raw/)
## curl -o ufw-dns -L https://gist.github.com/pacmac/41651c4f19c4760ebb20b5de0ee0476f/raw/
## */5 * * * * bash <(curl -s https://gist.github.com/pacmac/41651c4f19c4760ebb20b5de0ee0476f/raw/)
HOSTNAME="$1";
if [ $# -eq 0 ]; then
echo "Hostname is required";
exit 0;
fi
LOGDIR="$HOME/.ufw";
LOGFILE=$LOGDIR/$HOSTNAME
if [ ! -d "$LOGDIR" ];then
mkdir $LOGDIR
fi
function _alert {
echo "[$HOSTNAME] OLD:$OIP NEW:$CIP $1";
ufw status | grep $CIP
exit 0;
}
CIP=$(dig +short +nocomments $HOSTNAME | tail -1)
if [ ! -f $LOGFILE ]; then
/usr/sbin/ufw insert 1 allow from $CIP
echo -n $CIP > $LOGFILE
else
OIP=$(cat $LOGFILE)
if [ "$CIP" = "$OIP" ] ; then
_alert "no change."
else
/usr/sbin/ufw delete allow from $OIP
/usr/sbin/ufw insert 1 allow from $CIP
echo -n $CIP > $LOGFILE
_alert "Updated."
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment