Created
December 2, 2017 09:58
-
-
Save cdpb/051b7e6d4533d2f7a5357a6badd7a18f to your computer and use it in GitHub Desktop.
Log dynamic dns records
This file contains hidden or 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 | |
# first A record, second authoritive DNS | |
RECORDS=(example.com ns.example.com mustermann.de ns.google.de) | |
LOGPATH="/var/log/iplog" | |
COUNTER=1 | |
for RECORD in ${RECORDS[@]}; do | |
if [[ ! $(($COUNTER % 2)) -eq 0 ]]; then | |
DOMAIN=$RECORD | |
else | |
NS=$RECORD | |
IP=$(dig +noall +answer @$NS $DOMAIN | awk '{print $NF}') | |
if [[ ! $IP =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then | |
continue | |
fi | |
DOMAINLOG=$(echo $DOMAIN | tr '.' '-') | |
LOG="$LOGPATH/$DOMAINLOG.log" | |
LASTENTRY=$(tail -n1 $LOG | grep $IP) | |
if [[ $? -eq 0 ]]; then | |
echo "$DOMAIN didn't change" | |
else | |
if [[ ! -d $LOGPATH ]]; then | |
mkdir $LOGPATH | |
fi | |
echo "$(date +%F-%T) $DOMAIN $IP" | tee -a $LOG | |
fi | |
fi | |
COUNTER=$(($COUNTER + 1)) | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment