Linux/bash: How to get interface's IPv6 address?
There are lots of ways you could do this.
Here is one:
ip addr show dev eth0 | sed -e's/^.*inet6 \([^ ]*\)\/.*$/\1/;t;d'
It is similar to Robert's answer, except strips out the address only.
You could use:
ip -6 addr
It will return all the IPv6 adresses you have configured.
If you're under Linux you also can parse:
/proc/net/if_inet6
First column is the full IPv6 without colons. Sixth column is the interface name.
Here a rather lengthy not optimized version (I only now the basics of awk) Maybe python/perl is a better choice.
for i in "$(grep enp0s25 /proc/net/if_inet6)"; do
echo "$i" | awk '{
split($1, _, "[0-9a-f]{,4}", seps)
joined = sep = ""
for (i=1; i in seps; i++) {
joined = joined sep seps[i]
sep = ":"
}
print joined
}'
done
If you have GNU awk (gawk) this can be shortened to:
for i in "$(grep enp0s25 /proc/net/if_inet6)"; do
echo "$i" | gawk '@include "join"
{
split($1, _, "[0-9a-f]{,4}", seps)
print join(seps, 1, length(seps), ":")
}'
done
You may put it in a {ba,z,}sh function to use it later.