Last active
July 3, 2020 11:14
-
-
Save pacmac/41651c4f19c4760ebb20b5de0ee0476f to your computer and use it in GitHub Desktop.
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 | |
## 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