Stupid ipv6 tricks – get current ipv6 address – linux

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:

  1. ip -6 address show dev eth0
    1. This shows all the ipv6 addresses configured on eth0
  2.  grep mngtmpaddr
    1. This grep displays only addresses that were derived from the MAC address (IE SLAAC addresses)
  3. grep -v deprecated
    1. This grep removes addresses that are deprecated (IE preferred lifetime = 0) and shouldn’t be used.
  4. sed  -e’s/^.*inet6 \([^ ]*\)\/.*$/\1/;t;d’
    1. This regex removes  everything but IPv6 addresses from the line.
      1. found at https://superuser.com/questions/389766/linux-bash-how-to-get-interfaces-ipv6-address

need some way to detect when changes happen.

Leave a Reply


Xt q UUvnU