Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Get the ipv6 address from a interface

Reply
Thread Tools

Get the ipv6 address from a interface

 
 
Kai Timmer
Guest
Posts: n/a
 
      04-09-2009
Hello,
i need a function that returns the ipv6 address from a given interface
name. For ipv4 i use this one:
def get_ip_address(ifname):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
return socket.inet_ntoa(fcntl.ioctl(
s.fileno(),
0x8915, # SIOCGIFADDR
struct.pack('256s', ifname[:15])
)[20:24])

which works great. But i am not enough into python to port that to
ipv6. It has to work under linux only. Any help is appreciated.

Greets,
Kai
 
Reply With Quote
 
 
 
 
Roy Smith
Guest
Posts: n/a
 
      04-09-2009
In article
<(E-Mail Removed)>,
Kai Timmer <(E-Mail Removed)> wrote:

> Hello,
> i need a function that returns the ipv6 address from a given interface
> name. For ipv4 i use this one:
> def get_ip_address(ifname):
> s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
> return socket.inet_ntoa(fcntl.ioctl(
> s.fileno(),
> 0x8915, # SIOCGIFADDR
> struct.pack('256s', ifname[:15])
> )[20:24])
>
> which works great. But i am not enough into python to port that to
> ipv6. It has to work under linux only. Any help is appreciated.


I'm not 100% sure what you're trying to do, but the above is horribly
non-portable. You probably want to be looking at socket.getpeername() and
socket.getsockname().

In general, concepts like "the address of an interface" are difficult. In
many OS's, a given interface may have multiple addresses. This is
especially true in IPv6 where you've have both link local and global
unicast addresses on the same interface.

Can you back up a few steps and describe what it is that you're trying to
do, i.e. the use case?
 
Reply With Quote
 
 
 
 
Martin v. Lwis
Guest
Posts: n/a
 
      04-09-2009
> I'm not 100% sure what you're trying to do, but the above is horribly
> non-portable. You probably want to be looking at socket.getpeername() and
> socket.getsockname().


This only works if you are actually connected. I think he wants to find
out the local address without actually connecting.

> In general, concepts like "the address of an interface" are difficult. In
> many OS's, a given interface may have multiple addresses. This is
> especially true in IPv6 where you've have both link local and global
> unicast addresses on the same interface.


In Linux, you can only have one IPv4 address per interface (and you have
to use alias interfaces, such as eth0:0, to assign multiple addresses
to a physical link).

For IPv6 and Linux, you are right.

>
> Can you back up a few steps and describe what it is that you're trying to
> do, i.e. the use case?


I guess he wants to do the equivalent of ifconfig.

Regards,
Martin
 
Reply With Quote
 
Martin v. Lwis
Guest
Posts: n/a
 
      04-09-2009
> which works great. But i am not enough into python to port that to
> ipv6. It has to work under linux only. Any help is appreciated.


Not sure how universal this is, but I would read /proc/net/if_inet6.
At least, that's what ifconfig does, and it seems to work fine.

martin@mira:~$ cat /proc/net/if_inet6
fe80000000000000000000004d804137 1c 40 20 80 sixxs
fe80000000000000020d61fffe543e15 02 40 20 80 sis
00000000000000000000000000000001 01 80 10 80 lo
200106f809000a850000000000000002 1c 40 00 80 sixxs

Regards,
Martin
 
Reply With Quote
 
Дамјан Георгиевски
Guest
Posts: n/a
 
      04-11-2009
> In Linux, you can only have one IPv4 address per interface (and you
> have to use alias interfaces, such as eth0:0, to assign multiple
> addresses to a physical link).


that's actually not correct, use the "ip" tool (iproute2 package) to see
how easily you can have several addresses to a single interface.
ip addr add 1.1.1.1/24 dev eth0
ip addr add 2.2.2.1/24 dev eth0

the need for alias interfaces has been removed, a long time ago (AFAIK
even before the 2.4 kernel).

--
дамјан ( http://softver.org.mk/damjan/ )

In theory, there is no difference between theory and practice.
But, in practice, there is.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Interface name vs interface IP address Laughing Man Cisco 3 04-16-2010 05:21 AM
need to lookup master interface that inherit IP address to unnumbered interface via SNMP tua Cisco 0 10-30-2007 12:30 PM
Ipv6 on a ipv4/ipv6 hostname Jesse van den Kieboom Ruby 1 06-05-2005 12:57 PM
ipv6 - which source address to use? wd VOIP 1 07-04-2003 11:12 AM
ipv6 - which source address to use? wd Cisco 1 07-04-2003 11:12 AM



Advertisments