本文整理汇总了Python中ifaddr.get_adapters方法的典型用法代码示例。如果您正苦于以下问题:Python ifaddr.get_adapters方法的具体用法?Python ifaddr.get_adapters怎么用?Python ifaddr.get_adapters使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ifaddr
的用法示例。
在下文中一共展示了ifaddr.get_adapters方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_addresses
# 需要导入模块: import ifaddr [as 别名]
# 或者: from ifaddr import get_adapters [as 别名]
def get_addresses():
"""
Get all IP addresses.
Returns list of addresses.
"""
addresses = set()
for iface in ifaddr.get_adapters():
for addr in iface.ips:
# Filter out link-local addresses.
if addr.is_IPv4:
ip = addr.ip
if not ip.startswith('169.254.'):
addresses.add(ip)
elif addr.is_IPv6:
# Sometimes, IPv6 addresses will have the interface name
# appended, e.g. %eth0. Handle that.
ip = addr.ip[0].split('%')[0].lower()
if not ip.startswith('fe80:'):
addresses.add('[{}]'.format(ip))
return sorted(list(addresses))
示例2: get_ip_for_interface
# 需要导入模块: import ifaddr [as 别名]
# 或者: from ifaddr import get_adapters [as 别名]
def get_ip_for_interface(interface_name, ipv6=False):
"""
Get the ip address in IPv4 or IPv6 for a specific network interface
:param str interface_name: declares the network interface name for which the ip should be accessed
:param bool ipv6: Boolean indicating if the ipv6 address should be retrieved
:return: IPv4Address or IPv6Address object or None
"""
def get_interface_by_name(name):
for interface in ifaddr.get_adapters():
if interface.name == name:
return interface
return None
interface = get_interface_by_name(interface_name)
if interface is None:
return None
for ip in interface.ips:
if ip.is_IPv6 and ipv6:
return ipaddress.IPv6Address(ip.ip[0]) # first of (ip, flowinfo, scope_id) tuple
if ip.is_IPv4 and not ipv6:
return ipaddress.IPv4Address(ip.ip)
return None
示例3: get_local_ip
# 需要导入模块: import ifaddr [as 别名]
# 或者: from ifaddr import get_adapters [as 别名]
def get_local_ip(host):
"""
The primary ifaddr based approach, tries to guess the local ip from the cc ip,
by comparing the subnet of ip-addresses of all the local adapters to the subnet of the cc ip.
This should work on all platforms, but requires the catt box and the cc to be on the same subnet.
As a fallback we use a socket based approach, that does not suffer from this limitation, but
might not work on all platforms.
"""
host_ipversion = type(ipaddress.ip_address(host))
for adapter in ifaddr.get_adapters():
for adapter_ip in adapter.ips:
aip = adapter_ip.ip[0] if isinstance(adapter_ip.ip, tuple) else adapter_ip.ip
try:
if not isinstance(ipaddress.ip_address(aip), host_ipversion):
continue
except ValueError:
continue
ipt = [(ip, adapter_ip.network_prefix) for ip in (aip, host)]
catt_net, cc_net = [ipaddress.ip_network("{0}/{1}".format(*ip), strict=False) for ip in ipt]
if catt_net == cc_net:
return aip
else:
continue
try:
return [
(s.connect(("8.8.8.8", 53)), s.getsockname()[0], s.close())
for s in [socket.socket(socket.AF_INET, socket.SOCK_DGRAM)]
][0][1]
except OSError:
return None
示例4: interfaces
# 需要导入模块: import ifaddr [as 别名]
# 或者: from ifaddr import get_adapters [as 别名]
def interfaces():
adapters = ifaddr.get_adapters(include_unconfigured=True)
return [a.name for a in adapters]
示例5: test_get_adapters_contains_localhost
# 需要导入模块: import ifaddr [as 别名]
# 或者: from ifaddr import get_adapters [as 别名]
def test_get_adapters_contains_localhost(self):
found = False
adapters = ifaddr.get_adapters()
for adapter in adapters:
for ip in adapter.ips:
if ip.ip == "127.0.0.1":
found = True
self.assertTrue(found, "No adapter has IP 127.0.0.1: %s" % str(adapters))
示例6: __init__
# 需要导入模块: import ifaddr [as 别名]
# 或者: from ifaddr import get_adapters [as 别名]
def __init__(self,
admin_only: bool = True,
available_platforms: List[str] = ['Linux', 'Darwin', 'Windows']) -> None:
"""
Init
"""
# Check user is admin/root
if admin_only:
self.check_user()
# Check platform
self.check_platform(available_platforms=available_platforms)
# If current platform is Windows get network interfaces settings
if self.get_platform().startswith('Windows'):
self._windows_adapters = get_adapters()
init(convert=True)
self.cINFO: str = Style.BRIGHT + Fore.BLUE
self.cERROR: str = Style.BRIGHT + Fore.RED
self.cSUCCESS: str = Style.BRIGHT + Fore.GREEN
self.cWARNING: str = Style.BRIGHT + Fore.YELLOW
self.cEND: str = Style.RESET_ALL
self.c_info: str = self.cINFO + '[*]' + self.cEND + ' '
self.c_error: str = self.cERROR + '[-]' + self.cEND + ' '
self.c_success: str = self.cSUCCESS + '[+]' + self.cEND + ' '
self.c_warning: str = self.cWARNING + '[!]' + self.cEND + ' '
self.lowercase_letters: str = 'abcdefghijklmnopqrstuvwxyz'
self.uppercase_letters: str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
self.digits: str = '0123456789'
# endregion
# region Output functions
示例7: get_ip_for_interface
# 需要导入模块: import ifaddr [as 别名]
# 或者: from ifaddr import get_adapters [as 别名]
def get_ip_for_interface(interface_name, ipv6=False):
"""
Get the ip address in IPv4 or IPv6 for a specific network interface
:param str interface_name: declares the network interface name for which the ip should be accessed
:param bool ipv6: Boolean indicating if the ipv6 address should be retrieved
:return: IPv4Address or IPv6Address object or None
"""
def get_interface_by_name(name):
for interface in ifaddr.get_adapters():
if interface.name == name:
return interface
return None
interface = get_interface_by_name(interface_name)
if interface is None:
return None
for ip in interface.ips:
if ip.is_IPv6 and ipv6:
return ipaddress.IPv6Address(ip.ip[0]) # first of (ip, flowinfo, scope_id) tuple
if ip.is_IPv4 and not ipv6:
return ipaddress.IPv4Address(ip.ip)
return None
示例8: get_ip_address
# 需要导入模块: import ifaddr [as 别名]
# 或者: from ifaddr import get_adapters [as 别名]
def get_ip_address(interface_name):
adapters = ifaddr.get_adapters()
for adapter in adapters:
if adapter.name == interface_name or adapter.nice_name == interface_name:
for ip in adapter.ips:
if ":" not in ip.ip[0]: # We only want ipv4
return ip.ip
return "0.0.0.0"
示例9: get_loopback
# 需要导入模块: import ifaddr [as 别名]
# 或者: from ifaddr import get_adapters [as 别名]
def get_loopback():
import ifaddr
for adapter in ifaddr.get_adapters():
if adapter.name.startswith('lo'):
return adapter.name
return None
示例10: get_interface_list
# 需要导入模块: import ifaddr [as 别名]
# 或者: from ifaddr import get_adapters [as 别名]
def get_interface_list():
active_interfaces = defaultdict(list)
active_v4_interfaces = defaultdict(list)
for adapter in ifaddr.get_adapters():
for ipaddress in adapter.ips:
# If it's ipv4, it's a string. ipv6 is a tuple
if isinstance(ipaddress.ip, str) and ipaddress.ip not in LOOPBACK:
active_v4_interfaces[adapter.nice_name].append(ipaddress.ip)
# If there is no take ipv6 instead
if not active_v4_interfaces:
for adapter in ifaddr.get_adapters():
for ipaddress in adapter.ips:
# If it's ipv4, it's a string. ipv6 is a tuple
if not isinstance(ipaddress.ip, str) and ipaddress.ip[0] not in LOOPBACK:
active_interfaces[adapter.nice_name].append(ipaddress.ip[0])
else:
active_interfaces = active_v4_interfaces
interface_list = []
with concurrent.futures.ThreadPoolExecutor(max_workers = len(active_interfaces)) as executor:
app_exec_pool = [executor.submit(get_interface_data, interface_name, active_interfaces[interface_name][0]) for
interface_name in active_interfaces]
for future in concurrent.futures.as_completed(app_exec_pool):
interface_list.append(future.result())
return interface_list