|
|||
|
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 |
|
|
||||
|
||||
|
|
|
|||
|
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? |
|
|||
|
> 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 |
|
|||
|
> 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 |
|
|||
|
> 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. |
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|