dkutils.ssh.remote_client module

Client to handle connections and actions executed against a remote host.

class dkutils.ssh.remote_client.CommandResult(status, stdin, stdout, stderr)

Bases: tuple

A namedtuple containing the results of command execution

Variables
  • status (int) – which contains an integer the exit code of the process on the server. Normally this should be 0

  • stdin (file-like) – a file-like object representing the stdin for the command

  • stdout (file-like) – a file-like object representing the stdout for the command

  • stderr (file-like) – a file-like object representing the stderr for the command

status

Alias for field number 0

stderr

Alias for field number 3

stdin

Alias for field number 1

stdout

Alias for field number 2

class dkutils.ssh.remote_client.RemoteClient(host, user, password=None, key_filename=None, logger=None)[source]

Bases: object

Client to interact with a remote host via SSH & SCP.

Parameters
  • host (str) – the server to connect to

  • user (str) – the username to authenticate as

  • password (str, optional) – used for password authentication

  • key_filename (str, optional) – the file name of the pem file to be used for authentication

  • logger (Python logger, optional) – python logger

__init__(host, user, password=None, key_filename=None, logger=None)[source]

Client to interact with a remote host via SSH & SCP.

Parameters
  • host (str) – the server to connect to

  • user (str) – the username to authenticate as

  • password (str, optional) – used for password authentication

  • key_filename (str, optional) – the file name of the pem file to be used for authentication

  • logger (Python logger, optional) – python logger

bulk_download(remote_path, files, local_path='')[source]

Download multiple files from remote directory.

Parameters
  • remote_path (str or pathlib.PurePath) – Target directory on the server from which to download files

  • files (List(str)) – List of remote filenames to be downloaded

  • local_path (str or pathlib.PurePath, optional) – Local destination directory of downloaded files

bulk_upload(remote_path, files)[source]

Upload multiple files to a remote directory.

Parameters
  • remote_path (str or pathlib.PurePath) – The directory to upload files to on the server

  • files (List(str)) – List of local file paths to be uploaded

disconnect()[source]

Close SSH & SCP connection.

execute_commands(commands, stream_logs=False)[source]

Execute multiple commands in succession.

Parameters
  • commands (List(str)) – List of commands as strings

  • stream_logs (Boolean, optional) – Stream logs to python logger - this exhausts stdout

Return type

list of CommandResult

Examples

>>> from dkutils.ssh.remote_client import RemoteClient
... HOST = "ec2-107-23-93-203.compute-1.amazonaws.com"
... USER = "ec2-user"
... key_filename = 'some.pem'
... client = RemoteClient(HOST, USER, key_filename=key_filename)
... result = client.execute_commands(['ls /'])[0]
... if result.status != 0:
...     for line in result.stderr:
...         print(line.rstrip())
... else:
...     for line in result.stdout:
...        print(line.rstrip())