sshreader API

All the classes and functions that make sshreader tick

class sshreader.sshreader.Hook(target, args=None, kwargs=None)[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
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.sshreader.ServerJob(fqdn, cmds, username=None, password=None, keyfile=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)
  • 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)

keys()[source]

So you can work with the object in Dictionary form

output()[source]

Prints the status of the ServerJob and details of each cmd in the job

Returns:None
run()[source]

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

Returns:ServerJob.status
sshreader.sshreader.cpuhardlimit()[source]

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

cpusoftlimit() * __cpuHardLimitFactor__

Returns:Integer
sshreader.sshreader.cpusoftlimit()[source]

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

Returns:Integer
sshreader.sshreader.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.sshreader.print_results(serverjobs)[source]

Print the output of all ServerJobs in as ServerJobList by job status

Warning

This call will be removed in v4.0

Parameters:serverjobs – List of ServerJob objects
Returns:SortedJobs named tuple
sshreader.sshreader.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.sshreader.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() * 2

Returns:Integer