Skip to content

获取最新有效期 ipv6 地址 #1327

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
2 tasks done
klttr opened this issue Nov 25, 2024 · 6 comments
Open
2 tasks done

获取最新有效期 ipv6 地址 #1327

klttr opened this issue Nov 25, 2024 · 6 comments
Labels
enhancement New feature or request keep

Comments

@klttr
Copy link

klttr commented Nov 25, 2024

Description

获取最新valid_lft时间的ipv6

  • ip a命令可以查看到网口ip地址,存在多个ipv6,每个ipv6都有valid_lftpreferred_lft时间,获取最新的valid_lft有效时间(最大值);当然你可以用将下面命令中valid_lft换成preferred_lft(其实两者效果一样)

  • 使用valid_lft

    ip -6 a | awk -F '[ \t]+|/' '/inet6/ && $3 != "::1" && $3 !~ /^fe80::/ && $3 !~ /^fda8:/ {ipv6=$3; getline; if ($2=="valid_lft") {split($3, t, "sec"); if (t[1] > max) {max=t[1]; best=ipv6}}} END {print best}'
  • 使用preferred_lft

    ip -6 a | awk -F '[ \t]+|/' '/inet6/ && $3 != "::1" && $3 !~ /^fe80::/ && $3 !~ /^fda8:/ {ipv6=$3; getline; if ($4=="preferred_lft") {split($5, t, "sec"); if (t[1] > max) {max=t[1]; best=ipv6}}} END {print best}'

    $3 !~ /^fe80::/是用于排除已fe80::开头的ipv6;需要排除其他已xxxx开头的ipv6自行添加

  • 或者使用

     ip -6 a | awk -F '[ \t]+|/' '/inet6/ && $3 ~ /^(2408|2409|240e)(:|::)/ {ipv6=$3; getline; if ($2=="valid_lft") {split($3, t, "sec"); if (t[1] > max) {max=t[1]; best=ipv6}}} END {print best}'

    直接获取以2408|2409|240e开头的ip

Problem

No response

Other Description

No response

Checklist

  • I am using the latest version and have confirmed that the feature is not yet implemented in the latest version
  • I have searched for similar feature requests before submitting this one
@klttr klttr added the enhancement New feature or request label Nov 25, 2024
@imShire
Copy link

imShire commented Nov 29, 2024

遇到同样问题,感谢分享

@jeessy2 jeessy2 added the keep label Dec 18, 2024
@Test031
Copy link

Test031 commented Dec 20, 2024

没搞懂如何操作,可以傻瓜一点吗?

@Test031
Copy link

Test031 commented Dec 21, 2024

获取IPv6结果失败! 未能成功执行命令

@NEANC
Copy link

NEANC commented Mar 30, 2025

获取IPv6结果失败! 未能成功执行命令

这个示例是在Linux系统中获取最新有效期IPv6地址,win请查阅#531 (comment)

@qiao-summer
Copy link

我竟然获取到了个fec0开头的,是不是可以不排除fe80,而是只获取240开头的呢

@klttr
Copy link
Author

klttr commented Mar 30, 2025

我竟然获取到了个fec0开头的,是不是可以不排除fe80,而是只获取240开头的呢

不想获取的地址添加排除就行了 $3 !~ /^fec0::/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request keep
Projects
None yet
Development

No branches or pull requests

6 participants