Bladerunner 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 use an intermediary host if there are networking restrictions.
This file is part of Bladerunner.
Copyright (c) 2014, 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:
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.
Main logic for the serial execution of commands on hosts.
Initialized by a dictionary with the following optional keys (defaults):
username: string username (None/current user)
password: string plain text password, if required (None)
ssh_key: string non-default ssh key file location (None)
delay: integer in seconds to pause between servers (None)
extra_prompts: list of strings of additional expect prompts ([])
width: integer terminal width for output, or it uses all (None/guess)
jump_host: string hostname of intermediary host (None)
jump_user: alternate username for jump_host (None)
jump_password: alternate password for jump_host (None)
jump_port: SSH port for jump_host (22)
second_password: an additional different password for commands (None)
password_safety: check if the first login succeeds first (False)
port: SSH port for the servers (22)
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 (0)
csv_char: string character to use for CSV results (",")
progressbar: boolean to declare if we want a progress display (False)
unix_line_endings: force sending LF as line endings for commands
windows_line_endings: force sending CRLF as line endings for commands
Closes a connection object.
Args:
sshc: the pexpect object to close
terminate: a boolean value to terminate all connections or not
Connects to a server, maybe from another server.
Args:
target: the hostname, as a string
username: the user we are connecting as
password: list or string plain text password(s) to try
port: ssh port number, as integer
Ends an interactive stored session.
Builds a BladerunnerInteractive version of this instance for a host.
Internal method for logging in, used by connect/_multipass.
Args:
sshc: the pexpect object
password: plain text password to send
login_response: the pexpect return status integer
Executes commands on servers.
Args:
commands: a list of strings of commands to run
servers: a list of strings of hostnames
commands_on_servers: an optional dictionary used when providing
unique lists of commands per server
Runs a single command interactively on a list of hostnames.
Args:
command: string command to send
hosts: string or list of hostnames to add to the interactive list
print_results: boolean to print the results or return a dict
Runs a function defined by the user over a list of hosts.
The function made can contain logic within to send a different set of commands based on the output of earlier ones issued.
The results returned are in the structure you decide in your own function. If you do not return anything, this function will also return None. The single argument that you are passed in your function will be a BladerunnerInteractive object inialized for one of the hosts in the list provided. Order of execution is not guarenteed.
Note also that the hosts kwarg is only required for the first run of this function. Further runs will reuse the same interactive session(s).
Args:
function: a function to call with a BladerunnerInteractive object
hosts: list of hosts to run the function with
Non-blocking call which creates and starts a thread for self.run().
Args:
commands: a list of strings of commands to run
servers: a list of strings of hostnames
commands_on_servers: an optional dictionary used when providing
unique lists of commands per server
callback: function that will receive results when run is finished
Executes the commands on a pexpect object.
Args:
server: the pexpect host object
hostname: the string hostname of the server
a dictionary with two keys:
name: string of the server's hostname
results: a list of tuples with each command and its result