how to get current ipv6 address when addresses change
my computer got a new address, the old address has a preferred lifetime of 0:
# ip -6 address show dev eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 state UP qlen 1000
inet6 2604:3d09:3580:a:f66d:4ff:fe32:eaf9/64 scope global mngtmpaddr dynamic
valid_lft 600sec preferred_lft 300sec
inet6 2604:3d00:d44a:486:f66d:4ff:fe32:eaf9/64 scope global deprecated mngtmpaddr dynamic
valid_lft 300sec preferred_lft 0sec
inet6 fe80::f66d:4ff:fe32:eaf9/64 scope link
valid_lft forever preferred_lft forever
I only want the valid address for scripts:
# ip -6 address show dev eth0 | grep -v deprecated | grep mngtmpaddr | sed -e’s/^.*inet6 \([^ ]*\)\/.*$/\1/;t;d’
2604:3d09:3580:a:f66d:4ff:fe32:eaf9
yay!
What this does:
- ip -6 address show dev eth0
- This shows all the ipv6 addresses configured on eth0
- grep mngtmpaddr
- This grep displays only addresses that were derived from the MAC address (IE SLAAC addresses)
- grep -v deprecated
- This grep removes addresses that are deprecated (IE preferred lifetime = 0) and shouldn’t be used.
- sed -e’s/^.*inet6 \([^ ]*\)\/.*$/\1/;t;d’
- This regex removes everything but IPv6 addresses from the line.
- found at https://superuser.com/questions/389766/linux-bash-how-to-get-interfaces-ipv6-address
- This regex removes everything but IPv6 addresses from the line.
need some way to detect when changes happen.