Automatic creation and download of sysdumps with SteelScript

Overview

This script loads a list of IP addresses or hostnames (appliances.txt) and then logs into every host to

create a sysdump. After creation of that dump, it downloads the dumps in a subdirectory "sysdumps"

of the current working directory (i.e. where you started the script from).

 

I haven't found a way to download the dump using SteelScript natively or the API, so I had to use pexpect.

The script is "as ugly as it get's" and leave room for improvements, but at least it's working.

 

A refactored and feature enhanced version will published shortly on our gitlab page: ZyckoProfessionalServices | GitLab

Customizations

You need to provide a file with all IP Addresses or hostnames named "appliances.txt" in the same

directory as the script itself. Every ip / hostname is a new line with no blank lines anywhere.

When calling the script, you need to pass a common username and password for all appliances:

 

python sysdump_createanddownload_scp_threaded.py "username" "password"

(make sure you use "double quotes"!

 

Prerequisites:

    - Steelscript and SteelHead extension installed:

        - pip install steelscript

        - steel install

        - steel install --steelhead

        see https://support.riverbed.com/apis/steelscript/ for further details

    - pexpect

        - pip install pexpect

    - everything else should be already installed in every standard installation

 

Please note: all my tools / scripts, unless noted otherwise are released for use “AS IS” without any warranties of any kind,

including, but not limited to their installation, use, or performance. We disclaim any and all warranties,

either express or implied, including but not limited to any warranty of noninfringement, merchantability,

and/ or fitness for a particular purpose. We do not warrant that the technology will meet your requirements,

that the operation thereof will be uninterrupted or error-free, or that any errors will be corrected.

Any use of these scripts and tools is at your own risk. There is no guarantee that they have been through

thorough testing in a comparable environment and we are not responsible for any damage or data loss incurred

with their use.

You are responsible for reviewing and testing any scripts you run thoroughly before use in any non-testing

environment.



The script

 

#!/usr/bin/env python
# encoding: utf-8


"""
This script loads a list of IP addresses or hostnames (appliances.txt) and then logs into every host to
create a sysdump. After creation of that dump, it downloads the dumps in a subdirectory "sysdumps"
of the current working directory (i.e. where you started the script from)


You need to provide a file with all IP Addresses or hostnames named "appliances.txt" in the same
directory as the script itself. Every ip / hostname is a new line with no blank lines anywhere.


When calling the script, you need to pass a common username and password for all appliances:


python sysdump_createanddownload_scp_threaded.py "username" "password"
(make sure you use "double quotes"!


Prerequisites:


    - Steelscript and SteelHead extension installed:
        - pip install steelscript
        - steel install
        - steel install --steelhead
        see https://support.riverbed.com/apis/steelscript/ for further details


    - pexpect
        - pip install pexpect


    - everything else should be already installed in every standard installation


Please note: all my tools / scripts, unless noted otherwise are released for use “AS IS” without any warranties of any kind,
including, but not limited to their installation, use, or performance. We disclaim any and all warranties,
either express or implied, including but not limited to any warranty of noninfringement, merchantability,
and/ or fitness for a particular purpose. We do not warrant that the technology will meet your requirements,
that the operation thereof will be uninterrupted or error-free, or that any errors will be corrected.
Any use of these scripts and tools is at your own risk. There is no guarantee that they have been through
thorough testing in a comparable environment and we are not responsible for any damage or data loss incurred
with their use.
You are responsible for reviewing and testing any scripts you run thoroughly before use in any non-testing
environment.
"""


__author__ = 'Andre Dieball (andre@dieball.net), Jason Buttler (me@microbrain.co.uk)'
__copyright__ = 'Copyright (c) 2015 Andre Dieball, Jason Buttler'
__license__ = 'GPLv3'
__vcs_id__ = '$'
__version__ = '1.0.0'




import argparse
parser = argparse.ArgumentParser()
parser.add_argument("username", type=str,
                    help="REQUIRED: Username to use to log into the SteelHead applicances")


parser.add_argument("password", type=str,
                    help="REQUIRED: Password to use to log in the SteelHead appliances."
                         "Please enclose the password in double quotes if it contains any special characters")


args = parser.parse_args()




def main():
    import threading
    import csv
    h = open('appliances.txt')
    for host in csv.reader(h):
        sysdumps_thread = threading.Thread(target=sysdumps, args=(host))
        sysdumps_thread.start()
    h.close()




def sysdumps(host):


    ip = str(host).strip('[]')
    import os
    from steelscript.steelhead.core import steelhead
    import time
    import pexpect


    auth = steelhead.CLIAuth(username=args.username, password=args.password)
    start_time = time.time()
    sh = steelhead.SteelHead(host=ip, auth=auth)


    print "Connected to " + ip + " in " + sh.cli.current_cli_mode() + " mode"


    s = sh.cli.exec_command("debug generate dump", mode="enable", timeout=180)


    dumpname = s.split(None, 2)[-1]


    elapsed_time = time.time() - start_time




    print "Time needed to create " + dumpname + " on " + ip + " : " + str(elapsed_time)
    print
    print "Downloading SysDump " + dumpname + " from " + ip + " now!"




    sysdumppath = os.getcwd() + '/sysdumps/'
    os.system('mkdir ' + sysdumppath)


    try:
        ssh_newkey = "Are you sure you want to continue connecting"


        var_password = args.password
        var_command = "scp " + args.username + "@" + ip + ":/var/opt/tms/sysdumps/" + dumpname + " " + sysdumppath + "/" + dumpname
        var_child = pexpect.spawn(var_command)
        i = var_child.expect([ssh_newkey, "password:", pexpect.EOF])


        if i==0:
            var_child.sendline('yes')
            i = var_child.expect([ssh_newkey, "password:", pexpect.EOF])
        if i==1:
            var_child.sendline(var_password)
            var_child.expect(pexpect.EOF)
        elif i==2:
            print "Got the key or connection timeout"
            pass


    except Exception as error:
        print "Oops Something went wrong buddy"
        print error


if __name__ == '__main__':
    main()


 

Downloadlink: Dropbox - sysdump_createanddownload_scp_threaded.py