我有一个 bash 脚本,我想获取给定网络适配器(在我的情况下为“ens18”)的全局 IPv6 地址。首先,我发出了以下命令:
ip -6 addr
这给了我以下输出,其中包含我正在寻找的地址:
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 state UNKNOWN qlen 1000
inet6 ::1/128 scope host noprefixroute
valid_lft forever preferred_lft forever
2: ens18: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 state UP qlen 1000
inet6 fd00::3170:4ff:eff4:a4ab/64 scope global dynamic mngtmpaddr noprefixroute
valid_lft 6736sec preferred_lft 3136sec
inet6 2a02:8070:2982:4300:3170:4ff:eff4:a4ab/64 scope global dynamic mngtmpaddr noprefixroute
valid_lft 6736sec preferred_lft 3136sec
inet6 af60::3170:4ff:eff4:a4ab/64 scope link
valid_lft forever preferred_lft forever
3: br-7ac197bd041e: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 state UP
inet6 af60::42:80ff:fe52:3481/64 scope link
valid_lft forever preferred_lft forever
4: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 state UP
inet6 af60::42:6eff:fecf:5e6/64 scope link
valid_lft forever preferred_lft forever
6: veth074f8b0@if5: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 state UP
inet6 af60::7073:40ff:fe59:aad0/64 scope link
valid_lft forever preferred_lft forever
8: vethe85ed67@if7: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 state UP
inet6 af60::5c09:65ff:fe34:34f4/64 scope link
valid_lft forever preferred_lft forever
因此全局 IPv6 地址为“2a02:8070:2982:4300:3170:4ff:eff4:a4ab”。
如何从我的 bash 脚本中可靠地检索该值?