Produnis
Produnis

Reputation: 567

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

how can I get my current global IPv6 prefix in python?

Let's say I have the global IP 2010:a:b:c::400/64 on eth0, how can I get this address in python? And is there a way to split the prefix?

Upvotes: 0

Views: 541

Answers (2)

jfly
jfly

Reputation: 7990

You can use netifaces to do that:

>>> 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'}]

note that there is a link-local ipv6 address which starts with fe80.

Upvotes: 3

Anmol Gautam
Anmol Gautam

Reputation: 1020

For such process you can try check_output in subprocess module of python . A simple example for same:

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

This will print the output of ifconfig . Hope it helps , happy coding!

Upvotes: 0

Related Questions