Use IPv6 ULA addresses for Pi-hole

I noticed my IPv6 GUA changes, whenever I reboot my router and found @DL6ER script here, unfortunately, this script runs 'pihole -g' (and restarts dnsmasq or FTLDNS), even if the IPv6 address didn't change, so I improved the script.

Using GUA: Replace the first few digits ('2a02' in my case) in the grep command to match your own!!!
Using LUA: Replace '2a02' in the grep command with 'fc\|fd' (see @DL6ER script)

#!/bin/bash

# read current IPv6 address from file
CURRENT_IPV6_ADDRESS=$(ip -6 a | grep '2a02' | awk -F " " '{gsub("/[0-9]*",""); print $2}')

# read/compare previous IPv6 address from file
file=/etc/pihole/setupVars.conf
if ! grep -q "$CURRENT_IPV6_ADDRESS" $file; then
	sed -i.bak "/IPV6_ADDRESS/d;" "/etc/pihole/setupVars.conf"
	echo "IPV6_ADDRESS=${CURRENT_IPV6_ADDRESS}" >> "/etc/pihole/setupVars.conf"
	{
		echo to: <your gmail address>
		echo from: <your gmail address>
		echo subject: pihole IPv6 address change
		echo
		cat /etc/pihole/setupVars.conf
	} | /usr/sbin/ssmtp <your gmail address>
	/usr/local/bin/pihole updateGravity
fi

The script also sends me a mail, whenever the IPv6 address changed, for this to work, you need to follow the instruction in my manual, section 4.9 (install mail) and change 'your gmail address' in the script, with the desired gmail address (needs to be changed 3 times)

In order to automate the IPv6 update, you might want to add it to cron. Create /etc/cron.d/IPv6check, containing:

29 6    * * *   root    PATH="$PATH:/home/pi/" /home/pi/IPv6check.sh

change the time to something appropriate for you, I run it at 06h29

edit
correct wrong SUFFIX in sed -i command, changed from sed -i.setupVars.bak to sed -i.bak
/edit

edit2
When the job runs with cron, pihole -g isn't a valid command. Replaced the command with /usr/local/bin/pihole updateGravity
/edit2

3 Likes