Created
November 4, 2022 02:04
-
-
Save diginfo/779a31e4ccf1f41094f287deaae44c25 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 | |
HOSTNAME="$1" | |
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; | |
} | |
#### MAIN-FUNC ##### | |
CIP=$(dig +short +nocomments $HOSTNAME | tail -1) | |
ISBLOCK="$(ufw status | grep "REJECT" | grep $CIP)" | |
if [ ! -f $LOGFILE ]; then | |
/usr/sbin/ufw insert 1 allow from $CIP | |
echo -n $CIP > $LOGFILE | |
else | |
OIP=$(cat $LOGFILE) | |
if [ "$ISBLOCK" ];then | |
echo "$CIP is Blocked. Allowing now..." | |
/usr/sbin/ufw delete reject from $CIP | |
/usr/sbin/ufw insert 1 allow from $CIP | |
elif [ "$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