<reponame>dorty3541/SecureCRT
#$language = "python"
#$interface = "1.0"

################################  SCRIPT INFO  ################################
# Document_Device.py
# 
# Description:
#   Sends a series of Cisco Show commands one by one as listed in the
#   COMMANDS array.  The results of each command are captured into a
#   variable, and then written to an individual log file (one log file
#   for each command).
# 


###############################  SCRIPT SETTING  ###############################
#
# Settings for this script are saved in the "script_settings.py" file that
# should be located in the same directory as this script.
#

##################################  IMPORTS  ##################################
# Import OS and Sys module to be able to perform required operations for adding
# the script directory to the python path (for loading modules), and manipulating
# paths for saving files.
import os
import sys

# Add the script directory to the python path (if not there) so we can import 
# modules.
script_dir = os.path.dirname(crt.ScriptFullName)
if script_dir not in sys.path:
    sys.path.insert(0, script_dir)

# Import Settings from Settings File or Default settings
try:
    from script_settings import settings
except ImportError:
    import shutil
    src_file = os.path.join(script_dir, 'script_settings_default.py')
    dst_file = os.path.join(script_dir, 'script_settings.py')
    try:
        shutil.copy(src_file, dst_file)
        setting_msg = ("Personal settings file created in directory:\n'{}'\n\n"
                       "Please edit this file to make any settings changes."
                       ).format(script_dir)
        crt.Dialog.MessageBox(setting_msg, "Settings Created", 64)
        from script_settings import settings
    except IOError, ImportError:
        err_msg =   ('Cannot find settings file.\n\nPlease make sure either the file\n'
                    '"script_settings_default.py"\n exists in the directory:\n"{}"\n'.format(script_dir)
                    )
        crt.Dialog.MessageBox(str(err_msg), "Settings Error", 16)
        exit(0)

# Imports from Cisco SecureCRT library
from ciscolib import StartSession
from ciscolib import EndSession
from ciscolib import GetDateString
from ciscolib import GetAbsolutePath
from ciscolib import WriteOutput

##################################  SCRIPT  ###################################


# Be careful when adding to this list.  If you forget a "," then those two
# commands strings will run together into a single string and sent to the
# device, meaning that neither output will be captures.
#
# The last entry MUST NOT have a comma after it, which might happen if you 
# comment out the last line.
COMMANDS = [
    # "show call active voice brief",
    # "show call history voice brief",
    # "show cdp neighbors detail",
    "show cdp neighbors",
    # "show clock",
    # "show controllers E1",
    # "show controllers T1",
    # "show crypto ipsec sa",
    # "show crypto isakmp sa",
    # "show crypto map",
    # "show debug",
    # "show dial-peer voice summary",
    "show environment power",
    "show etherchannel summary",
    # "show hsrp brief",
    # "show interface counters error",
    # "show interface description",
    # "show interface stats",
    "show interface status",
    # "show interface summary",
    # "show interface transceiver detail",
    # "show interface transceiver",
    # "show interfaces",
    "show inventory",
    # "show ip access-list",
    # "show ip arp",
    # "show ip bgp",
    # "show ip bgp summary",
    "show ip eigrp neighbor",
    "show ip interface brief | ex una",
    # "show ip ospf neighbor",
    # "show ip protocols",
    # "show ip route 0.0.0.0",
    "show ip route",
    # "show ipv6 interface brief",
    # "show ipv6 protocols",
    # "show ipv6 protocols",
    # "show ipv6 route",
    "show license",
    "show license udi",
    # "show log",
    "show mac address-table dynamic",
    # "show mac address-table",
    # "show module",
    # "show policy-map interface",
    # "show policy-map",
    # "show port-channel summary",
    # "show power",
    # "show route-map",
    "show running",
    "show spanning-tree",
    "show spanning-tree root",
    "show standby brief",
    "show stack-power",
    "show switch detail",
    "show version",
    "show vlan",
    "show vtp status"
    ]


def Main():

    # Run session start commands and save session information into a dictionary
    session = StartSession(crt)

    # Extract the hostname from the session info.
    hostname = session['hostname']
    save_path = os.path.join(settings['savepath'], hostname)

    # Get the current date in the format supplied in date_format
    mydate = GetDateString(settings['date_format'])

    # Iterate through each command and write a file with the output.
    for (index, SendCmd) in enumerate(COMMANDS):
        SendCmd = SendCmd.strip()
        # Save command without spaces to use in output filename.
        CmdName = SendCmd.replace(" ", "_")
        # Add a newline to command before sending it to the remote device.
        SendCmd = SendCmd + "\n"

        # Create Filename
        filebits = [CmdName, hostname, mydate + ".txt"]
        filename = '-'.join(filebits)
        
        # Capture output and write to file (extension already in filename, so
        # override the default for the function (.txt), or it'll append twice.)
        fullFileName = GetAbsolutePath(session, save_path, filename)
        
        # Write the output of the command to a file.
        WriteOutput(session, SendCmd, fullFileName, ext="")
        
        # If file isn't empty (greater than 3 bytes)
        # Some of these file only save one CRLF, and so we can't match on 0 bytes
        if os.path.getsize(fullFileName) > 3: 
            # Open the file we just created.
            newfile = open(fullFileName, "r")
            # If the file only contains invalid command error, delete it.
            for line in newfile:
                if "% Invalid" in line:
                    newfile.close()
                    os.remove(fullFileName)
                    break 
            else:
                newfile.close()
        # If the file is empty, delete it
        else:
            os.remove(fullFileName)
    
    # Clean up before closing session
    EndSession(session)
        
    crt.Dialog.MessageBox("Device Documentation Script Complete", "Script Complete", 64)


if __name__ == "__builtin__":
    Main()

#$language = "python"
#$interface = "1.0"

################################  SCRIPT INFO  ################################
# Document_Device.py
# 
# Description:
#   Sends a series of Cisco Show commands one by one as listed in the
#   COMMANDS array.  The results of each command are captured into a
#   variable, and then written to an individual log file (one log file
#   for each command).
# 


###############################  SCRIPT SETTING  ###############################
#
# Settings for this script are saved in the "script_settings.py" file that
# should be located in the same directory as this script.
#

##################################  IMPORTS  ##################################
# Import OS and Sys module to be able to perform required operations for adding
# the script directory to the python path (for loading modules), and manipulating
# paths for saving files.
import os
import sys

# Add the script directory to the python path (if not there) so we can import 
# modules.
script_dir = os.path.dirname(crt.ScriptFullName)
if script_dir not in sys.path:
    sys.path.insert(0, script_dir)

# Import Settings from Settings File or Default settings
try:
    from script_settings import settings
except ImportError:
    import shutil
    src_file = os.path.join(script_dir, 'script_settings_default.py')
    dst_file = os.path.join(script_dir, 'script_settings.py')
    try:
        shutil.copy(src_file, dst_file)
        setting_msg = ("Personal settings file created in directory:\n'{}'\n\n"
                       "Please edit this file to make any settings changes."
                       ).format(script_dir)
        crt.Dialog.MessageBox(setting_msg, "Settings Created", 64)
        from script_settings import settings
    except IOError, ImportError:
        err_msg =   ('Cannot find settings file.\n\nPlease make sure either the file\n'
                    '"script_settings_default.py"\n exists in the directory:\n"{}"\n'.format(script_dir)
                    )
        crt.Dialog.MessageBox(str(err_msg), "Settings Error", 16)
        exit(0)

# Imports from Cisco SecureCRT library
from ciscolib import StartSession
from ciscolib import EndSession
from ciscolib import GetDateString
from ciscolib import GetAbsolutePath
from ciscolib import WriteOutput

##################################  SCRIPT  ###################################


# Be careful when adding to this list.  If you forget a "," then those two
# commands strings will run together into a single string and sent to the
# device, meaning that neither output will be captures.
#
# The last entry MUST NOT have a comma after it, which might happen if you 
# comment out the last line.
COMMANDS = [
    # "show call active voice brief",
    # "show call history voice brief",
    # "show cdp neighbors detail",
    "show cdp neighbors",
    # "show clock",
    # "show controllers E1",
    # "show controllers T1",
    # "show crypto ipsec sa",
    # "show crypto isakmp sa",
    # "show crypto map",
    # "show debug",
    # "show dial-peer voice summary",
    "show environment power",
    "show etherchannel summary",
    # "show hsrp brief",
    # "show interface counters error",
    # "show interface description",
    # "show interface stats",
    "show interface status",
    # "show interface summary",
    # "show interface transceiver detail",
    # "show interface transceiver",
    # "show interfaces",
    "show inventory",
    # "show ip access-list",
    # "show ip arp",
    # "show ip bgp",
    # "show ip bgp summary",
    "show ip eigrp neighbor",
    "show ip interface brief | ex una",
    # "show ip ospf neighbor",
    # "show ip protocols",
    # "show ip route 0.0.0.0",
    "show ip route",
    # "show ipv6 interface brief",
    # "show ipv6 protocols",
    # "show ipv6 protocols",
    # "show ipv6 route",
    "show license",
    "show license udi",
    # "show log",
    "show mac address-table dynamic",
    # "show mac address-table",
    # "show module",
    # "show policy-map interface",
    # "show policy-map",
    # "show port-channel summary",
    # "show power",
    # "show route-map",
    "show running",
    "show spanning-tree",
    "show spanning-tree root",
    "show standby brief",
    "show stack-power",
    "show switch detail",
    "show version",
    "show vlan",
    "show vtp status"
    ]


def Main():

    # Run session start commands and save session information into a dictionary
    session = StartSession(crt)

    # Extract the hostname from the session info.
    hostname = session['hostname']
    save_path = os.path.join(settings['savepath'], hostname)

    # Get the current date in the format supplied in date_format
    mydate = GetDateString(settings['date_format'])

    # Iterate through each command and write a file with the output.
    for (index, SendCmd) in enumerate(COMMANDS):
        SendCmd = SendCmd.strip()
        # Save command without spaces to use in output filename.
        CmdName = SendCmd.replace(" ", "_")
        # Add a newline to command before sending it to the remote device.
        SendCmd = SendCmd + "\n"

        # Create Filename
        filebits = [CmdName, hostname, mydate + ".txt"]
        filename = '-'.join(filebits)
        
        # Capture output and write to file (extension already in filename, so
        # override the default for the function (.txt), or it'll append twice.)
        fullFileName = GetAbsolutePath(session, save_path, filename)
        
        # Write the output of the command to a file.
        WriteOutput(session, SendCmd, fullFileName, ext="")
        
        # If file isn't empty (greater than 3 bytes)
        # Some of these file only save one CRLF, and so we can't match on 0 bytes
        if os.path.getsize(fullFileName) > 3: 
            # Open the file we just created.
            newfile = open(fullFileName, "r")
            # If the file only contains invalid command error, delete it.
            for line in newfile:
                if "% Invalid" in line:
                    newfile.close()
                    os.remove(fullFileName)
                    break 
            else:
                newfile.close()
        # If the file is empty, delete it
        else:
            os.remove(fullFileName)
    
    # Clean up before closing session
    EndSession(session)
        
    crt.Dialog.MessageBox("Device Documentation Script Complete", "Script Complete", 64)


if __name__ == "__builtin__":
    Main()
#$language = "python" #$interface = "1.0" ################################  SCRIPT INFO  ################################ # Document_Device.py # # Description: #   Sends a series of Cisco Show commands one by one as listed in the #   COMMANDS array.  The results of each command are captured into a #   variable, and then written to an individual log file (one log file #   for each command). # ###############################  SCRIPT SETTING  ############################### # # Settings for this script are saved in the "script_settings.py" file that # should be located in the same directory as this script. # ##################################  IMPORTS  ################################## # Import OS and Sys module to be able to perform required operations for adding # the script directory to the python path (for loading modules), and manipulating # paths for saving files. # Add the script directory to the python path (if not there) so we can import # modules. # Import Settings from Settings File or Default settings # Imports from Cisco SecureCRT library ##################################  SCRIPT  ################################### # Be careful when adding to this list.  If you forget a "," then those two # commands strings will run together into a single string and sent to the # device, meaning that neither output will be captures. # # The last entry MUST NOT have a comma after it, which might happen if you # comment out the last line. # "show call active voice brief", # "show call history voice brief", # "show cdp neighbors detail", # "show clock", # "show controllers E1", # "show controllers T1", # "show crypto ipsec sa", # "show crypto isakmp sa", # "show crypto map", # "show debug", # "show dial-peer voice summary", # "show hsrp brief", # "show interface counters error", # "show interface description", # "show interface stats", # "show interface summary", # "show interface transceiver detail", # "show interface transceiver", # "show interfaces", # "show ip access-list", # "show ip arp", # "show ip bgp", # "show ip bgp summary", # "show ip ospf neighbor", # "show ip protocols", # "show ip route 0.0.0.0", # "show ipv6 interface brief", # "show ipv6 protocols", # "show ipv6 protocols", # "show ipv6 route", # "show log", # "show mac address-table", # "show module", # "show policy-map interface", # "show policy-map", # "show port-channel summary", # "show power", # "show route-map", # Run session start commands and save session information into a dictionary # Extract the hostname from the session info. # Get the current date in the format supplied in date_format # Iterate through each command and write a file with the output. # Save command without spaces to use in output filename. # Add a newline to command before sending it to the remote device. # Create Filename # Capture output and write to file (extension already in filename, so # override the default for the function (.txt), or it'll append twice.) # Write the output of the command to a file. # If file isn't empty (greater than 3 bytes) # Some of these file only save one CRLF, and so we can't match on 0 bytes # Open the file we just created. # If the file only contains invalid command error, delete it. # If the file is empty, delete it # Clean up before closing session