util package

Submodules

util.multinet_requests module

Multinet utility functions to communicate with the master and the worker machines

util.multinet_requests.aggregate_broadcast_response(responses)[source]

Perform an aggregation on a list of HTTP responses If all the responses status code is successful return 200 else return 500 Gather all the responses text in a list

Args:
responses (list): A list of HTTP responses
Returns:
status (int): The aggregate status code body (list): The list of all the responses text
util.multinet_requests.broadcast_cmd(worker_ip_list, worker_port_list, opcode, data=None)[source]

Broadcast a POST request to all the workers Use multiple processes to send POST requests to a specified endpoint of all the workers simultaneously.

Args:
worker_ip_list (list): A list of IP addresses to broadcast the POST request worker_port (int): The port of the workers opcode (str): The REST API endpoint topo_size (int): The number of topology switches data (dict): JSON data to go with the request
Returns:
list: A list of responses for all the POST requests performed
util.multinet_requests.dpid_offset_range(num_vms)[source]

Generate a range of dpid dpid_offset_list Every VM has allocates 1000 unique dpid offsets

Args:
num_vms (int): The number of virtual machines topo_size (int): The number of topology switches
Returns:
list: The dpid offset range
util.multinet_requests.handle_post_request(post_call, exit_on_fail=True)[source]

Handle the response of a REST request If the status code is not successful and the caller specifies so, sys.exit Else log the response text

Args:
post_call (requests.models.Response): The response to handle exit_on_fail (Optional[bool]): True -> Exit on error status code / False -> continue
util.multinet_requests.make_post_request(host_ip, host_port, route, data=None)[source]

Make a POST request Make a POST request to a remote REST server and log the response

Args:
host_ip (str): The ip of the remote REST server host_port (int): The port of the remote REST server route (str): The REST API endpoint data (dict): A dictionary or a list with any additional data
Returns:
requests.models.Response: The HTTP response for the performed request
util.multinet_requests.make_post_request_runner(host_ip, host_port, route, data, queue)[source]

Wrapper function to create a new job for each POST request. Make a POST request and put the response in a queue. Used for multiprocessing.

Args:
host_ip (str): The IP address of the REST server host_port (int): The port of the REST server route (str): The REST API endpoint data (str): Any additional JSON data queue (multiprocessing.Queue): The queue where all the responses are stored
util.multinet_requests.master_cmd(master_ip, master_port, opcode, data=None)[source]

Wrapper function to send a command to the master

Args:
master_ip (str): The IP address of the master master_port (int): The port of the master opcode (str): The REST API endpoint (the command we want to send)
Returns:
requests.models.Response: The HTTP response for the performed request
util.multinet_requests.parse_arguments()[source]

Reads the arguments passed from command line.

Command line Args:
–json-config (str): Compulsory argument. The path to the JSON configuration file. –serial-requests (bool): Optional argument. Defines if the requests will be sent from the master to the workers parallely, simultaneusly to all workers, or serially, one worker each time.
Returns:
collection: An object containing the values of all arguments.
util.multinet_requests.parse_json_conf(json_config)[source]

Parse a JSON configuration file. The path to this file is given from command line argument –json-config and passed as an argument in this function.

Args:
json_config (str): The path to the JSON configuration file
Returns:
dict: The parsed json configuration

util.netutil module

General network utilities

util.netutil.copy_directory_to_target(ipaddr, user, passwd, local_path, remote_path, remote_port=22)[source]

Copy a local directory on a remote machine.

Parameters:
  • ipaddr (str) – IP adress of the remote machine
  • user (str) – username of the remote user
  • passwd (str) – password of the remote user
  • local_path (str) – directory path from local machine to copy, full location required
  • remote_path (str) – remote destination, full location required
  • remote_port (int) – port to perform sftp from
util.netutil.create_remote_directory(ipaddr, user, passwd, remote_path, remote_port=22)[source]

Opens an ssh connection to a remote machine and creates a new directory.

Parameters:
  • ipaddr (str) – Ip adress of the remote machine
  • user (str) – username of the remote user
  • passwd (str) – password of the remote user
  • remote_path (str) – maximum number of times to connect
  • remote_port (int) – port to perform sftp from
util.netutil.isdir(path, sftp)[source]

Checks if a given remote path is a directory

Parameters:
  • path (str) – A string with the full path we want to check
  • sftp (paramiko.SSHClient) – An sftp connection object (paramico)
Returns:

True if the given path is a directory false otherwise.

Return type:

bool

util.netutil.make_remote_file_executable(ipaddr, user, passwd, remote_file, remote_port=22)[source]

Makes the remote file executable.

Parameters:
  • ipaddr (str) – Ip adress of the remote machine
  • user (str) – username of the remote user
  • passwd (str) – password of the remote user
  • remote_file (str) – remote file to make executable
  • remote_port (int) – port to perform sftp from
util.netutil.remove_remote_directory(ipaddr, user, passwd, path, remote_port=22)[source]

Removes recursively remote directories (removes all files and other sub-directories).

Parameters:
  • ipaddr (str) – Ip adress of the remote machine
  • user (str) – username of the remote user
  • passwd (str) – password of the remote user
  • remote_file (str) – remote file to make executable
  • remote_port (int) – port to perform sftp from
util.netutil.ssh_connect_or_return(ipaddr, user, passwd, maxretries, remote_port=22)[source]

Opens a connection and returns a connection object. If it fails to open a connection after a specified number of tries, it returns -1.

Parameters:
  • ipaddr (str) – Ip adress of the remote machine
  • user (str) – username of the remote user
  • passwd (str) – password of the remote user
  • maxretries (int) – maximum number of times to connect
Returns:

an ssh connection handle or -1

Return type:

paramiko.SSHClient (or -1 when failure)

util.netutil.ssh_copy_file_to_target(ipaddr, user, passwd, local_file, remote_file, remote_port=22)[source]

Copies local file on a remote machine target.

Parameters:
  • ipaddr (str) – Ip adress of the remote machine
  • user (str) – username of the remote user
  • passwd (str) – password of the remote user
  • local_file (str) – file from local machine to copy,full location required
  • remote_file (str) – remote destination, full location required i.e /tmp/foo.txt
  • remote_port (int) – port to perform sftp from
util.netutil.ssh_delete_file_if_exists(ipaddr, user, passwd, remote_file, remote_port=22)[source]

Deletes the file on e remote machine, if it exists

Parameters:
  • ipaddr (str) – Ip adress of the remote machine
  • user (str) – username of the remote user
  • passwd (str) – password of the remote user
  • remote_file (str) – remote file to remove, full path must be used.
  • remote_port (int) – port to perform sftp from
util.netutil.ssh_run_command(ssh_session, command_to_run)[source]

Runs the specified command on a remote machine

:param ssh_session : SSH session provided by paramiko to run the command :param command_to_run: Command to execute :returns: the output of the remotely executed command :rtype: tuple (stdin, stdout, stderr) :type ssh_session: paramiko.SSHClient :type command_to_run: str

Module contents