当前位置: 首页>>代码示例>>Python>>正文


Python netifaces.AF_LINK属性代码示例

本文整理汇总了Python中netifaces.AF_LINK属性的典型用法代码示例。如果您正苦于以下问题:Python netifaces.AF_LINK属性的具体用法?Python netifaces.AF_LINK怎么用?Python netifaces.AF_LINK使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在netifaces的用法示例。


在下文中一共展示了netifaces.AF_LINK属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: _get_addresses_from_ifaddresses

# 需要导入模块: import netifaces [as 别名]
# 或者: from netifaces import AF_LINK [as 别名]
def _get_addresses_from_ifaddresses(self, ifaddresses):
        """Get addresses of a given interface

        :param ifaddresses: raw addresses of interface (from netifaces)
        :return: list of addresses
        """
        addresses = []
        for family in ifaddresses:
            if family != netifaces.AF_LINK:  # no hwaddr
                for addr in ifaddresses[family]:
                    a = addr["addr"]
                    if family == netifaces.AF_INET6:
                        a = self._remove_ipv6_special_stuff(a)
                    addresses.append(
                        Address(address=a, family=family))

        return addresses 
开发者ID:OpenMTC,项目名称:OpenMTC,代码行数:19,代码来源:GEventNetworkManager.py

示例2: _create_interface

# 需要导入模块: import netifaces [as 别名]
# 或者: from netifaces import AF_LINK [as 别名]
def _create_interface(self, name, ifaddresses):
        """Create Interface tuple based on given interfaces addresses. (function
        independent of netifaces)

        :param name:
        :param ifaddresses:
        :return:
        """
        addresses = self._get_addresses_from_ifaddresses(ifaddresses)
        try:
            hwaddress = ifaddresses[netifaces.AF_LINK][0]["addr"]
        except (IndexError, KeyError):
            self.logger.debug("No hardware address found for %s!", name)
            hwaddress = None

        return Interface(name=name,
                         addresses=addresses,
                         hwaddress=hwaddress) 
开发者ID:OpenMTC,项目名称:OpenMTC,代码行数:20,代码来源:GEventNetworkManager.py

示例3: _get_interface

# 需要导入模块: import netifaces [as 别名]
# 或者: from netifaces import AF_LINK [as 别名]
def _get_interface(self, name):
        """Returns an Interface object identified by name

        :param name: name of interface
        :return Interface: interface
        :raise UnknownInterface: if interface was not found
        """
        if name not in netifaces.interfaces():
            raise InterfaceNotFoundException("%s was not found" % name)
        else:
            ifaddresses = netifaces.ifaddresses(name)
            addresses = self._get_addresses_from_ifaddresses(ifaddresses)
            try:
                hwaddress = ifaddresses[netifaces.AF_LINK][0]["addr"]
            except (IndexError, KeyError):
                self.logger.debug("No hardware address found for %s!", name)
                hwaddress = None
            return Interface(name=name,
                             addresses=addresses,
                             hwaddress=hwaddress) 
开发者ID:OpenMTC,项目名称:OpenMTC,代码行数:22,代码来源:GEventNetworkManager.py

示例4: get_host_devices

# 需要导入模块: import netifaces [as 别名]
# 或者: from netifaces import AF_LINK [as 别名]
def get_host_devices(iface_prefix=''):

    rst = {}

    for ifacename in netifaces.interfaces():

        if not ifacename.startswith(iface_prefix):
            continue

        addrs = netifaces.ifaddresses(ifacename)

        if netifaces.AF_INET in addrs and netifaces.AF_LINK in addrs:

            ips = [addr['addr'] for addr in addrs[netifaces.AF_INET]]

            for ip in ips:
                if is_ip4_loopback(ip):
                    break
            else:
                rst[ifacename] = {'INET': addrs[netifaces.AF_INET],
                                  'LINK': addrs[netifaces.AF_LINK]}

    return rst 
开发者ID:bsc-s2,项目名称:pykit,代码行数:25,代码来源:net.py

示例5: interface

# 需要导入模块: import netifaces [as 别名]
# 或者: from netifaces import AF_LINK [as 别名]
def interface(self):
        # TODO: Reject this idea? Maybe interfaces should be chosen without
        # regard to connection status, if NM can't be trusted.
        # In which case, tests that get a list of interfaces should just use
        # netifaces directly.
        try:
            return self._settings['connection']['interface-name']
        except KeyError:
            connection_type = self._settings['connection']['type']
            # TODO: Test this on different types.
            mac_address = self._settings[connection_type]['mac-address']
            for iface in netifaces.interfaces():
                iface_mac = netifaces.ifaddresses(iface)[netifaces.AF_LINK][0]['addr'].lower()
                if mac_address.lower() == iface_mac:
                    return iface
        raise XVEx("Couldn't find any connection interfaces") 
开发者ID:expressvpn,项目名称:expressvpn_leak_testing,代码行数:18,代码来源:network_services.py

示例6: get_if_raw_hwaddr

# 需要导入模块: import netifaces [as 别名]
# 或者: from netifaces import AF_LINK [as 别名]
def get_if_raw_hwaddr(iff):
    err = create_string_buffer(PCAP_ERRBUF_SIZE)
    devs = POINTER(pcap_if_t)()
    ret = b"\0\0\0\0\0\0"
    if pcap_findalldevs(byref(devs), err) < 0:
      return ret
    try:
      p = devs
      while p:
        if p.contents.name.endswith(iff.encode('ascii')):
          a = p.contents.addresses
          while a:
            if hasattr(socket, 'AF_LINK') and a.contents.addr.contents.sa_family == socket.AF_LINK:
              ap = a.contents.addr
              val = cast(ap, POINTER(sockaddr_dl))
              ret = bytes(val.contents.sdl_data[ val.contents.sdl_nlen : val.contents.sdl_nlen + val.contents.sdl_alen ])
            a = a.contents.next
          break
        p = p.contents.next
      return ret
    finally:
      pcap_freealldevs(devs) 
开发者ID:phaethon,项目名称:kamene,代码行数:24,代码来源:pcapdnet.py

示例7: __init__

# 需要导入模块: import netifaces [as 别名]
# 或者: from netifaces import AF_LINK [as 别名]
def __init__(self, homebrewNetifaces, ifNameStructLen):
        self.homebrewNetifaces = homebrewNetifaces
        self.ifNameStructLen = ifNameStructLen
        if self.homebrewNetifaces:
            Netifaces.AF_LINK = 1
            Netifaces.AF_INET = 2
            self.interfaceAttrs = {}
        else:
            import netifaces
            Netifaces.AF_LINK = netifaces.AF_LINK
            Netifaces.AF_INET = netifaces.AF_INET 
开发者ID:alsmith,项目名称:multicast-relay,代码行数:13,代码来源:multicast-relay.py

示例8: interfaces

# 需要导入模块: import netifaces [as 别名]
# 或者: from netifaces import AF_LINK [as 别名]
def interfaces(self):
        if self.homebrewNetifaces:
            import array
            import fcntl

            maxInterfaces = 128
            bufsiz = maxInterfaces * 40
            nullByte = b'\0'

            s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
            ifNames = array.array('B', nullByte * bufsiz)
            ifNameLen = struct.unpack('iL', fcntl.ioctl(
                s.fileno(),
                0x8912, # SIOCGIFCONF
                struct.pack('iL', bufsiz, ifNames.buffer_info()[0])
            ))[0]

            if ifNameLen % self.ifNameStructLen != 0:
                print('Do you need to set --ifNameStructLen? %s/%s ought to have a remainder of zero.' % (ifNameLen, self.ifNameStructLen))
                sys.exit(1)

            ifNames = ifNames.tostring()
            for i in range(0, ifNameLen, self.ifNameStructLen):
                name      = ifNames[i:i+16].split(nullByte, 1)[0].decode()
                if not name:
                    print('Cannot determine interface name: do you need to set --ifNameStructLen? %s/%s ought to have a remainder of zero.' % (ifNameLen, self.ifNameStructLen))
                    sys.exit(1)
                ip        = socket.inet_ntoa(fcntl.ioctl(socket.socket(socket.AF_INET, socket.SOCK_DGRAM), 0x8915, struct.pack('256s', str(name)))[20:24]) # SIOCGIFADDR
                netmask   = socket.inet_ntoa(fcntl.ioctl(socket.socket(socket.AF_INET, socket.SOCK_DGRAM), 0x891b, struct.pack('256s', str(name)))[20:24]) # SIOCGIFNETMASK
                broadcast = socket.inet_ntoa(fcntl.ioctl(socket.socket(socket.AF_INET, socket.SOCK_DGRAM), 0x8919, struct.pack('256s', str(name)))[20:24]) # SIOCGIFBRDADDR
                hwaddr    = ':'.join(['%02x' % ord(char) for char in fcntl.ioctl(socket.socket(socket.AF_INET, socket.SOCK_DGRAM), 0x8927, struct.pack('256s', str(name)))[18:24]]) # SIOCGIFHWADDR
                self.interfaceAttrs[name] = {Netifaces.AF_LINK: [{'addr': hwaddr}], Netifaces.AF_INET: [{'addr': ip, 'netmask': netmask, 'broadcast': broadcast}]}
            return self.interfaceAttrs.keys()
        else:
            import netifaces
            return netifaces.interfaces() 
开发者ID:alsmith,项目名称:multicast-relay,代码行数:38,代码来源:multicast-relay.py

示例9: get_addresses

# 需要导入模块: import netifaces [as 别名]
# 或者: from netifaces import AF_LINK [as 别名]
def get_addresses(interface):
    addrs = netifaces.ifaddresses(interface)
    link_addr = addrs[netifaces.AF_LINK]
    iface_addrs = addrs[netifaces.AF_INET]
    iface_dict = iface_addrs[0]
    link_dict = link_addr[0]
    hwaddr = link_dict.get('addr')
    iface_addr = iface_dict.get('addr')
    iface_broadcast = iface_dict.get('broadcast')
    iface_netmask = iface_dict.get('netmask')
    return hwaddr, iface_addr, iface_broadcast, iface_netmask 
开发者ID:funkandwagnalls,项目名称:pythonpentest,代码行数:13,代码来源:msfrpc_smb.py

示例10: get_local_mac

# 需要导入模块: import netifaces [as 别名]
# 或者: from netifaces import AF_LINK [as 别名]
def get_local_mac(self, iface):
        return ni.ifaddresses(iface)[ni.AF_LINK][0]['addr'] 
开发者ID:ecthros,项目名称:pina-colada,代码行数:4,代码来源:core.py

示例11: _get_mac_address_by_local_ip

# 需要导入模块: import netifaces [as 别名]
# 或者: from netifaces import AF_LINK [as 别名]
def _get_mac_address_by_local_ip(ip_addr):
    for iface in netifaces.interfaces():
        addrs = netifaces.ifaddresses(iface)
        for addr in addrs.get(netifaces.AF_INET, []):
            if addr['addr'] == ip_addr:
                return addrs[netifaces.AF_LINK][0]['addr'] 
开发者ID:cloudbase,项目名称:cloudbase-init,代码行数:8,代码来源:dhcp.py

示例12: test_get_mac_address_by_local_ip

# 需要导入模块: import netifaces [as 别名]
# 或者: from netifaces import AF_LINK [as 别名]
def test_get_mac_address_by_local_ip(self, mock_interfaces,
                                         mock_ifaddresses):
        fake_addresses = {}
        fake_addresses[netifaces.AF_INET] = [{'addr': 'fake address'}]
        fake_addresses[netifaces.AF_LINK] = [{'addr': 'fake mac'}]

        mock_interfaces.return_value = ['fake interface']
        mock_ifaddresses.return_value = fake_addresses

        response = dhcp._get_mac_address_by_local_ip('fake address')

        mock_interfaces.assert_called_once_with()
        mock_ifaddresses.assert_called_once_with('fake interface')
        self.assertEqual(fake_addresses[netifaces.AF_LINK][0]['addr'],
                         response) 
开发者ID:cloudbase,项目名称:cloudbase-init,代码行数:17,代码来源:test_dhcp.py

示例13: get_interface_mac_address

# 需要导入模块: import netifaces [as 别名]
# 或者: from netifaces import AF_LINK [as 别名]
def get_interface_mac_address(self,
                                  interface_name: str = 'eth0',
                                  exit_on_failure: bool = True,
                                  exit_code: int = 7,
                                  quiet: bool = False) -> Union[None, str]:
        """
        Get MAC address of the network interface
        :param interface_name: Network interface name (default: 'eth0')
        :param exit_on_failure: Exit in case of error (default: True)
        :param exit_code: Set exit code integer (default: 7)
        :param quiet: Quiet mode, if True no console output (default: False)
        :return: MAC address string (example: '01:23:45:67:89:0a') or None in case of error
        """
        if interface_name in self._network_interfaces_settings.keys():
            if self._network_interfaces_settings[interface_name]['mac-address'] is not None:
                return self._network_interfaces_settings[interface_name]['mac-address']

        try:
            return str(ifaddresses(interface_name)[AF_LINK][0]['addr'])

        except NameError:
            return get_mac_address(interface=interface_name)

        except ValueError:
            pass

        except KeyError:
            pass

        if not quiet:
            self.print_error('Network interface: ', interface_name, ' does not have MAC address!')
        if exit_on_failure:
            exit(exit_code)
        return None 
开发者ID:raw-packet,项目名称:raw-packet,代码行数:36,代码来源:base.py

示例14: get_device_mac

# 需要导入模块: import netifaces [as 别名]
# 或者: from netifaces import AF_LINK [as 别名]
def get_device_mac(self):
        try:
            return ifaddresses(self.interface)[AF_LINK][0]['addr']
        except KeyError:
            return "00:00:00:00:00:00" 
开发者ID:emlid,项目名称:pywificontrol,代码行数:7,代码来源:wificommon.py

示例15: get_local_port_mac

# 需要导入模块: import netifaces [as 别名]
# 或者: from netifaces import AF_LINK [as 别名]
def get_local_port_mac(self, bridge_name):
        mac_address = netifaces.ifaddresses(
            bridge_name)[netifaces.AF_LINK][0].get('addr')
        return mac_address 
开发者ID:openstack,项目名称:networking-generic-switch,代码行数:6,代码来源:test_ngs_basic_ops.py


注:本文中的netifaces.AF_LINK属性示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。