You are not logged in.

#1 2016-04-09 22:55:38

TheChickenMan
Member
From: United States
Registered: 2015-07-25
Posts: 354

Dynamic DNS using GIT

I have a server running on a machine which has an IP address that changes periodically. Traditional dynamic DNS services never worked reliably enough for me and I had always been wary of running a service which I do not fully understand. For some time I lived with the inevitability that the IP would change and I would have to go to the server and get, then update it manually. It was definitely not the most convenient solution. Recently I decided that I could create my own set of scripts to do this using git. The first script updates a git repository with the new IP if it has changed since last run and the second updates the hosts file of a remote computer if there is a new entry in the git repository. I have these run daily using cron.

#!/usr/bin/bash

GIT_DIR="/var/log/lab-dns"
IP_LOG='external-IP.log'
TEST_SITE='icanhazip.com'

cd $GIT_DIR

CURRENT_IP=$(curl -s $TEST_SITE)
PREVIOUS_IP=$(tail -n1 $IP_LOG)
PREVIOUS_IP="${PREVIOUS_IP%%\ *}"

if ! [ $CURRENT_IP == $PREVIOUS_IP ] ; then
	echo "$CURRENT_IP --- $(date)" >> $IP_LOG
	git add $IP_LOG
	git commit -m "Auto-Updating IP Address List"
	git push -u origin master
fi
#!/usr/bin/bash

GIT_DIR="/var/log/lab-dns"
IP_LOG='external-IP.log'
HOST_NAME='EXTERNAL.BBN'
HOSTS_FILE='/etc/hosts'
LINE='20'

cd $GIT_DIR
git pull -q

CURRENT_IP=$(tail -n1 $IP_LOG)
CURRENT_IP="${CURRENT_IP%%\ *}"

PREVIOUS_IP="$(sed -n "${LINE}p" $HOSTS_FILE)"
PREVIOUS_IP="${PREVIOUS_IP%%\	*}"

if ! [ $CURRENT_IP == $PREVIOUS_IP ] ; then
	sed -i "${LINE}s/.*/${CURRENT_IP}	${HOST_NAME}/" $HOSTS_FILE
fi

Last edited by TheChickenMan (2016-04-09 22:56:10)


If quantum mechanics hasn't profoundly shocked you, you haven't understood it yet.
Niels Bohr

Offline

Board footer

Powered by FluxBB