ssh API

A wrapper for Paramiko that attempts to make ssh sessions easier to work with. It also contains the shell_command function for running local shell scripts!

sshreader.ssh.envvars()[source]

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

Returns:NamedTuple of (username, rsa_key, dsa_key, ecdsa_key)
sshreader.ssh.shell_command(command, combine=False, decodebytes=True)[source]

Run a command in the shell on localhost and return the output

Parameters:
  • command – String containing the shell script to run
  • combine – Direct stderr to stdout
  • decodebytes – Decode bytes objects to unicode strings
Returns:

NamedTuple for (cmd, stdout, stderr) or (cmd, stdout)

class sshreader.ssh.SSH(fqdn, username=None, password=None, keyfile=None, port=22, timeout=30, connect=True)[source]

SSH Session object

Parameters:
  • fqdn – Fully qualified domain name or IP address
  • username – SSH username
  • password – SSH password
  • keyfile – SSH keyfile (can be used instead of password)
  • port – SSH port
  • timeout – SSH connection timeout in seconds
  • connect – Initiate the connect
Returns:

SSH connection object

Raises:

SSHException

close()[source]

Closes an established ssh connection

Returns:None
connect()[source]

Opens an SSH Connection

Returns:True
Raises:SSHException
is_alive()[source]

Is an SSH connection alive

Returns:True or False
Raises:SSHException
reconnect()[source]

Alias to connect

sftp_get(srcfile, dstfile)[source]

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

Parameters:
  • srcfile – Path to the remote file
  • dstfile – Path to the local file
Returns:

Result of paramiko.SFTPClient.get()

sftp_put(srcfile, dstfile)[source]

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

Parameters:
  • srcfile – Path to the local file
  • dstfile – Path to the remote file
Returns:

Result of paramiko.SFTPClient.put()

ssh_command(command, timeout=30, combine=False, decodebytes=True)[source]

Run a command over an ssh connection

Parameters:
  • command – The command to run
  • timeout – Timeout for the command
  • combine – Combine stderr and stdout (pseudo TTY)
  • decodebytes – Decode bytes objects to unicode strings
Returns:

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

Raises:

SSHException