Get the ipv6 address from a interface

Discussion in 'Python' started by Kai Timmer, Apr 9, 2009.

  1. Kai Timmer

    Kai Timmer Guest

    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
     
    Kai Timmer, Apr 9, 2009
    #1
    1. Advertising

  2. Kai Timmer

    Roy Smith Guest

    In article
    <>,
    Kai Timmer <> 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?
     
    Roy Smith, Apr 9, 2009
    #2
    1. Advertising

  3. > 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
     
    Martin v. Löwis, Apr 9, 2009
    #3
  4. > 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
     
    Martin v. Löwis, Apr 9, 2009
    #4
  5. > 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.
     
    Дамјан ГеоргиевÑки, Apr 11, 2009
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Skumar
    Replies:
    3
    Views:
    833
    Steve Zimmerman
    Aug 24, 2003
  2. Replies:
    8
    Views:
    5,489
    Jim Langston
    Feb 9, 2006
  3. Replies:
    6
    Views:
    883
    James Kanze
    Apr 2, 2008
  4. Philipp Hagemeister
    Replies:
    3
    Views:
    1,148
    Martin v. Löwis
    Mar 4, 2009
  5. Jesse van den Kieboom

    Ipv6 on a ipv4/ipv6 hostname

    Jesse van den Kieboom, Jun 5, 2005, in forum: Ruby
    Replies:
    1
    Views:
    394
    Dibya Prakash
    Jun 5, 2005
Loading...

Share This Page