msl.network.ssh module

Helper functions for connecting to a remote computer via SSH.

Follow these instructions to install/enable an SSH server on Windows. You can also create an SSH server using the paramiko package (which is included when MSL-Network is installed).

The two functions start_manager() and parse_console_script_kwargs() are meant to be used together to automatically start a Network Manager, and possibly Services, on a remote computer.

See Starting a Service from another computer for an example on how to start a Service on a Raspberry Pi from another computer.

msl.network.ssh.parse_console_script_kwargs()[source]

Parses the command line for keyword arguments sent from a remote computer.

New in version 0.4.

Returns:

dict – The keyword arguments that were passed from start_manager().

msl.network.ssh.start_manager(host, console_script_path, *, ssh_username=None, ssh_password=None, timeout=10, as_sudo=False, missing_host_key_policy=None, paramiko_kwargs=None, **kwargs)[source]

Start a Network Manager on a remote computer.

New in version 0.4.

Parameters:
  • host (str) – The hostname (or IP address) of the remote computer. For example – '192.168.1.100', 'raspberrypi', 'pi@raspberrypi'

  • console_script_path (str) – The file path to where the console script is located on the remote computer.

  • ssh_username (str, optional) – The username to use to establish the SSH connection. If None and the ssh_username is not specified in host then you will be asked for the ssh_username.

  • ssh_password (str, optional) – The password to use to establish the SSH connection. If None then you will be asked for the ssh_password.

  • timeout (int or float, optional) – The maximum number of seconds to wait for the SSH connection.

  • as_sudo (bool, optional) – Whether to run the console script as a superuser.

  • missing_host_key_policy (MissingHostKeyPolicy, optional) – The policy to use when connecting to servers without a known host key. If None then uses AutoAddPolicy.

  • paramiko_kwargs (dict, optional) – Additional keyword arguments that are passed to ssh.connect.

  • kwargs – The keyword arguments in run_forever(), and if that console script also starts Services on the remote computer as well, then the keyword arguments also found in start(). The kwargs should be parsed by parse_console_script_kwargs() on the remote computer.

msl.network.ssh.connect(host, *, username=None, password=None, timeout=10, missing_host_key_policy=None, **kwargs)[source]

SSH to a remote computer.

New in version 0.4.

Parameters:
  • host (str) – The hostname (or IP address) of the remote computer. For example – '192.168.1.100', 'raspberrypi', 'pi@raspberrypi'

  • username (str, optional) – The username to use to establish the SSH connection. If None and the username is not specified in host then you will be asked for the username.

  • password (str, optional) – The password to use to establish the SSH connection. If None then you will be asked for the password.

  • timeout (int or float, optional) – The maximum number of seconds to wait for the SSH connection.

  • missing_host_key_policy (MissingHostKeyPolicy, optional) – The policy to use when connecting to servers without a known host key. If None then uses AutoAddPolicy.

  • kwargs – Additional keyword arguments that are passed to SSHClient.connect.

Returns:

SSHClient – The SSH connection to the remote computer.

msl.network.ssh.exec_command(ssh_client, command, *, timeout=10)[source]

Execute the SSH command on the remote computer.

New in version 0.4.

Parameters:
  • ssh_client (SSHClient) – The SSH client that has already established a connection to the remote computer. See also connect().

  • command (str) – The command to execute on the remote computer.

  • timeout (int or float, optional) – The maximum number of seconds to wait for the command to finish.

Raises:

RuntimeError – If an error occurred. Either a timeout or stderr on the remote computer contains text from executing the command.

Returns:

list of str – stdout from the remote computer.