libcloud.compute.ssh module

Wraps multiple ways to communicate over SSH.

class libcloud.compute.ssh.BaseSSHClient(hostname, port=22, username='root', password=None, key=None, key_files=None, timeout=None)[source]

Bases: object

Base class representing a connection over SSH/SCP to a remote node.

Parameters:
  • hostname (str) – Hostname or IP address to connect to.

  • port (int) – TCP port to communicate on, defaults to 22.

  • username (str) – Username to use, defaults to root.

  • password (str) – Password to authenticate with or a password used to unlock a private key if a password protected key is used.

  • key – Deprecated in favor of key_files argument.

  • key_files (str or list) – A list of paths to the private key files to use.

close() bool[source]

Shutdown connection to the remote node.

Returns:

True if the connection has been successfully closed, False otherwise.

Return type:

bool

connect() bool[source]

Connect to the remote node over SSH.

Returns:

True if the connection has been successfully established, False otherwise.

Return type:

bool

delete(path: str) bool[source]

Delete/Unlink a file on the remote node.

Parameters:

path (str) – File path on the remote node.

Returns:

True if the file has been successfully deleted, False otherwise.

Return type:

bool

put(path: str, contents: str | bytes | None = None, chmod: int | None = None, mode: str = 'w') str[source]

Upload a file to the remote node.

Parameters:
  • path (str) – File path on the remote node.

  • contents (str) – File Contents.

  • chmod (int) – chmod file to this after creation.

  • mode (str) – Mode in which the file is opened.

Returns:

Full path to the location where a file has been saved.

Return type:

str

putfo(path, fo=None, chmod=None)[source]

Upload file like object to the remote server.

Parameters:
  • path (str) – Path to upload the file to.

  • fo (File handle or file like object.) – File like object to read the content from.

  • chmod (int) – chmod file to this after creation.

Returns:

Full path to the location where a file has been saved.

Return type:

str

run(cmd: str, timeout: float | None = None) Tuple[str, str, int][source]

Run a command on a remote node.

Parameters:

cmd (str) – Command to run.

:return list of [stdout, stderr, exit_status]

class libcloud.compute.ssh.ParamikoSSHClient(hostname, port=22, username='root', password=None, key=None, key_files=None, key_material=None, timeout=None, keep_alive=None, use_compression=False)[source]

Bases: BaseSSHClient

A SSH Client powered by Paramiko.

Authentication is always attempted in the following order:

  • The key passed in (if key is provided)

  • Any key we can find through an SSH agent (only if no password and key is provided)

  • Any “id_rsa” or “id_dsa” key discoverable in ~/.ssh/ (only if no password and key is provided)

  • Plain username/password auth, if a password was given (if password is provided)

Parameters:
  • keep_alive (int) – Optional keep alive internal (in seconds) to use.

  • use_compression (bool) – True to use compression.

CHUNK_SIZE = 4096
SLEEP_DELAY = 0.2
close()[source]

Shutdown connection to the remote node.

Returns:

True if the connection has been successfully closed, False otherwise.

Return type:

bool

connect()[source]

Connect to the remote node over SSH.

Returns:

True if the connection has been successfully established, False otherwise.

Return type:

bool

delete(path)[source]

Delete/Unlink a file on the remote node.

Parameters:

path (str) – File path on the remote node.

Returns:

True if the file has been successfully deleted, False otherwise.

Return type:

bool

put(path, contents=None, chmod=None, mode='w')[source]

Upload a file to the remote node.

Parameters:
  • path (str) – File path on the remote node.

  • contents (str) – File Contents.

  • chmod (int) – chmod file to this after creation.

  • mode (str) – Mode in which the file is opened.

Returns:

Full path to the location where a file has been saved.

Return type:

str

putfo(path, fo=None, chmod=None)[source]

Upload file like object to the remote server.

Unlike put(), this method operates on file objects and not directly on file content which makes it much more efficient for large files since it utilizes pipelining.

run(cmd: str, timeout: float | None = None) Tuple[str, str, int][source]

Note: This function is based on paramiko’s exec_command() method.

Parameters:

timeout (float) – How long to wait (in seconds) for the command to finish (optional).

exception libcloud.compute.ssh.SSHCommandTimeoutError(cmd: str, timeout: float, stdout: str | None = None, stderr: str | None = None)[source]

Bases: Exception

Exception which is raised when an SSH command times out.

class libcloud.compute.ssh.ShellOutSSHClient(hostname, port=22, username='root', password=None, key=None, key_files=None, timeout=None)[source]

Bases: BaseSSHClient

This client shells out to “ssh” binary to run commands on the remote server.

Note: This client should not be used in production.

Parameters:
  • hostname (str) – Hostname or IP address to connect to.

  • port (int) – TCP port to communicate on, defaults to 22.

  • username (str) – Username to use, defaults to root.

  • password (str) – Password to authenticate with or a password used to unlock a private key if a password protected key is used.

  • key – Deprecated in favor of key_files argument.

  • key_files (str or list) – A list of paths to the private key files to use.

close()[source]

Shutdown connection to the remote node.

Returns:

True if the connection has been successfully closed, False otherwise.

Return type:

bool

connect()[source]

This client doesn’t support persistent connections establish a new connection every time “run” method is called.

delete(path)[source]

Delete/Unlink a file on the remote node.

Parameters:

path (str) – File path on the remote node.

Returns:

True if the file has been successfully deleted, False otherwise.

Return type:

bool

put(path, contents=None, chmod=None, mode='w')[source]

Upload a file to the remote node.

Parameters:
  • path (str) – File path on the remote node.

  • contents (str) – File Contents.

  • chmod (int) – chmod file to this after creation.

  • mode (str) – Mode in which the file is opened.

Returns:

Full path to the location where a file has been saved.

Return type:

str

putfo(path, fo=None, chmod=None)[source]

Upload file like object to the remote server.

Parameters:
  • path (str) – Path to upload the file to.

  • fo (File handle or file like object.) – File like object to read the content from.

  • chmod (int) – chmod file to this after creation.

Returns:

Full path to the location where a file has been saved.

Return type:

str

run(cmd, timeout=None)[source]

Run a command on a remote node.

Parameters:

cmd (str) – Command to run.

:return list of [stdout, stderr, exit_status]