How to use get_active_interfaces method in autotest

Best Python code snippet using autotest_python

network.py

Source:network.py Github

copy

Full Screen

...22 @param flags: the integer value of an interface's flags.23 @see /usr/include/linux/if.h for the meaning of the flags.24 """25 return flags & 126def get_active_interfaces():27 """Generator yields (active network interface name, address data) tuples.28 Address data is formatted exactly like L{netifaces.ifaddresses}, e.g.::29 ('eth0', {30 AF_LINK: [31 {'addr': '...', 'broadcast': '...'}, ],32 AF_INET: [33 {'addr': '...', 'broadcast': '...', 'netmask': '...'},34 {'addr': '...', 'broadcast': '...', 'netmask': '...'},35 ...],36 AF_INET6: [37 {'addr': '...', 'netmask': '...'},38 {'addr': '...', 'netmask': '...'},39 ...], })40 Interfaces with no IP address are ignored.41 """42 for interface in netifaces.interfaces():43 ifaddresses = netifaces.ifaddresses(interface)44 # Skip interfaces with no IPv4 or IPv6 addresses.45 inet_addr = ifaddresses.get(netifaces.AF_INET, [{}])[0].get('addr')46 inet6_addr = ifaddresses.get(netifaces.AF_INET6, [{}])[0].get('addr')47 if inet_addr or inet6_addr:48 yield interface, ifaddresses49def get_ip_addresses(ifaddresses):50 """Return all IP addresses of an interfaces.51 Returns the same structure as L{ifaddresses}, but filtered to keep52 IP addresses only.53 @param ifaddresses: a dict as returned by L{netifaces.ifaddresses} or54 the address data in L{get_active_interfaces}'s output.55 """56 results = {}57 if netifaces.AF_INET in ifaddresses:58 results[netifaces.AF_INET] = ifaddresses[netifaces.AF_INET]59 if netifaces.AF_INET6 in ifaddresses:60 # Ignore link-local IPv6 addresses (fe80::/10).61 global_addrs = [addr for addr in ifaddresses[netifaces.AF_INET6]62 if not addr['addr'].startswith('fe80:')]63 if global_addrs:64 results[netifaces.AF_INET6] = global_addrs65 return results66def get_broadcast_address(ifaddresses):67 """Return the broadcast address associated to an interface.68 @param ifaddresses: a dict as returned by L{netifaces.ifaddresses} or69 the address data in L{get_active_interfaces}'s output.70 """71 return ifaddresses[netifaces.AF_INET][0].get('broadcast', '0.0.0.0')72def get_netmask(ifaddresses):73 """Return the network mask associated to an interface.74 @param ifaddresses: a dict as returned by L{netifaces.ifaddresses} or75 the address data in L{get_active_interfaces}'s output.76 """77 return ifaddresses[netifaces.AF_INET][0].get('netmask', '')78def get_ip_address(ifaddresses):79 """Return the first IPv4 address associated to the interface.80 @param ifaddresses: a dict as returned by L{netifaces.ifaddresses} or81 the address data in L{get_active_interfaces}'s output.82 """83 return ifaddresses[netifaces.AF_INET][0]['addr']84def get_mac_address(ifaddresses):85 """86 Return the hardware MAC address for an interface in human friendly form,87 ie. six colon separated groups of two hexadecimal digits, if available;88 otherwise an empty string.89 @param ifaddresses: a dict as returned by L{netifaces.ifaddresses} or90 the address data in L{get_active_interfaces}'s output.91 """92 if netifaces.AF_LINK in ifaddresses:93 return ifaddresses[netifaces.AF_LINK][0].get('addr', '')94 return ''95def get_flags(sock, interface):96 """Return the integer value of the interface flags for the given interface.97 @param sock: a socket instance.98 @param interface: The name of the interface.99 @see /usr/include/linux/if.h for the meaning of the flags.100 """101 data = fcntl.ioctl(102 sock.fileno(), SIOCGIFFLAGS, struct.pack("256s", interface[:15]))103 return struct.unpack("H", data[16:18])[0]104def get_active_device_info(skipped_interfaces=("lo",),105 skip_vlan=True, skip_alias=True, extended=False):106 """107 Returns a dictionary containing information on each active network108 interface present on a machine.109 """110 results = []111 try:112 sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM,113 socket.IPPROTO_IP)114 for interface, ifaddresses in get_active_interfaces():115 if interface in skipped_interfaces:116 continue117 if skip_vlan and "." in interface:118 continue119 if skip_alias and ":" in interface:120 continue121 flags = get_flags(sock, interface.encode())122 if not is_up(flags):123 continue124 interface_info = {"interface": interface}125 interface_info["flags"] = flags126 speed, duplex = get_network_interface_speed(127 sock, interface.encode())128 interface_info["speed"] = speed...

Full Screen

Full Screen

interfaces.py

Source:interfaces.py Github

copy

Full Screen

...74 for x in netmask.split(':'):75 if x != '':76 res += bin(int("{0}".format(x), 16)).count("1")77 return res78def get_active_interfaces() -> List[Interface]:79 """80 list get all network interface (except the loopback one) available on the device.81 Returns82 -------83 interfaces : List[Interface]84 List of Interface85 """86 results: List[Interface] = list()87 interfaces = netifaces.interfaces()88 for interface in interfaces:89 if netifaces.ifaddresses(interface).get(netifaces.AF_INET) is not None or netifaces.ifaddresses(interface).get(90 netifaces.AF_INET6) is not None:91 iface = Interface(interface)92 for af_type, af_list in netifaces.ifaddresses(interface).items():93 if af_type == netifaces.AF_INET:94 for af_dict in af_list:95 cidr = calculateIPv4CIDR(af_dict.get("netmask"))96 iface.ipv4_interfaces.append(IPv4Interface("{0}/{1}".format(af_dict.get("addr"), cidr)))97 if af_type == netifaces.AF_INET6:98 for af_dict in af_list:99 cidr = af_dict.get("netmask").split("/")[1]100 iface.ipv6_interfaces.append(101 IPv6Interface("{0}/{1}".format(af_dict.get("addr").split('%')[0], cidr))102 )103 if af_type == netifaces.AF_LINK:104 for af_dict in af_list:105 iface.mac += af_list[0].get("addr")106 gateway_list: List[tuple] = list()107 for gateway in netifaces.gateways()[2]:108 if gateway[1] == iface.name:109 gateway_list.append(gateway)110 if len(gateway_list) != 0:111 iface.gateways = gateway_list112 results.append(iface)113 return results if len(results) > 1 else None114def get_ipv4_networks(interfaces: List[Interface] = None) -> List[IPv4Network]:115 """116 Get all IPv4 networks available in the list of Interfaces117 Parameters118 ----------119 interfaces : List[Interfaces]120 List of Interfaces (usually generated by the host config)121 Returns122 -------123 ipv4_networks : List[IPv4Network]124 List of networks125 """126 if interfaces is None:127 interfaces = get_active_interfaces()128 res_ipv4_networks: list = list()129 for interface in interfaces:130 if interface.name != "lo":131 for ipv4_interface in interface.ipv4_interfaces:132 res_ipv4_networks.append(ipv4_interface.network)133 return res_ipv4_networks134def get_ipv4_address(interfaces: List[Interface] = None) -> List[IPv4Address]:135 if interfaces is None:136 interfaces = get_active_interfaces()137 res_ipv4_address: list = list()138 for interface in interfaces:139 if interface.name != "lo":140 for ipv4_network in interface.ipv4_interfaces:141 res_ipv4_address.append(ipv4_network.ip)142 return res_ipv4_address143def get_ipv4_network_hosts(networks: List[IPv4Network] = None) -> List[IPv4Address]:144 """145 Get all IPv4 address from a list of IPv4 network146 Parameters147 ----------148 networks: List[IPv4Network]149 List of IPv4Network from which extract the IPv4 address150 networks...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run autotest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful