本文整理匯總了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