bladerunner.py

Bladerunner version 3.4. Released July 4, 2013. Written in Python 2.7.

Provides a method of pushing changes or running audits on groups of similar hosts over SSH using pexpect (http://pexpect.sourceforge.net). Can be extended to utilize an intermediary host in the event of networking restrictions.

Copyright (c) 2013, Activision Publishing, Inc. All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  • Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  • Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  • Neither the name of Activision Publishing, Inc. nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

class bladerunner.bladerunner.Bladerunner(options=None)[source]

Main logic for the serial execution of commands on hosts.

Initialized by a dictionary with the following optional keys:

username: string username, or getpass will guess.
password: string plain text password, if required.
ssh_key: string non-default ssh key file location.
delay: integer in seconds to pause between servers.
extra_prompts: list of strings of additional expect prompts.
width: integer terminal width for output, or it uses all.
jump_host: string hostname of intermediary host.
jump_user: alternate username for jump_host.
jump_password: alternate password for jump_host.
second_password: an additional different password for commands.
password_safety: check if the first login succeeds first (False).
cmd_timeout: integer in seconds to wait for commands (20).
timeout: integer in seconds to wait to connect (20).
threads: integer number of parallel threads to run (100)
style: integer for outputting. Between 0-3 are pretty, or CSV.
csv_char: string character to use for CSV results.
progressbar: boolean to declare if we want a progress display.
close(sshc, terminate)[source]

Closes a connection object.

Args:

sshc: the pexpect object to close
terminate: a boolean value to terminate all connections or not
Returns:
None: the sshc will be at the jumpbox, or the connection is closed
connect(target, username, password)[source]

Connects to a server, maybe from another server.

Args:

target: the hostname, as a string
username: the user we are connecting as
password: plain text password to pass
Returns:
a pexpect object that can be passed back here or to send_commands()
login(sshc, password, login_response)[source]

Internal method for logging in, used by connect.

Args:

sshc: the pexpect object
password: plain text password to send
login_response: the pexpect return status integer
Returns:
a tuple of the connection object and error code
run(commands, servers)[source]

Executes commands on servers.

Args:

commands: a list of strings of commands to run
servers: a list of strings of hostnames
Returns:
a list of dictionaries with two keys: name, and results. results is a list of tuples of commands issued and their replies.
send_commands(server, hostname)[source]

Executes the commands on a pexpect object.

Args:

server: the pexpect host object
hostname: the string hostname of the server
Returns:

a dictionary with two keys:

name: string of the server's hostname
results: a list of tuples with each command and its result
send_interrupt(sshc)[source]

Sends ^c and pushes pexpect forward on the object.

Args:
sshc: the pexpect object
Returns:
None: the sshc maintains its state and should be ready for use
bladerunner.bladerunner.consolidate(results)[source]

Makes a list of servers and replies, consolidates dupes.

Args:
results: the results dictionary from Bladerunner.run
Returns:
a results dictionary, with a names key instead of name, containing a lists of hosts with matching outputs
bladerunner.bladerunner.csv_results(results, options=None)[source]

Prints the results consolidated and in a CSV-ish fashion.

Args:

results: the results dictionary from Bladerunner.run
options: dictionary with optional keys:
    csv_char: a character or string to separate with
bladerunner.bladerunner.format_output(output, command)[source]

Formatting function to strip colours, remove tabs, etc.

Args:

output: the pexpect object's before method after issuing the command
command: the command last issued
Returns:
a (hopefully) nicely formatted string of the command’s output
bladerunner.bladerunner.main_exit(results, options)[source]

A buffer for selecting the correct output function and exiting.

Args:

results: the results dictionary from Bladerunner.run
options: the options dictionary, uses 'style' key only
bladerunner.bladerunner.no_empties(input_list)[source]

Searches through a list and tosses empty elements.

bladerunner.bladerunner.pretty_header(options)[source]

Internal function for printing the header of pretty_results.

Args:

options: a dictionary with the following keys:
    width: terminal width, already determined in pretty_results
    chars: the character dictionary map, defined in pretty_results
    left_len: the left side length, defined in pretty_results
    jump_host: a string hostname of the jumpbox (if any)
bladerunner.bladerunner.pretty_results(results, options=None)[source]

Prints the results in a relatively pretty way.

Args:

results: the results dictionary from Bladerunner.run
options: a dictionary with optional keys.
    style: integer style, from 0-3
    jump_host: the string jumpbox hostname
    width: integer fixed width for output
bladerunner.bladerunner.set_shells(options)[source]

Set password, shell and extra prompts for the username.

Args:
options dictionary with username, jump_user and extra_prompts keys.
Returns:
options dictionary with shell_prompts and passwd_prompts keys

Previous topic

Bladerunner

Next topic

progressbar.py

This Page