fileinsert
fileinsert

Reputation: 430

Linux - find router IPv6 global address

I'm trying to find a way to reliably find the global IPv6 address of the local router. The command "ip -6 neigh show" doesn't display it. I have found the following works, so long as the router is using EUI-64 to generate a host address:

NET=$(ip -6 route|egrep "^[23]"|cut -d':' -f1-4)
EUI=$(ip -6 route|egrep "^default.*metric 1 "|cut -d':' -f3-6|cut -d' ' -f1)

ping6 $NET:$EUI -B -c 1 > /dev/null 2>&1

However this obviously doesn't work when EUI-64 isn't being used. Is there any other way I can find it?

Upvotes: 1

Views: 2880

Answers (1)

Jeremy Visser
Jeremy Visser

Reputation: 6621

There is no reliable way to determine this, as your local router doesn’t have to have a global IPv6 address at all. Best practice says it should, and the vast majority of routers out there will, but technically it’s possible that your router could only have link-local addresses on both interfaces and still route global prefixes. (Yes, I have done this before. And yes, it’s evil.)

Unless you manually added a default route pointing to a global address, you probably learned your default route via Router Advertisement which means the routing table and neighbour table (as you pointed out above) will contain a link-local address only.

However, if you perform a traceroute, the first hop is probably (key word: probably — it’s possible to spoof this stuff) your local router.

traceroute to 2600:: (2600::), 30 hops max, 80 byte packets
 1  2001:db8:1::1        0.534 ms   0.510 ms   0.749 ms
 2  2001:db8:2::1       32.494 ms  33.944 ms  35.406 ms
 3  2600::              36.935 ms  38.102 ms  39.860 ms

TL;DR it’s not possible to reliably determine this, so the short answer is “no”.

Upvotes: 5

Related Questions