avocado.utils.network package

Submodules

avocado.utils.network.common module

avocado.utils.network.common.run_command(command, host, sudo=False)

avocado.utils.network.exceptions module

exception avocado.utils.network.exceptions.NWException

Bases: Exception

Base Exception Class for all exceptions

avocado.utils.network.hosts module

This module provides an useful API for hosts in a network.

class avocado.utils.network.hosts.Host(host)

Bases: object

This class represents a base Host and shouldn’t be instantiated.

Use one of the child classes (LocalHost or RemoteHost).

During the initialization of a child, all interfaces will be detected and available via interfaces attribute. This could be accessed on LocalHost and RemoteHost instances.

So, for instance, you could have a local and a remote host:

remote = RemoteHost(host='foo', port=22,
                    username='foo', password='bar')
local = LocalHost()

You can iterate over the network interfaces of any host:

for i in remote.interfaces:
    print(i.name, i.is_link_up())
get_default_route_interface()

Get a list of default routes interfaces

Returns:list of interface names
get_interface_by_ipaddr(ipaddr)

Return an interface that has a specific ipaddr.

interfaces
class avocado.utils.network.hosts.LocalHost(host='localhost')

Bases: avocado.utils.network.hosts.Host

This class represents a local host and inherit from Host.

You should use this class when trying to get information about your localhost.

Example:

local = LocalHost()
class avocado.utils.network.hosts.RemoteHost(host, username, port=22, key=None, password=None)

Bases: avocado.utils.network.hosts.Host

This class represents a remote host and inherit from Host.

You must provide at least an username to establish a connection.

Example with password:

remote = RemoteHost(host=’192.168.0.1’,
port=22, username=’foo’, password=’bar’)

You can also provide a key instead of a password.

avocado.utils.network.interfaces module

class avocado.utils.network.interfaces.NetworkInterface(if_name, host, if_type='Ethernet')

Bases: object

This class represents a network card interface (NIC).

An “NetworkInterface” is attached to some host. This could be an instance of LocalHost or RemoteHost. If a RemoteHost then all commands will be executed on a remote_session (host.remote_session). Otherwise will be executed locally.

Here you will find a few methods to perform basic operations on a NIC.

add_ipaddr(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.

Parameters:
  • ipaddr – IP Address
  • netmask – Network mask
add_vlan_tag(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.

Parameters:
  • vlan_num – VLAN ID
  • vlan_name – option to name VLAN interface, by default it is named <interface_name>.<vlan_num>
bring_down()

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.

bring_up()

“Wake-up the interface.

This will wake-up the interface link.

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

config_filename
get_hwaddr()

Get the Hardware Address (MAC) of this interface.

This method will try to get the address and if fails it will raise a NWException.

get_ipaddrs(version=4)

Get the IP addresses from a network interface.

Interfaces can hold multiple IP addresses. This method will return a list with all addresses on this interface.

Parameters:version – Address Family Version (4 or 6). This must be a integer and default is 4.
Returns:IP address as string.

Method used to get the current link state of this interface.

This method will return ‘up’, ‘down’ or ‘unknown’, based on the network interface state. Or it will raise a NWException if is unable to get the interface state.

get_mtu()

Return the current MTU value of this interface.

This method will try to get the current MTU value, if fails will raise a NWException.

Check the admin link state is up or not.

Returns:True or False, True if network interface state is ‘UP’ otherwise will return False.
is_available()

Check if interface is available.

This method checks if the interface is available.

rtype: bool

is_bond()

Check if interface is a bonding device.

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

rtype: bool

Check if the interface is up or not.

Returns:True or False. True if admin link state and operational link state is up otherwise will return False.

Check Operational link state is up or not.

Returns:True or False. True if operational link state is LOWER_UP, otherwise will return False.
ping_check(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.

Parameters:
  • peer_ip – Peer IP address (IPv4 or IPv6)
  • count – How many packets to send. Default is 2
  • options – ping command options. Default is None
remove_all_vlans()

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.

remove_cfg_file()

Remove any config files that is created as a part of the test

remove_ipaddr(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.

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.

remove_vlan_by_tag(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.

Parameters:vlan_num – VLAN ID
Returns:True or False, True if it found the VLAN interface and removed it successfully, otherwise it will return False.
restore_from_backup()

Revert interface file from backup.

This method checks if a backup version is available for given interface then it copies backup file to interface file in /sysfs path.

save(ipaddr, netmask)

Save current interface IP Address to the system configuration file.

If the ipaddr is valid (currently being used by the interface) this will try to save the current settings into /etc/. This check is necessary to avoid inconsistency. Before save, you should add_ipaddr, first.

Currently, only RHEL, Fedora and SuSE are supported. And this will create a backup file of your current configuration if found.

:param ipaddr : IP Address which need to configure for interface :param netmask: Network mask which is associated to the provided IP

set_hwaddr(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.

Parameters:hwaddr – Hardware Address (Mac Address)
set_mtu(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.

Parameters:
  • mtu – mtu size that need to be set. This must be an int.
  • timeout – how many seconds to wait until the interface is up again. Default is 30.
vlans

Return all interface’s VLAN.

This is a dict were key is the VLAN number and the value is the name of the VLAN interface.

rtype: dict

avocado.utils.network.ports module

Module with network related utility functions

avocado.utils.network.ports.FAMILIES = (<AddressFamily.AF_INET: 2>, <AddressFamily.AF_INET6: 10>)

Families taken into account in this class

avocado.utils.network.ports.PROTOCOLS = (<SocketKind.SOCK_STREAM: 1>, <SocketKind.SOCK_DGRAM: 2>)

Protocols taken into account in this class

class avocado.utils.network.ports.PortTracker

Bases: avocado.utils.data_structures.Borg

Tracks ports used in the host machine.

find_free_port(start_port=None)
register_port(port)
release_port(port)
avocado.utils.network.ports.find_free_port(start_port=1024, end_port=65535, address='localhost', sequent=False)

Return a host free port in the range [start_port, end_port].

Parameters:
  • start_port – header of candidate port range, defaults to 1024
  • end_port – ender of candidate port range, defaults to 65535
  • address – Socket address to bind or connect
  • sequent – Find port sequentially, random order if it’s False
Return type:

int or None if no free port found

avocado.utils.network.ports.find_free_ports(start_port, end_port, count, address='localhost', sequent=False)

Return count of host free ports in the range [start_port, end_port].

Parameters:
  • start_port – header of candidate port range
  • end_port – ender of candidate port range
  • count – Initial number of ports known to be free in the range.
  • address – Socket address to bind or connect
  • sequent – Find port sequentially, random order if it’s False
avocado.utils.network.ports.is_port_free(port, address)

Return True if the given port is available for use.

Currently we only check for TCP/UDP connections on IPv4/6

Parameters:
  • port – Port number
  • address – Socket address to bind or connect

Module contents