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 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)
class sshreader.ssh.SSH(fqdn, username=None, password=None, keyfile=None, keypass=None, port=22, connect=True)[source]

SSH Session object

Parameters:
  • fqdn – Fully qualified domain name or IP address
  • username – SSH username
  • password – SSH password
  • keyfile – SSH private key file
  • keypass – SSH private key password
  • port – SSH port
  • connect – Initiate the connect
Returns:

SSH connection object

Raises:

SSHException

alive()[source]

Is an SSH connection alive

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

Closes an established ssh connection

Returns:None
connect(failfast=True, timeout=30)[source]

Opens an SSH Connection

Parameters:
  • failfast – Test socket connection before attempting to connect
  • timeout – SSH connection timeout in seconds
Returns:

True

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