Connection multi classes

This file describes the utility wrapper for multiple connections.

ssh_utilities.multi_connection

The main module with toplevel MultiConnection class.

The connection class is the main public class that initializes local or remote connection arrays of classes as needed abcd on input arguments.

class ssh_utilities.multi_connection.MultiConnection(ssh_servers: Union[List[str], str], local: Union[List[bool], bool] = False, quiet: bool = False, thread_safe: Union[List[bool], bool] = False)

Bases: ssh_utilities.multi_connection._dict_interface.DictInterface, ssh_utilities.multi_connection._persistence.Pesistence, ssh_utilities.abc._connection.ConnectionABC

Wrapper for multiple connections.

All methods work exactly the same as for single connections, only now they return Generators instead of their respective values. Can be used to prallelize connections or to share one ssh connection between multiple classes

Parameters:
  • ssh_servers (Union[List[str], str]) – list od ssh srevers to connect to
  • local (Union[List[bool], bool], optional) – bool or list of bools specifying if the respective connection(s), should be local or remote by default False
  • quiet (bool, optional) – bool or a list of bools specifing if login messages should be printed, on individula connections initialization by default False
  • thread_safe (Union[List[bool], bool], optional) – bool or a list of bools specifying if respective connection(s) should, be made thead safe or not by default False
  • share_connection (Union[List[int], int], optional) – Not implemented, by default 1
classmethod add_hosts(hosts: Union[_HOSTS, List[_HOSTS]])

Add or override availbale host read fron ssh config file.

You can use supplied config parser to parse some externaf ssh config file.

Parameters:hosts (Union[_HOSTS, List[_HOSTS]]) – dictionary or a list of dictionaries containing keys: user, hostname and identityfile

See also

:func:ssh_utilities.config_parser

builtins

Inner class providing access to substitutions for python builtins.

Type:abc.Builtins
classmethod get_available_hosts() → List[str]

List all elegible hosts for connection from ~/.ssh/config.

Returns:list of available hosts
Return type:List[str]
os

Inner class providing access to substitutions for python os module.

Type:abc.Os
pathlib

Inner class providing access to substitutions for pathlib module.

Type:abc.Pathlib
shutil

Inner class providing access to substitutions for shutil module.

Type:abc.Shutil
subprocess

Inner class providing access to substitutions for subprocess module.

Type:abc.Subprocess
to_dict() → Dict[int, Dict[str, Union[str, bool, int, None]]]

Saves all the importatnt info from object to dictonary.

Returns:dictionary representing the object
Return type:Dict[int, Dict[str, Optional[Union[str, bool, int, None]]]]

ssh_utilities.multi_connection._delegated

Module that provides delegation callables.

These callables defer calls from MultiConnection to individual connections.

ssh_utilities.multi_connection._delegated.Inner(abc_parent: ABCs, multi_connection: MultiConnection)

Class implementing the inner classes of connection object.

Reimplementation of abstract methods is done auto-magically. This function is esentially a factory for the inner classes of connection.

This makes the class hierarchy ‘transposed’ instead of this:

>>> [Connection1.method1(), Connection2.method1()]
>>> [Connection1.method2(), Connection2.method2()]

we now essentialy have:

>>> method1[Connection1, Connection2]
>>> method2[Connection1, Connection2]

The methods become classes and each one holds reference to all open connections

Parameters:
  • abc_parent (ABCs) – The abstract parent that is going to be reimplemented
  • multi_connection (MultiConnection) – reference to MultiConnection object

See also

ssh_utilities.multi_conncection.MultiConnection

ssh_utilities.multi_connection._dict_interface

Module providing dictionary interface for multi_connection.

class ssh_utilities.multi_connection._dict_interface.DictInterface

Bases: collections.abc.MutableMapping

Class providing dictionary interface methods to MultiConnection.

See also

ssh_utilities.multi_conncection.MultiConnection

_add_multi(other: MultiConnection)

Register multiple new connections.

_add_one(other: _CONN, key: Optional[str] = None)

Add one connecction to the undelying dictionary.

Parameters:
  • other (_CONN) – SSHConnection or LocalConnection
  • key (Optional[str]) – if used connection is added under this key, else key is extracted from connection.server_name attribute
Raises:

AttributeError – if key is already pressent among registered connections

clear()

Close and delete all underlying connections.

copy() → _DictInterface1

Get a shallow copy of MultiConnection.

Returns:MultiConnection object shallow copy
Return type:MultiConnection
get(key: str, *args) → Optional[_CONN]

Get one connection from MultiConnection object based on key.

Parameters:
  • key (str) – key denoting the connection
  • default (Any, optional) – optianal value returned if key is not pressent
Returns:

SSHConnection or LocalConnection object shallow copy

Return type:

_CONN

Raises:

AttributeError – if the input key is not present among the connections and default is not defined.

items() → ItemsView[str, _CONN]

Iterate over key, Connection pairs same as dict.items method.

Yields:ItemsView[str, _CONN] – key, SSHConnection/LocalConnection
keys() → KeysView[str]

Iterate over registered key same as dict.keys method.

Yields:KeysView[str] – key
pop(key: str, *args) → _CONN

Pop one connection from MultiConnection object based on key.

Parameters:
  • key (str) – key denoting the connection
  • default (Any, optional) – optianal value returned if key is not pressent
Returns:

SSHConnection or LocalConnection object

Return type:

_CONN

Raises:

AttributeError – if the input key is not present among the connections and default is not defined.

popitem() → Tuple[str, _CONN]

Pops one key, connection pair from MultiConnection.

Returns:key, SSHConnection/LocalConnection pair
Return type:Tuple[str, _CONN]
update(other: MultiConnection)

Updates Multiconnection with another Multiconnection.

This only merges underlying dictionaries holding connections

Parameters:other (MultiConnection) – the added object of same type as self
values() → ValuesView[_CONN]

Iterate over registered Connections same as dict.values method.

Yields:ValuesView[_CONN]SSHConnection/LocalConnection

ssh_utilities.multi_connection._persistence

Module providing persistance methods for multi_connection.

Adds the ability to persist object using: - str - dictionary - pickle - deepcopy

class ssh_utilities.multi_connection._persistence.Pesistence

Bases: object

Class providing persistance methods to MultiConnection.

Adds the ability to persist object using: - str - dictionary - pickle - deepcopy

See also

ssh_utilities.multi_conncection.MultiConnection

static _parse_persistence_dict(d: dict) → Tuple[List[str], List[int], List[bool], List[bool]]

Parses dictionary produced by to_dict method.

Parameters:d (dict) – dictionary of values needed to reinitialize the class
Returns:Tuple of lists with parsed information
Return type:Tuple[List[str], List[int], List[bool], List[bool]]
classmethod from_dict(json: dict, quiet: bool = False) → MultiConnection

Initializes Connection from str.

String must be formated as defined by base.ConnectionABC._to_str method.

Parameters:
  • json (dict) – dictionary initialize connection from
  • quiet (bool) – If True suppress login messages
Returns:

initialized local or remmote connection based on parameters parsed from string

Return type:

Union[SSHConnection, LocalConnection]

Raises:

KeyError – if required key is missing from string

classmethod from_str(string: str, quiet: bool = False) → MultiConnection

Initializes Connection from str.

String must be formated as defined by base.ConnectionABC._to_str method.

Parameters:
  • string (str) – json str to initialize connection from
  • quiet (bool) – If True suppress login messages
Returns:

initialized local or remmote connection based on parameters parsed from string

Return type:

Union[SSHConnection, LocalConnection]

Raises:

KeyError – if required key is missing from string

to_dict() → Dict[int, Dict[str, Union[str, bool, int, None]]]

Saves all the importatnt info from object to dictonary.

Returns:dictionary representing the object
Return type:Dict[int, Dict[str, Optional[Union[str, bool, int, None]]]]