glusto.connectible module

All things remote connection and local shell.

Note

Connectible is inherited by the Glusto class and not designed to be instantiated.

class glusto.connectible.Connectible

Bases: object

The class provding remote connections and local commands.

use_controlpersist = True
user = 'root'
classmethod run(host, command, user=None, log_level=None)

Run a command on a remote host via ssh.

Parameters:
  • host (str) – The hostname of the system.
  • command (str) – The command to run on the system.
  • user (optional[str]) – The user to use for connection.
  • log_level (optional[str]) – only log stdout/stderr at this level.
Returns:

A tuple consisting of the command return code, stdout, and stderr. None on error.

Example

To run the uname command on a remote host named “bunkerhill”…

>>> from glusto.core import Glusto as g
>>> results = g.run("bunkerhill", "uname -a")
classmethod run_async(host, command, user=None, log_level=None)

Run remote commands asynchronously.

Parameters:
  • host (str) – The hostname of the system.
  • command (str) – The command to run on the system.
  • user (optional[str]) – The user to use for connection.
  • log_level (optional[str]) – only log stdout/stderr at this level.
Returns:

An open connection descriptor to be used by the calling function. None on error.

Example

To run a command asynchronously on remote hosts named “bunkerhill” and “breedshill”…

>>> from glusto.core import Glusto as g
>>> command = "ls -R /etc"
>>> proc1 = g.run_async("bunkerhill", command)
>>> proc2 = g.run_async("breedshill", command)
>>> results1 = proc1.async_communicate()
>>> results2 = proc2.async_communicate()

This can also be used to run a command against the same system asynchronously as different users…

>>> command = "ls -R /etc"
>>> proc1 = g.run_async("breedshill", command, user="howe")
>>> proc2 = g.run_async("breedshill", command, user="pigot")
>>> results1 = proc1.async_communicate()
>>> results2 = proc2.async_communicate()

Note

run_async() runs commands asynchronously, but blocks on async_communicate() and reads output sequentially. This might not be a good fit for run-and-forget commands.

classmethod run_local(command, log_level=None)

Run a command on the local management system.

Parameters:
  • command (str) – Command to run locally.
  • log_level (optional[str]) – only log stdout/stderr at this level.
Returns:

A tuple consisting of the command return code, stdout, and stderr.

Example

To run a command locally…

>>> from glusto.core import Glusto as g
>>> retcode, stdout, stderr = g.run_local("uname -a")
classmethod run_serial(hosts, command, user=None, log_level=None)

Sequentially runs a command against a list of hosts.

Parameters:
  • hosts (list) – A list of hostnames to run command against.
  • command (str) – The command to run on the system.
  • user (optional[str]) – The user to use for connection.
  • log_level (optional[str]) – only log stdout/stderr at this level.
Returns:

A dictionary of tuples containing returncode, stdout, and stderr. Labeled by the host.

Example

To run a command against a list of hosts…

>>> from glusto.core import Glusto as g
>>> hosts = ["bunkerhill", "breedshill"]
>>> results = g.run_serial(hosts, "ls -Rail /etc")
classmethod run_parallel(hosts, command, user=None, log_level=None)

Runs a command against a list of hosts in parallel.

Parameters:
  • hosts (list) – A list of hostnames to run command against.
  • command (str) – The command to run on the system.
  • user (optional[str]) – The user to use for connection.
  • log_level (optional[str]) – only log stdout/stderr at this level.
Returns:

A dictionary of tuples containing returncode, stdout, and stderr. Labeled by the host.

Example

To run a command against a list of hosts in parallel…

>>> from glusto.core import Glusto as g
>>> hosts = ["bunkerhill", "breedshill"]
>>> results = g.run_serial(hosts, "ls -Rail /etc")
classmethod upload(host, localpath, remotepath, user=None)

Uploads a file to a remote system.

Parameters:
  • host (str) – Hostname of the remote system.
  • localpath (str) – The source path for the file on the local system.
  • remotepath (str) – The target path on the remote server.
  • user (optional[str]) – The user to use for the remote connection.
Returns:

None on failure.

classmethod download(host, remotepath, localpath, user=None)

Downloads a file from a remote system.

Parameters:
  • host (str) – Hostname of the remote system.
  • remotepath (str) – The source path on the remote server.
  • localpath (str) – The target path for the file on the local system.
  • user (optional[str]) – The user to use for the remote connection.
Returns:

None on failure.

classmethod transfer(sourcehost, sourcefile, targethost, targetfile, user=None)

Transfer a file between remote systems (scp) Requires keys to be set up between remote systems.

Parameters:
  • sourcehost (str) – Hostname of the remote system copying from.
  • sourcefile (str) – The source path on a remote system.
  • targethost (str) – Hostname of the remote system copying to.
  • targetfile (str) – The target path for the file on a remote system.
  • user (optional[str]) – The user to use for the remote connection.
Returns:

Nothing

classmethod ssh_list_connections()

Display the list of existing ssh connections on stdout.

classmethod ssh_get_connections()

Retrieves the dictionary of ssh connections.

Returns:A dictionary of ssh connections.
classmethod ssh_close_connection(host, user=None)

Close an SshMachine connection.

Parameters:
  • host (str) – Hostname of the system.
  • user (optional[str]) – User to use for connection.
Returns:

Nothing

classmethod ssh_close_connections()

Close all ssh connections.

Parameters:None
Returns:Nothing
classmethod ssh_set_keyfile(keyfile)
classmethod ssh_get_keyfile()