sshreader API

All the classes and functions that make sshreader tick

class sshreader.utils.Hook(target, args=None, kwargs=None, ssh_established=False)[source]

Custom class for pre and post hooks

Parameters:
  • target – Function to call when using the hook
  • args – List of args to pass to target
  • kwargs – Dictionary of kwargs to pass to target
  • ssh_established – Should the ssh connection be established when the hook is run
Returns:

Hook

Raises:

TypeError

run(*args, **kwargs)[source]

Run the Hook. You can add additional args or kwargs at this time!

Parameters:
  • args – Append to args
  • kwargs – Append to/update kwargs
Returns:

Result from target function

class sshreader.utils.ServerJob(fqdn, cmds, username=None, password=None, keyfile=None, keypass=None, timeout=(30, 30), runlocal=False, prehook=None, posthook=None, combine_output=False)[source]

Custom class for holding all the info needed to run ssh commands or shell commands in sub-processes or threads

Parameters:
  • fqdn – Fully qualified domain name or IP address
  • cmds – List of commands to run (in the order you want them run)
  • username – Username for SSH
  • password – Password for SSH
  • keyfile – Path to ssh key (can be used instead of password)
  • keypass – Password for private ssh key file
  • timeout – Tuple of timeouts in seconds (sshtimeout, cmdtimeout)
  • runlocal – Run job on localhost (skips ssh to localhost)
  • prehook – Optional Hook object
  • posthook – Optional Hook object
  • combine_output – Combine stdout and stderr
Returns:

ServerJob Object

Raises:

ValueError, TypeError

Property results:
 

List of namedtuples (cmd, stdout, stderr, return_code) or (cmd, stdout, return_code)

Property status:
 

Sum of return codes for entire job (255 = ssh did not connect)

run()[source]

Run a ServerJob. SSH to server, run cmds, return result

Returns:ServerJob.status
sshreader.utils.sshread(serverjobs, pcount=None, tcount=None, progress_bar=False)[source]

Takes a list of ServerJob objects and puts them into threads/sub-processes and runs them

Parameters:
  • serverjobs – List of ServerJob objects (A list of 1 job is acceptable)
  • pcount – Number of sub-processes to spawn (None = off, 0 = cpusoftlimit, -1 = cpuhardlimit)
  • tcount – Number of threads to spawn (None = off, 0 = threadlimit)
  • progress_bar – Print a progress bar
Returns:

List with completed ServerJob objects (single object returned if 1 job was passed)

Raises:

ExceedCPULimit, TypeError, ValueError

sshreader.utils.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)

sshreader.utils.echo(*args, **kwargs)[source]

Wrapper for print that implements a multiprocessing.Lock object as well as uses unbuffered output to sys.stdout.

Parameters:
  • args – Passthrough to print function
  • kwargs – Passthrough to print function
Returns:

None

sshreader.utils.cpusoftlimit()[source]

Return the default number of sub-processes your system is allowed to spawn

Returns:Integer
sshreader.utils.cpuhardlimit()[source]

Return the maximum number of sub-processes your system is allowed to spawn.

cpusoftlimit() * __cpuhardlimitfactor__

Returns:Integer
sshreader.utils.threadlimit()[source]

Return the maximum number of threads each process is allowed to spawn. The idea here is to not overload a system.

cpu_count() * __threadlimitfactor__

Returns:Integer