robottelo.ssh

Utility module to handle the shared ssh connection.

Module Contents

Classes

SSHCommandResult

Structure that returns in all ssh commands results.

SSHClient

Extended SSHClient allowing custom methods

Functions

decode_to_utf8(text)

Paramiko returns bytes object and we need to ensure it is utf-8 before

_call_paramiko_sshclient()

Call paramiko.SSHClient.

get_client(hostname=None, username=None, password=None, key_filename=None, key_string=None, timeout=None, port=22)

Returns a SSH client connected to given hostname

get_connection(hostname=None, username=None, password=None, key_filename=None, key_string=None, timeout=None, port=22)

Yield an ssh connection object.

get_sftp_session(hostname=None, username=None, password=None, key_filename=None, key_string=None, timeout=None)

Yield a SFTP session object.

add_authorized_key(key, hostname=None, username=None, password=None, key_filename=None, key_string=None, timeout=None)

Appends a local public ssh key to remote authorized keys

upload_file(local_file, remote_file, key_filename=None, key_string=None, hostname=None)

Upload a local file to a remote machine

upload_files(local_dir, remote_dir, file_search='*.txt', hostname=None, key_filename=None, key_string=None)

Upload all files from directory to a remote directory

_upload_file(sftp, local_file, remote_file)

Upload a file using existent sftp session

download_file(remote_file, local_file=None, hostname=None)

Download a remote file to the local machine. If hostname is not

command(cmd, hostname=None, output_format=None, username=None, password=None, key_filename=None, key_string=None, timeout=None, connection_timeout=None, port=22)

Executes SSH command(s) on remote hostname.

execute_command(cmd, connection, output_format=None, timeout=None, connection_timeout=None)

Execute a command via ssh in the given connection

is_ssh_pub_key(key)

Validates if a string is in valid ssh pub key format

Attributes

logger

robottelo.ssh.logger
exception robottelo.ssh.SSHCommandTimeoutError

Bases: Exception

Raised when the SSH command has not finished executing after a predefined period of time.

robottelo.ssh.decode_to_utf8(text)

Paramiko returns bytes object and we need to ensure it is utf-8 before parsing

class robottelo.ssh.SSHCommandResult(stdout=None, stderr=None, return_code=0, output_format=None)

Structure that returns in all ssh commands results.

__repr__(self)

Return repr(self).

class robottelo.ssh.SSHClient

Bases: paramiko.SSHClient

Extended SSHClient allowing custom methods

run(self, cmd, *args, **kwargs)

This method exists to allow the reuse of existing connections when running multiple ssh commands as in the following example of use:

with robottelo.ssh.get_connection() as connection:

connection.run(‘ls /tmp’) connection.run(‘another command’)

self is always passed as the connection when used in context manager only when using ssh.get_connection function.

Note: This method is named run to avoid conflicts with existing exec_command and local function execute_command.

robottelo.ssh._call_paramiko_sshclient()

Call paramiko.SSHClient.

This function does not alter the behaviour of paramiko.SSHClient. It exists solely for the sake of easing unit testing: it can be overridden for mocking purposes.

robottelo.ssh.get_client(hostname=None, username=None, password=None, key_filename=None, key_string=None, timeout=None, port=22)

Returns a SSH client connected to given hostname

Processes ssh credentials in the order: password, key_filename, ssh_key Config validation enforces one of the three must be set in settings.server

robottelo.ssh.get_connection(hostname=None, username=None, password=None, key_filename=None, key_string=None, timeout=None, port=22)

Yield an ssh connection object.

The connection will be configured with the specified arguments or will fall-back to server configuration in the configuration file.

Yield this SSH connection. The connection is automatically closed when the caller is done using it using contextlib, so clients should use the with statement to handle the object:

with get_connection() as connection:
    ...

kwargs are passed through to get_client

Returns

An SSH connection.

Return type

paramiko.SSHClient

robottelo.ssh.get_sftp_session(hostname=None, username=None, password=None, key_filename=None, key_string=None, timeout=None)

Yield a SFTP session object.

The session will be configured with the host whose hostname is passed as argument.

Yield this SFTP Session. The session is automatically closed when the caller is done using it using contextlib, so clients should use the``with`` statement to handle the object:

with get_sftp_session() as session:
...

kwargs are passed through to get_connection

robottelo.ssh.add_authorized_key(key, hostname=None, username=None, password=None, key_filename=None, key_string=None, timeout=None)

Appends a local public ssh key to remote authorized keys

refer to: remote_execution_ssh_keys provisioning template

kwargs are passed through to get_client

Parameters

key – either a file path, key string or a file-like obj to append.

robottelo.ssh.upload_file(local_file, remote_file, key_filename=None, key_string=None, hostname=None)

Upload a local file to a remote machine

Parameters
  • local_file – either a file path or a file-like object to be uploaded.

  • remote_file – a remote file path where the uploaded file will be placed.

  • hostname – target machine hostname. If not provided will be used the server.hostname from the configuration.

  • key_filename (str) – The path of the ssh private key to use when connecting to the server. If it is None key_filename from configuration’s server section will be used.

robottelo.ssh.upload_files(local_dir, remote_dir, file_search='*.txt', hostname=None, key_filename=None, key_string=None)

Upload all files from directory to a remote directory

Parameters
  • local_dir – all files from local path to be uploaded.

  • remote_dir – a remote path where the uploaded files will be placed.

  • file_search – filter only files contains the type extension

  • hostname – target machine hostname. If not provided will be used the server.hostname from the configuration.

  • key_filename (str) – The path of the ssh private key to use when connecting to the server. If it is None key_filename from configuration’s server section will be used.

robottelo.ssh._upload_file(sftp, local_file, remote_file)

Upload a file using existent sftp session

Parameters
  • sftp – sftp session object

  • local_file – either a file path or a file-like object to be uploaded.

  • remote_file – a remote file path where the uploaded file will be placed.

robottelo.ssh.download_file(remote_file, local_file=None, hostname=None)

Download a remote file to the local machine. If hostname is not provided will be used the server.

robottelo.ssh.command(cmd, hostname=None, output_format=None, username=None, password=None, key_filename=None, key_string=None, timeout=None, connection_timeout=None, port=22)

Executes SSH command(s) on remote hostname.

kwargs are passed through to get_connection

Parameters
  • cmd (str) – The command to run

  • output_format (str) – json, csv or None

  • timeout (int) – Time to wait for the ssh command to finish.

  • connection_timeout – Time to wait for establishing the connection.

robottelo.ssh.execute_command(cmd, connection, output_format=None, timeout=None, connection_timeout=None)

Execute a command via ssh in the given connection

Parameters
  • cmd – a command to be executed via ssh

  • connection – SSH Paramiko client connection

  • output_format – base|json|csv|list valid only for hammer commands

  • timeout – Time to wait for the ssh command to finish.

  • connection_timeout – Time to wait for establishing the connection.

Returns

SSHCommandResult

robottelo.ssh.is_ssh_pub_key(key)

Validates if a string is in valid ssh pub key format

Parameters

key – A string containing a ssh public key encoded in base64

Returns

Boolean