How to use _get_interface_details method in avocado

Best Python code snippet using avocado_python

interfaces.py

Source:interfaces.py Github

copy

Full Screen

...39 def __init__(self, if_name, host, if_type='Ethernet'):40 self.name = if_name41 self.if_type = if_type42 self.host = host43 def _get_interface_details(self, version=4):44 cmd = "ip -{} -j address show {}".format(version, self.name)45 output = run_command(cmd, self.host)46 try:47 result = json.loads(output)48 for item in result:49 if item.get('ifname') == self.name:50 return item51 raise NWException("Interface not found")52 except (NWException, json.JSONDecodeError):53 msg = "Unable to get IP address on interface {}".format(self.name)54 log.error(msg)55 raise NWException(msg)56 def _move_file_to_backup(self, filename, ignore_missing=True):57 destination = "{}.backup".format(filename)58 if os.path.exists(filename):59 shutil.move(filename, destination)60 else:61 if not ignore_missing:62 raise NWException("%s interface not available" % self.name)63 def _write_to_file(self, filename, values):64 self._move_file_to_backup(filename)65 with open(filename, 'w+') as fp:66 for key, value in values.items():67 fp.write("{}={}\n".format(key, value))68 def set_hwaddr(self, hwaddr):69 """Sets a Hardware Address (MAC Address) to the interface.70 This method will try to set a new hwaddr to this interface, if71 fails it will raise a NWException.72 You must have sudo permissions to run this method on a host.73 :param hwaddr: Hardware Address (Mac Address)74 """75 cmd = "ip link set dev {} address {}".format(self.name, hwaddr)76 try:77 run_command(cmd, self.self.host, sudo=True)78 except Exception as ex:79 raise NWException("Adding hw address fails: %s" % ex)80 def add_ipaddr(self, ipaddr, netmask):81 """Add an IP Address (with netmask) to the interface.82 This method will try to add a new ipaddr/netmask this interface, if83 fails it will raise a NWException.84 You must have sudo permissions to run this method on a host.85 :param ipaddr: IP Address86 :param netmask: Network mask87 """88 ip = ip_interface("{}/{}".format(ipaddr, netmask))89 cmd = 'ip addr add {} dev {}'.format(ip.compressed,90 self.name)91 try:92 run_command(cmd, self.host, sudo=True)93 except Exception as ex:94 raise NWException("Failed to add address {}".format(ex))95 def bring_down(self):96 """Shutdown the interface.97 This will shutdown the interface link. Be careful, you might lost98 connection to the host.99 You must have sudo permissions to run this method on a host.100 """101 cmd = "ip link set {} down".format(self.name)102 try:103 run_command(cmd, self.host, sudo=True)104 except Exception as ex:105 raise NWException("Failed to bring down: %s" % ex)106 def bring_up(self):107 """"Wake-up the interface.108 This will wake-up the interface link.109 You must have sudo permissions to run this method on a host.110 """111 cmd = "ip link set {} up".format(self.name)112 try:113 run_command(cmd, self.host, sudo=True)114 except Exception as ex:115 raise NWException("Failed to bring up: %s" % ex)116 def is_admin_link_up(self):117 """Check the admin link state is up or not.118 :return: True or False, True if network interface state is 'UP'119 otherwise will return False.120 """121 try:122 if 'UP' in self._get_interface_details().get('flags'):123 return True124 except (NWException, IndexError):125 raise NWException("Could not get Administrative link state.")126 return False127 def is_operational_link_up(self):128 """Check Operational link state is up or not.129 :return: True or False. True if operational link state is LOWER_UP,130 otherwise will return False.131 """132 try:133 if 'LOWER_UP' in self._get_interface_details().get('flags'):134 return True135 except (NWException, IndexError):136 raise NWException("Could not get operational link state.")137 return False138 def is_link_up(self):139 """Check if the interface is up or not.140 :return: True or False. True if admin link state and operational141 link state is up otherwise will return False.142 """143 return self.is_admin_link_up() and self.is_operational_link_up()144 def get_ipaddrs(self, version=4):145 """Get the IP addresses from a network interface.146 Interfaces can hold multiple IP addresses. This method will return a147 list with all addresses on this interface.148 :param version: Address Family Version (4 or 6). This must be a integer149 and default is 4.150 :return: IP address as string.151 """152 if version not in [4, 6]:153 raise NWException("Version {} not supported".format(version))154 try:155 details = self._get_interface_details(version)156 addr_info = details.get('addr_info')157 if addr_info:158 return [x.get('local') for x in addr_info]159 except (NWException, IndexError):160 msg = "Could not get ip addresses for {}".format(self.name)161 log.debug(msg)162 return []163 def get_hwaddr(self):164 """Get the Hardware Address (MAC) of this interface.165 This method will try to get the address and if fails it will raise a166 NWException.167 """168 cmd = "cat /sys/class/net/{}/address".format(self.name)169 try:170 return run_command(cmd, self.host)171 except Exception as ex:172 raise NWException("Failed to get hw address: {}".format(ex))173 def get_link_state(self):174 """Method used to get the current link state of this interface.175 This method will return 'up', 'down' or 'unknown', based on the176 network interface state. Or it will raise a NWException if is177 unable to get the interface state.178 """179 warnings.warn("deprecated, use existing methods: is_operational_link_up,\180 is_admin_link_up", DeprecationWarning)181 cmd = "cat /sys/class/net/{}/operstate".format(self.name)182 try:183 return run_command(cmd, self.host)184 except CmdError as e:185 msg = ('Failed to get link state. Maybe the interface is '186 'missing. {}'.format(e))187 raise NWException(msg)188 def get_mtu(self):189 """Return the current MTU value of this interface.190 This method will try to get the current MTU value, if fails will191 raise a NWException.192 """193 try:194 return self._get_interface_details().get('mtu')195 except (NWException, IndexError):196 raise NWException("Could not get MUT value.")197 def ping_check(self, peer_ip, count=2, options=None):198 """This method will try to ping a peer address (IPv4 or IPv6).199 You should provide a IPv4 or IPV6 that would like to ping. This200 method will try to ping the peer and if fails it will raise a201 NWException.202 :param peer_ip: Peer IP address (IPv4 or IPv6)203 :param count: How many packets to send. Default is 2204 :param options: ping command options. Default is None205 """206 cmd = "ping -I {} {} -c {}".format(self.name, peer_ip, count)207 if options is not None:208 cmd = "{} {}".format(cmd, options)...

Full Screen

Full Screen

enabled.py

Source:enabled.py Github

copy

Full Screen

...206 def _build_port_strings(self, ports):207 port_range_list = group_sequences(ports, are_in_sequence=self._are_in_sequence)208 port_list = []209 for port_range in port_range_list:210 first_details = self._get_interface_details(port_range[0].name)211 if len(port_range) == 1:212 port_list.append("{}{}".format(first_details.port_prefix, first_details.port))213 else:214 port_list.append("{0}{1}-{0}{2}".format(first_details.port_prefix, first_details.port, self._get_interface_details(port_range[-1].name).port))215 return _assemble_elements_on_lines(port_list, max_line_char=13)216 def _get_interface_details(self, interface_name):217 interface_descriptor = namedtuple('InterfaceDescriptor', "interface port_prefix port")218 re_port_number = re.compile('(\d/[a-zA-Z]+)(\d+)')219 interface, slot_descriptor = interface_name.split(" ")220 port_prefix, port = re_port_number.match(slot_descriptor).groups()221 return interface_descriptor(interface, port_prefix, int(port))222 def _are_in_sequence(self, a, b):223 details_a = self._get_interface_details(a.name)224 details_b = self._get_interface_details(b.name)225 return details_a.port + 1 == details_b.port and details_a.port_prefix == details_b.port_prefix226 def continue_vlan_pages(self, lines, _):227 self.write_line("\r ")228 self.write_line("")229 self.show_vlan_page(lines)230 if not self.awaiting_keystroke:231 self.show_prompt()232 def show_page(self, lines):233 lines_per_pages = 23234 line = 0235 while len(lines) > 0 and line < lines_per_pages:236 self.write_line(lines.pop(0))237 line += 1238 if len(lines) > 0:...

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 avocado 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