0. 通过googel的dns服务器,获取出口的IPV6地址
dig -6 TXT +short o-o.myaddr.l.google.com @ns1.google.com | sed 's|"||g' 或者 dig @ns1.google.com -t txt o-o.myaddr.l.google.com +short | tr -d \"
通过查询一个 特殊地址( o-o.myaddr.l.google.com ) 的txt记录
或者通过一个http服务
curl http://icanhazip.com 或者 curl -s ipv6.icanhazip.com 或者 curl api64.ipify.org 或者 curl 'https://api64.ipify.org?format=json' 或 curl 6.ipw.cn
1. 通过查询本机的路由表
ip route get 2001:4860:4860::8888 | sed -n '/src/{s/.*src *\([^ ]*\).*/\1/p;q}'
sed命令解释
sed # the sed executable located via $PATH -n # no output unless explicitly requested ' # begin the command space /src/ # regex match the string 'src' { # begin a block of commands ** s/ # begin a substitution (match) .*src * # match anything leading up to and including src and any number of spaces \([^ ]*\) # define a group containing any number of non spaces .* # match any trailing characters (which will begin with a space because of the previous rule). / # begin the substitution replacement \1 # reference the content in the first defined group / # end the substitution p # print (explicitly, remember) the result ; # designate the end of the command q # quit } # end the block of commands ' # end the command space
或者用awk也可以
ip route get 2001:4860:4860::8888 | awk '{gsub(".*src",""); print $1; exit}'
或者让ip命令输出json
ip -6 -j route get 2001:4860:4860::8888
prefsrc的值,就是
或者
ip route get 2001:4860:4860::8888 | grep src| sed 's/.*src \(.*\)$/\1/g'
2. 用ip 命令获取地址 global类型地址
ip -6 a show scope global up | egrep -o '([0-9a-f:]+:+)+[0-9a-f]+' -m 1
只匹配第1个地址
3. 删除路由器上 deprecated 的地址
ip -6 addr show dev br-lan | grep deprecated | sed 's/.*inet6 \(.*\)$/\1/g' ip -6 addr show dev enp1s0 | sed -n '/deprecated/{s/.*inet6 *\([^ ]*\).*/\1/p;q}' ip -6 addr show dev enp1s0 | grep deprecated | egrep -o '([0-9a-f:]+:+)+[0-9a-f]+' -m 1 ip -6 addr del .... dev br-lan