Last active
December 18, 2023 16:39
-
-
Save saudiqbal/e27266c5fae5f15a45a6a76cf377c7fb to your computer and use it in GitHub Desktop.
Add multiple IPv6 address on prefix change.
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 | |
IPv6PrefixFile=$HOME/IPv6Prefix.txt | |
IPv6Prefix=$(cat $IPv6PrefixFile) | |
__rfc5952_expand () { | |
read addr mask < <(IFS=/; echo $1) | |
quads=$(grep -oE "[a-fA-F0-9]{1,4}" <<< ${addr/\/*} | wc -l) | |
grep -qs ":$" <<< $addr && { addr="${addr}0000"; (( quads++ )); } | |
grep -qs "^:" <<< $addr && { addr="0000${addr}"; (( quads++ )); } | |
[ $quads -lt 8 ] && addr=${addr/::/:$(for (( i=1; i<=$(( 8 - quads )) ; i++ )); do printf "0000:"; done)} | |
addr=$(for quad in $(IFS=:; echo ${addr}); do printf "${delim}%04x" "0x${quad}"; delim=":"; done) | |
[ ! -z $mask ] && echo $addr/$mask || echo $addr | |
} | |
IPv6Current=$(/sbin/ip -6 addr | grep inet6 | grep -vE 'host|mngtmpaddr|temporary' | grep -F 'scope global dynamic' | sed -e 's/^.*inet6 \([^ ]*\)\/.*$/\1/;t;d' | grep '^2') | |
IPv6Current="$(__rfc5952_expand $IPv6Current)" | |
IPv6Current=${IPv6Current:0:19} | |
if [ $IPv6Current != $IPv6Prefix ]; then | |
for i in $(cat $HOME/ipv6-addresses.txt); | |
do | |
ip -6 addr del ${IPv6Prefix}:${i} dev ens18 | |
ip -6 addr add ${IPv6Current}:${i} dev ens18 | |
done | |
echo $IPv6Current > $IPv6PrefixFile | |
fi | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
First create a file using
echo "0000:0000:0000:0000" > $HOME/IPv6Prefix.txt
After that create another file for storing all the suffix.
ipv6-addresses.txt
For Example
5e37:080b:66b7:68bb
52bf:52fe:a1df:60e3
3b32:a9d4:a9f3:074f
9ec7:f4da:b166:0a5f
620c:eb97:3e93:ece3
Rebooting the server removes all the added IPv6 suffixes so on server startup reset the IPv6Prefix.txt file again using
echo "0000:0000:0000:0000" > $HOME/IPv6Prefix.txt
because on reboot the script thinks the prefix has not changed and will not add the suffix on a fresh boot.