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¶
avocado.utils.network.hosts module¶
This module provides an useful API for hosts in a network.
-
class
avocado.utils.network.hosts.Host(host)¶ Bases:
objectThis 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_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.HostThis 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.HostThis 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:
objectThis 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
-
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.
-
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.
-
get_link_state()¶ 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.
-
is_link_up()¶ Check if the interface is up or not.
Returns: True or False. True if the current state is 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_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.
-
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.
-
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.BorgTracks 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