Revisions
-
provegard revised this gist
Dec 29, 2011 . 1 changed file with 0 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -37,7 +37,6 @@ class union_ifa_ifu(Union): ('ifu_dstaddr', POINTER(struct_sockaddr)),] class struct_ifaddrs(Structure): pass struct_ifaddrs._fields_ = [ ('ifa_next', POINTER(struct_ifaddrs)), @@ -69,7 +68,6 @@ def getfamaddr(sa): addr = inet_ntop(family, sa.sin6_addr) return family, addr class NetworkInterface(object): def __init__(self, name): self.name = name @@ -99,7 +97,6 @@ def get_network_interfaces(): if addr: i.addresses[family] = addr return retval.values() finally: libc.freeifaddrs(ifap) -
provegard revised this gist
Dec 29, 2011 . 1 changed file with 3 additions and 10 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -49,13 +49,6 @@ class struct_ifaddrs(Structure): ('ifa_data', c_void_p),] libc = ctypes.CDLL(ctypes.util.find_library('c')) def ifap_iter(ifap): ifa = ifap.contents @@ -80,7 +73,7 @@ def getfamaddr(sa): class NetworkInterface(object): def __init__(self, name): self.name = name self.index = libc.if_nametoindex(name) self.addresses = {} def __str__(self): @@ -91,7 +84,7 @@ def __str__(self): def get_network_interfaces(): ifap = POINTER(struct_ifaddrs)() result = libc.getifaddrs(pointer(ifap)) if result != 0: raise OSError(get_errno()) del result @@ -108,7 +101,7 @@ def get_network_interfaces(): return retval.values() finally: libc.freeifaddrs(ifap) if __name__ == '__main__': print [str(ni) for ni in get_network_interfaces()] -
provegard created this gist
Dec 29, 2011 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,114 @@ #!/usr/bin/python # Based on getifaddrs.py from pydlnadms [http://code.google.com/p/pydlnadms/]. # Only tested on Linux! from socket import AF_INET, AF_INET6, inet_ntop from ctypes import ( Structure, Union, POINTER, pointer, get_errno, cast, c_ushort, c_byte, c_void_p, c_char_p, c_uint, c_int, c_uint16, c_uint32 ) import ctypes.util import ctypes class struct_sockaddr(Structure): _fields_ = [ ('sa_family', c_ushort), ('sa_data', c_byte * 14),] class struct_sockaddr_in(Structure): _fields_ = [ ('sin_family', c_ushort), ('sin_port', c_uint16), ('sin_addr', c_byte * 4)] class struct_sockaddr_in6(Structure): _fields_ = [ ('sin6_family', c_ushort), ('sin6_port', c_uint16), ('sin6_flowinfo', c_uint32), ('sin6_addr', c_byte * 16), ('sin6_scope_id', c_uint32)] class union_ifa_ifu(Union): _fields_ = [ ('ifu_broadaddr', POINTER(struct_sockaddr)), ('ifu_dstaddr', POINTER(struct_sockaddr)),] class struct_ifaddrs(Structure): pass struct_ifaddrs._fields_ = [ ('ifa_next', POINTER(struct_ifaddrs)), ('ifa_name', c_char_p), ('ifa_flags', c_uint), ('ifa_addr', POINTER(struct_sockaddr)), ('ifa_netmask', POINTER(struct_sockaddr)), ('ifa_ifu', union_ifa_ifu), ('ifa_data', c_void_p),] libc = ctypes.CDLL(ctypes.util.find_library('c')) _if_nametoindex = libc.if_nametoindex _getifaddrs = libc.getifaddrs _getifaddrs.restype = c_int _getifaddrs.argtypes = [POINTER(POINTER(struct_ifaddrs))] _freeifaddrs = libc.freeifaddrs _freeifaddrs.restype = None _freeifaddrs.argtypes = [POINTER(struct_ifaddrs)] def ifap_iter(ifap): ifa = ifap.contents while True: yield ifa if not ifa.ifa_next: break ifa = ifa.ifa_next.contents def getfamaddr(sa): family = sa.sa_family addr = None if family == AF_INET: sa = cast(pointer(sa), POINTER(struct_sockaddr_in)).contents addr = inet_ntop(family, sa.sin_addr) elif family == AF_INET6: sa = cast(pointer(sa), POINTER(struct_sockaddr_in6)).contents addr = inet_ntop(family, sa.sin6_addr) return family, addr class NetworkInterface(object): def __init__(self, name): self.name = name self.index = _if_nametoindex(name) self.addresses = {} def __str__(self): return "%s [index=%d, IPv4=%s, IPv6=%s]" % ( self.name, self.index, self.addresses.get(AF_INET), self.addresses.get(AF_INET6)) def get_network_interfaces(): ifap = POINTER(struct_ifaddrs)() result = _getifaddrs(pointer(ifap)) if result != 0: raise OSError(get_errno()) del result try: retval = {} for ifa in ifap_iter(ifap): name = ifa.ifa_name i = retval.get(name) if not i: i = retval[name] = NetworkInterface(name) family, addr = getfamaddr(ifa.ifa_addr.contents) if addr: i.addresses[family] = addr return retval.values() finally: _freeifaddrs(ifap) if __name__ == '__main__': print [str(ni) for ni in get_network_interfaces()]