flowserv.util.ssh module

SSH client interface for interacting with remote servers using the paramiko package.

class flowserv.util.ssh.SSHClient(hostname: str, port: Optional[int] = None, timeout: Optional[float] = None, look_for_keys: Optional[bool] = False, sep: Optional[str] = '/')

Bases: object

SSH client that allows to run remote commands and access files.

close()

Close the SSH client.

exec_cmd(command) str

Execute command on the remote server.

Returns output from STDOUT. Raises an error if command execution on the remote server failed (as indicated by the program exit code).

Parameters

command (string) – Command line string that is executed on the remote server.

Return type

string

sftp() paramiko.sftp_client.SFTPClient

Get SFTP client.

Return type

paramiko.SFTPClient

property ssh_client: paramiko.client.SSHClient

Get an active instance of the SSH Client.

Return type

paramiko.SSHClient

walk(dirpath: str) List[str]

Get recursive listing of all files in a given directory.

Returns a list of relative path expressions for files in the directory.

If dirpath does not reference a directory the result is None.

Parameters

dirpath (string) – Path to a directory on the remote server.

Return type

list of string

flowserv.util.ssh.paramiko_ssh_client(hostname: str, port: Optional[int] = None, timeout: Optional[float] = None, look_for_keys: Optional[bool] = False) paramiko.client.SSHClient

Helper function to create a paramiko SSH Client.

This separate function is primarily intended to make patching easier for unit testing.

Parameters
  • hostname (string) – Server to connect to.

  • port (int, default=None) – Server port to connect to.

  • timeout (float, default=None) – Optional timeout (in seconds) for the TCP connect.

  • look_for_keys (bool, default=False) – Set to True to enable searching for discoverable private key files in ~/.ssh/.

Return type

paramiko.SSHClient

flowserv.util.ssh.ssh_client(hostname: str, port: Optional[int] = None, timeout: Optional[float] = None, look_for_keys: Optional[bool] = False, sep: Optional[str] = '/') flowserv.util.ssh.SSHClient

Context manager for the flowserv SSHCilent.

Parameters
  • hostname (string) – Server to connect to.

  • port (int, default=None) – Server port to connect to.

  • timeout (float, default=None) – Optional timeout (in seconds) for the TCP connect.

  • look_for_keys (bool, default=False) – Set to True to enable searching for discoverable private key files in ~/.ssh/.

  • sep (string, default=’/’) – Path separator used by the remote file system.

Return type

paramiko.SSHClient

flowserv.util.ssh.walk(client: paramiko.sftp_client.SFTPClient, dirpath: str, prefix: Optional[str] = None, sep: Optional[str] = '/') List[str]

Recursively scan contents of a remote directory.

Returns a list of tuples that contain the relative sub-directory path and the file name for all files. The sub-directory path for files in the dirpath is None.

If dirpath does not reference a directory the result is None.

Parameters
  • client (paramiko.SFTPClient) – SFTP client.

  • dirpath (string) – Path to a directory on the remote server.

  • prefix (string, default=None) – Prefix path for the current (sub-)directory.

  • sep (string, default=’/’) – Path separator used by the remote file system.

Return type

list of tuples of (string, string)