如何获取当前全局 IPv6 前缀作为 python 中的变量

How to get current global IPv6-prefix as a variable in python

如何在 python 中获取我当前的全球 IPv6 前缀?

假设我在 eth0 上有全球 IP 2010:a:b:c::400/64,我怎样才能在 python 中获得这个地址? 有没有办法拆分前缀?

对于这样的过程,您可以在 python 的 subprocess 模块中尝试 check_output。 一个简单的例子:

from subprocess import check_output
output=check_output(['ifconfig'],shell=True).decode('utf-8')
print(output)

这将打印 ifconfig 的输出。希望对您有所帮助,编码愉快!

您可以使用 netifaces 来做到这一点:

>>> import netifaces
>>> addrs=netifaces.ifaddresses('ens3')
>>> addrs[netifaces.AF_INET6]
[{'netmask': 'ffff:ffff:ffff:ffff::/64', 'addr': '2001:19f0:4400:4983:5400:ff:fe54:1677'}, {'netmask': 'ffff:ffff:ffff:ffff::/64', 'addr': 'fe80::5400:ff:fe5e:1977%ens3'}]

请注意,有一个 link-local ipv6 地址以 fe80 开头。