Пример #1
0
    def remove_all_vlans(self):
        """Remove all VLANs of this interface.

        This method will remove all the VLAN interfaces associated by the
        interface. If it fails, the method will raise a NWException.
        """
        try:
            for v in self.vlans.values():
                cmd = "ip link delete {}".format(v)
                run_command(cmd, self.host, sudo=True)
        except Exception as ex:
            raise NWException("Failed to remove VLAN interface: {}".format(ex))
Пример #2
0
    def bring_up(self):
        """"Wake-up the interface.

        This will wake-up the interface link.

        You must have sudo permissions to run this method on a host.
        """
        cmd = "ip link set {} up".format(self.name)
        try:
            run_command(cmd, self.host, sudo=True)
        except Exception as ex:
            raise NWException("Failed to bring up: %s" % ex)
Пример #3
0
    def bring_down(self):
        """Shutdown the interface.

        This will shutdown the interface link. Be careful, you might lost
        connection to the host.

        You must have sudo permissions to run this method on a host.
        """

        cmd = "ip link set {} down".format(self.name)
        try:
            run_command(cmd, self.host, sudo=True)
        except Exception as ex:
            raise NWException("Failed to bring down: %s" % ex)
Пример #4
0
    def remove_link(self):
        """Deletes virtual interface link.

        This method will try to delete the virtual device link and the
        interface will no more be listed with 'ip a' and if fails it
        will raise a NWException. Be careful, you can lost connection.

        You must have sudo permissions to run this method on a host.
        """
        cmd = 'ip link del dev {}'.format(self.name)
        try:
            run_command(cmd, self.host, sudo=True)
        except Exception as ex:
            msg = 'Failed to delete link. {}'.format(ex)
            raise NWException(msg)
Пример #5
0
    def set_hwaddr(self, hwaddr):
        """Sets a Hardware Address (MAC Address) to the interface.

        This method will try to set a new hwaddr to this interface, if
        fails it will raise a NWException.

        You must have sudo permissions to run this method on a host.

        :param hwaddr: Hardware Address (Mac Address)
        """
        cmd = "ip link set dev {} address {}".format(self.name, hwaddr)
        try:
            run_command(cmd, self.self.host, sudo=True)
        except Exception as ex:
            raise NWException("Adding hw address fails: %s" % ex)
Пример #6
0
    def is_available(self):
        """Check if interface is available.

        This method checks if the interface is available.

        rtype: bool
        """
        cmd = 'ip link show dev {}'.format(self.name)
        try:
            run_command(cmd, self.host)
            return True
        except Exception as ex:
            msg = "Interface {} is not available. {}".format(self.name, ex)
            LOG.debug(msg)
            return False
Пример #7
0
    def is_bond(self):
        """Check if interface is a bonding device.

        This method checks if the interface is a bonding device or not.

        rtype: bool
        """
        cmd = 'cat /proc/net/bonding/{}'.format(self.name)
        try:
            run_command(cmd, self.host)
            return True
        except Exception as ex:
            msg = "{} is not a bond device. {}".format(self.name, ex)
            LOG.debug(msg)
            return False
Пример #8
0
    def is_bond(self):
        """Check if interface is a bonding device.

        This method checks if the interface is a bonding device or not.

        rtype: bool
        """
        cmd = f"cat /proc/net/bonding/{self.name}"
        try:
            run_command(cmd, self.host)
            return True
        except Exception as ex:
            msg = f"{self.name} is not a bond device. {ex}"
            LOG.debug(msg)
            return False
Пример #9
0
    def is_available(self):
        """Check if interface is available.

        This method checks if the interface is available.

        rtype: bool
        """
        cmd = f"ip link show dev {self.name}"
        try:
            run_command(cmd, self.host)
            return True
        except Exception as ex:
            msg = f"Interface {self.name} is not available. {ex}"
            LOG.debug(msg)
            return False
Пример #10
0
    def flush_ipaddr(self):
        """Flush all the IP address for this interface.

        This method will try to flush the ip address from this interface
        and if fails it will raise a NWException. Be careful, you can
        lost connection.

        You must have sudo permissions to run this method on a host.
        """
        cmd = f"ip addr flush dev {self.name}"
        try:
            run_command(cmd, self.host, sudo=True)
        except Exception as ex:
            msg = f"Failed to flush ipaddr. {ex}"
            raise NWException(msg)
Пример #11
0
    def remove_ipaddr(self, ipaddr, netmask):
        """Removes an IP address from this interface.

        This method will try to remove the address from this interface
        and if fails it will raise a NWException. Be careful, you can
        lost connection.

        You must have sudo permissions to run this method on a host.
        """
        ip = ip_interface(f"{ipaddr}/{netmask}")
        cmd = f"ip addr del {ip.compressed} dev {self.name}"
        try:
            run_command(cmd, self.host, sudo=True)
        except Exception as ex:
            msg = f"Failed to remove ipaddr. {ex}"
            raise NWException(msg)
Пример #12
0
    def remove_ipaddr(self, ipaddr, netmask):
        """Removes an IP address from this interface.

        This method will try to remove the address from this interface
        and if fails it will raise a NWException. Be careful, you can
        lost connection.

        You must have sudo permissions to run this method on a host.
        """
        ip = ip_interface("{}/{}".format(ipaddr, netmask))
        cmd = 'ip addr del {} dev {}'.format(ip.compressed, self.name)
        try:
            run_command(cmd, self.host, sudo=True)
        except Exception as ex:
            msg = 'Failed to remove ipaddr. {}'.format(ex)
            raise NWException(msg)
Пример #13
0
    def add_vlan_tag(self, vlan_num, vlan_name=None):
        """Configure 802.1Q VLAN tagging to the interface.

        This method will attempt to add a VLAN tag to this interface. If it
        fails, the method will raise a NWException.

        :param vlan_num: VLAN ID
        :param vlan_name: option to name VLAN interface, by default it is named
                          <interface_name>.<vlan_num>
        """

        vlan_name = vlan_name or "{}.{}".format(self.name, vlan_num)
        cmd = "ip link add link {} name {} type vlan id {}".format(
            self.name, vlan_name, vlan_num)
        try:
            run_command(cmd, self.host, sudo=True)
        except Exception as ex:
            raise NWException("Failed to add VLAN tag: {}".format(ex))
Пример #14
0
 def _get_bondinterface_details(self):
     cmd = "cat /sys/class/net/{}/bonding/mode \
            /sys/class/net/{}/bonding/slaves".format(self.name, self.name)
     try:
         mode, slaves = run_command(cmd, self.host).splitlines()
         return {'mode': mode.split(), 'slaves': slaves.split()}
     except Exception:
         raise NWException(
             "Slave interface not found for the bond {}".format(self.name))
Пример #15
0
    def add_ipaddr(self, ipaddr, netmask):
        """Add an IP Address (with netmask) to the interface.

        This method will try to add a new ipaddr/netmask this interface, if
        fails it will raise a NWException.

        You must have sudo permissions to run this method on a host.

        :param ipaddr: IP Address
        :param netmask: Network mask
        """

        ip = ip_interface("{}/{}".format(ipaddr, netmask))
        cmd = 'ip addr add {} dev {}'.format(ip.compressed, self.name)
        try:
            run_command(cmd, self.host, sudo=True)
        except Exception as ex:
            raise NWException("Failed to add address {}".format(ex))
Пример #16
0
 def _get_bondinterface_details(self):
     cmd = (f"cat /sys/class/net/{self.name}/bonding/mode "
            f"/sys/class/net/{self.name}/bonding/slaves")
     try:
         mode, slaves = run_command(cmd, self.host).splitlines()
         return {"mode": mode.split(), "slaves": slaves.split()}
     except Exception:
         raise NWException(f"Slave interface not found for "
                           f"the bond {self.name}")
Пример #17
0
    def ping_check(self, peer_ip, count=2, options=None):
        """This method will try to ping a peer address (IPv4 or IPv6).

        You should provide a IPv4 or IPV6 that would like to ping. This
        method will try to ping the peer and if fails it will raise a
        NWException.

        :param peer_ip: Peer IP address (IPv4 or IPv6)
        :param count: How many packets to send. Default is 2
        :param options: ping command options. Default is None
        """
        cmd = "ping -I {} {} -c {}".format(self.name, peer_ip, count)
        if options is not None:
            cmd = "{} {}".format(cmd, options)
        try:
            run_command(cmd, self.host)
        except Exception as ex:
            raise NWException("Failed to ping: {}".format(ex))
Пример #18
0
    def set_mtu(self, mtu, timeout=30):
        """Sets a new MTU value to this interface.

        This method will try to set a new MTU value to this interface,
        if fails it will raise a NWException. Also it will wait until
        the Interface is up before returning or until timeout be
        reached.

        You must have sudo permissions to run this method on a host.

        :param mtu:  mtu size that need to be set. This must be an int.
        :param timeout: how many seconds to wait until the interface is
                        up again. Default is 30.
        """
        cmd = "ip link set %s mtu %s" % (self.name, mtu)
        run_command(cmd, self.host, sudo=True)
        wait_for(self.is_link_up, timeout=timeout)
        if int(mtu) != self.get_mtu():
            raise NWException("Failed to set MTU.")
Пример #19
0
    def get_hwaddr(self):
        """Get the Hardware Address (MAC) of this interface.

        This method will try to get the address and if fails it will raise a
        NWException.
        """
        cmd = "cat /sys/class/net/{}/address".format(self.name)
        try:
            return run_command(cmd, self.host)
        except Exception as ex:
            raise NWException("Failed to get hw address: {}".format(ex))
Пример #20
0
    def interfaces(self):
        cmd = 'ls /sys/class/net'
        try:
            names = run_command(cmd, self).split()
        except Exception as ex:
            raise NWException(f"Failed to get interfaces: {ex}")

        if "bonding_masters" in names:
            names.remove("bonding_masters")

        return [NetworkInterface(if_name=name, host=self) for name in names]
Пример #21
0
    def remove_vlan_by_tag(self, vlan_num):
        """Remove the VLAN of the interface by tag number.

        This method will try to remove the VLAN tag of this interface. If it fails,
        the method will raise a NWException.

        :param vlan_num: VLAN ID
        :return: True or False, True if it found the VLAN interface and removed
                 it successfully, otherwise it will return False.
        """
        if str(vlan_num) in self.vlans:
            vlan_name = self.vlans[str(vlan_num)]
        else:
            return False
        cmd = "ip link delete {}".format(vlan_name)

        try:
            run_command(cmd, self.host, sudo=True)
            return True
        except Exception as ex:
            raise NWException("Failed to remove VLAN interface: {}".format(ex))
Пример #22
0
    def get_default_route_interface(self):
        """Get a list of default routes interfaces

        :return: list of interface names
        """
        cmd = "ip -j route list default"
        output = run_command(cmd, self)
        try:
            result = json.loads(output)
            return [str(item['dev']) for item in result]
        except Exception as ex:
            raise NWException(f"could not get default route interface name:"
                              f" {ex}")
Пример #23
0
 def _get_interface_details(self, version=None):
     cmd = "ip -j link show {}".format(self.name)
     if version:
         cmd = "ip -{} -j address show {}".format(version, self.name)
     output = run_command(cmd, self.host)
     try:
         result = json.loads(output)
         for item in result:
             if item.get('ifname') == self.name:
                 return item
         raise NWException("Interface not found")
     except (NWException, json.JSONDecodeError):
         msg = "Unable to get the details of interface {}".format(self.name)
         LOG.error(msg)
         raise NWException(msg)
Пример #24
0
    def are_packets_lost(self, peer_ip, options=None, sudo=False):
        """Check packet loss that occurs during ping.

        Function returns True for 0% packet loss and False
        if packet loss occurs.

        :param peer_ip: Peer IP address (IPv4 or IPv6)
        :param options: Type is List. Options such as -c, -f. Default is None
        :param sudo: If sudo permissions are needed. Default is False
        """
        cmd = f"ping -I {self.name} {peer_ip}"
        cmd = f"{cmd} "
        if options is not None:
            for elem in options:
                cmd += f"{elem} "
        try:
            output = run_command(cmd, self.host, sudo=sudo)
            if "0% packet loss" not in output:
                return False
            return True
        except Exception as ex:
            msg = f"Failed to ping. {ex}"
            raise NWException(msg)