tags:

views:

44

answers:

3

Hi there,

Do you know how I could get one of the IPv6 adress of one of my interface in python2.6. I tried something with the socket module which lead me nowhere.

Thanks.

A: 

You could just simply run 'ifconfig' with a subprocess.* call and parse the output.

jvdneste
I thought about that, and I have actually a ready (and long) command to get my Ipv6 well formated. But it really look ugly. It must exist a righter way to do this.
jaes
+1  A: 

The netifaces module should do it under Linux.

import netifaces
addrs = netifaces.ifaddresses('eth0')
addrs[netifaces.AF_INET6][0]['addr']
Mark
Thanks a lot, sadly I want to stay very simple in this script which should run on a lot of different environment. So I was looking for something which wouldn't necessitate an easy_install.But still, it works great, thanks again.
jaes
A: 

I'll surely go with this, it sould be working good, even if I find that really ugly.

step1 = Popen(['ip','addr','show','br0'],stdout=PIPE)
step2 = Popen(['grep','inet6'],stdout=PIPE,stdin=step1.stdout)
step3 = Popen(['sed','-e','/fe80/d','-e','s/ *inet6 *//g','-e','s/\/64.*$//g'],stdout=PIPE,stdin=step2.stdout)
step4 = Popen(['tail','-n1'],stdout=PIPE,stdin=step3.stdout)
step4.communicate()[0]

Thanks for the help again.

jaes