本文整理汇总了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
示例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)
示例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)
示例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
示例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")
示例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)
示例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
示例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()
示例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
示例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']
示例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']
示例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)
示例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
示例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"
示例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