ssh API

A wrapper for Paramiko that attempts to make ssh sessions easier to work with.

class sshreader.ssh.SSH(fqdn: str, username: str, password: str | None = None, keyfile: str | None = None, keypass: str | None = None, port: int = 22, connect: bool = True, rsa_sha2: bool = True)[source]

SSH Session class which can be used to send commands to a remote server. It also supports basic SFTP commands and can be used to push/pull files from a remote system.

Parameters:
  • fqdn (str, required) – Fully qualified domain name or IP address

  • username (str, required) – SSH username

  • password (str, optional) – SSH password

  • keyfile (str, optional) – Path to SSH Private Key File

  • keypass (str, optional) – SSH private key password

  • port (int, optional) – SSH port (Default: 22)

  • connect (bool, optional) – Initiate ssh connection on object initialization (Default: True)

  • rsa_sha2 (bool, optional) – Enable/Disable RSA w/SHA2 hashes (Default: True)

Raises:

paramiko.SSHException

alive()[source]

Is an SSH connection alive

Returns:

True or False

Return type:

bool

Raises:

paramiko.SSHException

close()[source]

Closes an established ssh connection

Returns:

None

connect(timeout: int | float = 0.5) bool[source]

Opens an SSH Connection

Parameters:

timeout (int or float, optional) – TCP Timeout in seconds (Default: 0.5)

Returns:

True

Return type:

bool

Raises:

paramiko.SSHException

reconnect()[source]

Alias to connect

sftp_get(srcfile: str, dstfile: str) None[source]

Use the SFTP subsystem of OpenSSH to copy a remote file to the localhost

Parameters:
  • srcfile (str, required) – Path to the remote file

  • dstfile (str, required) – Path to the local file

Returns:

None

sftp_put(srcfile: str, dstfile: str) Any[source]

Use the SFTP subsystem of OpenSSH to copy a local file to a remote host

Parameters:
  • srcfile (str, required) – Path to the local file

  • dstfile (str, required) – Path to the remote file

Returns:

Result of paramiko.SFTPClient.put()

ssh_command(command: str, timeout: int | float = 30, combine: bool = False, decodebytes: bool = True) Command[source]

Run a command over an ssh connection

Parameters:
  • command (str, required) – The command to run

  • timeout (int or float, optional) – Timeout for blocking commands in seconds (Default: 30)

  • combine (bool, optional) – Combine stderr and stdout using a pseudo TTY (Default: False)

  • decodebytes (bool, optional) – Decode bytes objects to unicode strings in Python3 (Default: True)

Returns:

Namedtuple of (cmd, stdout, stderr, return_code) or (cmd, stdout, return_code)

Return type:

Command

Raises:

paramiko.SSHException

sshreader.ssh.envvars() EnvVars[source]

Attempt to determine the current username and location of any ssh private keys. If any value is unable to be determined it is returned as ‘None’.

This method also checks for any private keys loaded into the SSH Agent.

Returns:

NamedTuple of (username, agent_keys, rsa_key, dsa_key, ecdsa_key)

Return type:

typing.NamedTuple

Indices and tables