Go Back   Rhinocerus > Newsgroup > Newsgroup comp.lang.python

Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old 04-09-2009, 08:36 AM
Kai Timmer
Guest
 
Posts: n/a
Default Get the ipv6 address from a interface

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
Alt Today
Advertising
 
and become member of Rhinocerus
Standard Sponsored Links

  #2 (permalink)  
Old 04-09-2009, 12:17 PM
Roy Smith
Guest
 
Posts: n/a
Default Re: Get the ipv6 address from a interface

In article
<86176ef7-c2e0-4c5d-b883-d91672e3eb0b@w40g2000yqd.googlegroups.com>,
Kai Timmer <email@kait.de> 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
  #3 (permalink)  
Old 04-09-2009, 10:46 PM
=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=
Guest
 
Posts: n/a
Default Re: Get the ipv6 address from a interface

> 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
  #4 (permalink)  
Old 04-09-2009, 10:47 PM
=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=
Guest
 
Posts: n/a
Default Re: Get the ipv6 address from a interface

> 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
  #5 (permalink)  
Old 04-11-2009, 01:50 AM
=?UTF-8?B?0JTQsNC80ZjQsNC9INCT0LXQvtGA0LPQuNC10LLRgdC60Lg=?=
Guest
 
Posts: n/a
Default Re: Get the ipv6 address from a 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).


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
Display Modes

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 Off
Pingbacks are Off
Refbacks are Off




All times are GMT. The time now is 06:52 PM.


Copyright ©2009

LinkBacks Enabled by vBSEO 3.3.0 RC2 © 2009, Crawlability, Inc.