Description
Purpose:
When SpiceWorks backs up Cisco configuration files they cannot be sorted easily without having to read the hostname in the configuration. I have designed a script that renames Cisco switch configuration files returned from SpiceWorks during a backup. It will name the files based on the hostname (Switch Name) within the config file. Also, the last modified date will be added to the file name.
How To Use:
You can run the program and select the configuration directory where the Cisco config files are stored. Also, you can run the program via command line. This can be used if you are trying to automate the backup of configs for DR.
Never used AutoIT before? You can download the software for free and use the software to compile this code into an EXE. After that point it can be ran on Windows machines without having AutoIT’s software installed.
Note:
The original configuration files are not touched, so SpiceWorks don’t try and capture them again. This script will create a new folder in the same directory called Renamed. It will split the startup and running configurations into two different folders. Any temp files that had no data will not be copied and they will stay outside of the startup and running folders.
Source Code
#cs ----------------------------------------------------------------------------
Created By: KV-Tools
Purpose: Renames Cisco switch configuration files returned from SpiceWorks. It will name the files based on the hostname
(Switch Name) within the config file. Also, the last modified date will be added to the file name.
How To Use:
You can run the program and select the configuration directory where the Cisco config files are stored. Also, you can run
the program via command line. This can be used if you are trying to automate the backup of configs for DR.
Note: The original configuration files are not touched, so SpiceWorks don't try and capture them again. This program will
create a new folder in the same directory called Renamed. It will split the startup and running configurations into two
different folders. Any temp files that had no data will not be copied and they will stay outside of the startup and running folders.
#ce ----------------------------------------------------------------------------
#include <File.au3>
#include <Array.au3>
;Gobal Variables
Global $TheFileListConfigPathArray[1]
Global $TheFileCountLogs = 1
Global $OriginalConfigurationFilePath
Global $RenamedConfigurationFilePath
;Path Name = $cmdline[2]
If $CMDLine[0] = 2 Then
If $cmdline[1] = "/Path" Then
If Not $cmdline[2] = "" Then
If FileExists($CMDLine[2]) Then
;Sets Configuration Path
$OriginalConfigurationFilePath = $CMDLine[2]
Else
MsgBox(0,"Bad Path", "The path entered does not exist or you do not have rights to view this directory.")
Exit
EndIf
EndIf
EndIf
Else
;Calls FielSelectFolder because user did not entery any switching
If UBound($cmdline) = 1 Then
;Select Config Folder
$OriginalConfigurationFilePath = FileSelectFolder("Choose SpiceWorks Cisco Configuration Save Path", "")
;No Path Selected
If $OriginalConfigurationFilePath = "" Then
Exit
EndIf
ElseIf $CMDLine[1] = "?" Then
MsgBox(0,"Command Line Switch", "Command line parameters: " & @LF & @LF & "/Path path to configs")
Exit
ElseIf $CMDLine[1] = "/?" Then
MsgBox(0,"Command Line Switch", "Command line parameters: " & @LF & @LF & "/Path path to configs")
Exit
ElseIf UBound($cmdline) = 2 Then
If $CMDLine[1] = "/Path" Then
MsgBox(0,"Missing Path", "Please enter the configuration path.")
Exit
Else
MsgBox(0,"Wrong Switch", "Wrong switch command")
Exit
EndIf
EndIf
EndIf
;Path For Configuration Files
$RenamedConfigurationFilePath = $OriginalConfigurationFilePath & "\Renamed"
;Creates Copy Directory
If Not FileExists($RenamedConfigurationFilePath) Then
DirCreate($RenamedConfigurationFilePath)
EndIf
;Creates Startup Folders If It Don't Exist
If Not FileExists($RenamedConfigurationFilePath & "\Startup Configs") Then
DirCreate($RenamedConfigurationFilePath & "\Startup Configs")
EndIf
;Creates Running Folder If It Don't Exist
If Not FileExists($RenamedConfigurationFilePath & "\Running Configs") Then
DirCreate($RenamedConfigurationFilePath & "\Running Configs")
EndIf
;Moves Files For Rename
;Note: Originals Need To Exist For Copy Purposes
MovesOriginalsIntoRename()
;Gets Files
FileReturnFromConfigFolder($RenamedConfigurationFilePath)
;Renames Returned Configs
RenameConfigs()
;Moves Original Configs From SpiceWorks Into Rename Folder
Func MovesOriginalsIntoRename()
;Copies Original Configs Into Renamed Folder
DirCopy($OriginalConfigurationFilePath, $RenamedConfigurationFilePath,1)
EndFunc
;Goes through every folder based on the configuration directory and returns them into an array
Func FileReturnFromConfigFolder($ConfigDirectoryFolder)
Local $Search
Local $TheFile
Local $TheFileAttributes
Local $FullFilePath
$Search = FileFindFirstFile($ConfigDirectoryFolder & "\*.*")
While 1
If $Search = -1 Then
ExitLoop
EndIf
$TheFile = FileFindNextFile($Search)
If @error Then ExitLoop
;Sets Full Path
$FullFilePath = $ConfigDirectoryFolder & "\" & $TheFile
;Gets Attribute
$TheFileAttributes = FileGetAttrib($FullFilePath)
;Makes Sure Directories Are Not Added
If Not StringInStr($TheFileAttributes,"D") Then
;This will continue to add to the array as needed after the first slot has been created.
_ArrayAdd($TheFileListConfigPathArray, $FullFilePath)
EndIf
WEnd
FileClose($Search)
;Removes Empty Slot
_ArrayDelete($TheFileListConfigPathArray, 0)
EndFunc
;Rename Configs
Func RenameConfigs()
;Loops Through Array To Process Each Config File
For $i = 0 to UBound($TheFileListConfigPathArray) - 1
Local $SplitConfigLine
Local $CurrentConfigName
Local $LastSlotInSplit
Local $SplitFilePath
Local $SplitConfigNameUnderScore
Local $SplitCOnfigNameDot
Local $ConfigChangeDate
Local $OldFileName, $NewFileName
Local $SplitTime
Local $TimeWithOutSpecialCharacter
Local $TheFile = FileOpen($TheFileListConfigPathArray[$i], 0)
; Check if file opened for reading OK
If $TheFile = -1 Then
MsgBox(0, "Error", "Unable to open file.")
Exit
EndIf
; Read in lines of text until the EOF is reached
While 1
Local $line = FileReadLine($TheFile)
If @error = -1 Then ExitLoop
;Splits string to find hostname
$SplitConfigLine = StringSplit($line, " ")
;Checks Array Size
If UBound($SplitConfigLine) >= 11 Then
;Gets Last Configuation Change Date
If $SplitConfigLine[2] & " " & $SplitConfigLine[3] = "last configuration" Then
;Splits Time Because You Can't Name A File With :
$SplitTime = StringSplit($SplitConfigLine[6], ":")
;Builds Time w/o Special Characters
$TimeWithOutSpecialCharacter = $SplitTime[1] & $SplitTime[2] & $SplitTime[3]
;Changes Name of Month To Number
Select
Case $SplitConfigLine[9] = "Jan"
;Sets Config Change Date
$ConfigChangeDate = "1" & "-" & $SplitConfigLine[10] & "-" & $SplitConfigLine[11] & "__" & $TimeWithOutSpecialCharacter
Case $SplitConfigLine[9] = "Feb"
;Sets Config Change Date
$ConfigChangeDate = "2" & "-" & $SplitConfigLine[10] & "-" & $SplitConfigLine[11] & "__" & $TimeWithOutSpecialCharacter
Case $SplitConfigLine[9] = "Mar"
;Sets Config Change Date
$ConfigChangeDate = "3" & "-" & $SplitConfigLine[10] & "-" & $SplitConfigLine[11] & "__" & $TimeWithOutSpecialCharacter
Case $SplitConfigLine[9] = "Apr"
;Sets Config Change Date
$ConfigChangeDate = "4" & "-" & $SplitConfigLine[10] & "-" & $SplitConfigLine[11] & "__" & $TimeWithOutSpecialCharacter
Case $SplitConfigLine[9] = "May"
;Sets Config Change Date
$ConfigChangeDate = "5" & "-" & $SplitConfigLine[10] & "-" & $SplitConfigLine[11] & "__" & $TimeWithOutSpecialCharacter
Case $SplitConfigLine[9] = "Jun"
;Sets Config Change Date
$ConfigChangeDate = "6" & "-" & $SplitConfigLine[10] & "-" & $SplitConfigLine[11] & "__" & $TimeWithOutSpecialCharacter
Case $SplitConfigLine[9] = "Jul"
;Sets Config Change Date
$ConfigChangeDate = "7" & "-" & $SplitConfigLine[10] & "-" & $SplitConfigLine[11] & "__" & $TimeWithOutSpecialCharacter
Case $SplitConfigLine[9] = "Aug"
;Sets Config Change Date
$ConfigChangeDate = "8" & "-" & $SplitConfigLine[10] & "-" & $SplitConfigLine[11] & "__" & $TimeWithOutSpecialCharacter
Case $SplitConfigLine[9] = "Sep"
;Sets Config Change Date
$ConfigChangeDate = "9" & "-" & $SplitConfigLine[10] & "-" & $SplitConfigLine[11] & "__" & $TimeWithOutSpecialCharacter
Case $SplitConfigLine[9] = "Oct"
;Sets Config Change Date
$ConfigChangeDate = "10" & "-" & $SplitConfigLine[10] & "-" & $SplitConfigLine[11] & "__" & $TimeWithOutSpecialCharacter
Case $SplitConfigLine[9] = "Nov"
;Sets Config Change Date
$ConfigChangeDate = "11" & "-" & $SplitConfigLine[10] & "-" & $SplitConfigLine[11] & "__" & $TimeWithOutSpecialCharacter
Case $SplitConfigLine[9] = "Dec"
;Sets Config Change Date
$ConfigChangeDate = "12" & "-" & $SplitConfigLine[10] & "-" & $SplitConfigLine[11] & "__" & $TimeWithOutSpecialCharacter
Case Else
MsgBox(0, "Error", "Date name don't match case statement: " & $SplitConfigLine[9])
EndSelect
EndIf
EndIf
;Looks For HostName In Config File
If $SplitConfigLine[1] = "hostname" Then
;Splits Path To Get Config File Name
$SplitFilePath = StringSplit($TheFileListConfigPathArray[$i], "\")
;Holds Max Array Slot Count
$LastSlotInSplit = $SplitFilePath[0]
$CurrentConfigName = $SplitFilePath[$LastSlotInSplit]
;Splits the config name to get the first part of the name and date
$SplitConfigNameUnderScore = StringSplit($CurrentConfigName, "_")
;Splits the config name to get the running or starting
$SplitConfigNameDot = StringSplit($SplitConfigNameUnderScore[2], ".")
;Closes File
FileClose($TheFile)
;Holds Naming Information For Easy Understanding Within Script
$OldFileName = $RenamedConfigurationFilePath & "\" & $CurrentConfigName
$NewFileName = $RenamedConfigurationFilePath & "\" & $SplitConfigLine[2] & "_" & $SplitConfigNameDot[1] & "_" & $ConfigChangeDate & ".config"
;MsgBox(0,"Moving", $OldFileName & "........................" & $NewFileName)
;Renames File After File CLose
FileMove($OldFileName,$NewFileName)
;Checks For Type (Startup or Running)
If $SplitConfigNameDot[1] = "startup-config" Then
;Checks To See If Config Was Already Processed At Another Time
If FileExists($RenamedConfigurationFilePath & "\Startup Configs" & "\" & $SplitConfigLine[2] & "_" & $SplitConfigNameDot[1] & "_" & $ConfigChangeDate & ".config") Then
;MsgBox(0,"Removing", $NewFileName)
;Deletes Config Because It Was Already Copied At Another Time
FileDelete($NewFileName)
FileDelete($OldFileName)
Else
;Moves Into Startup Folder
FileMove($NewFileName, $RenamedConfigurationFilePath & "\Startup Configs" & "\" & $SplitConfigLine[2] & "_" & $SplitConfigNameDot[1] & "_" & $ConfigChangeDate & ".config",1)
EndIf
ElseIf $SplitConfigNameDot[1] = "running-config" Then
If FileExists($RenamedConfigurationFilePath & "\Running Configs" & "\" & $SplitConfigLine[2] & "_" & $SplitConfigNameDot[1] & "_" & $ConfigChangeDate & ".config") Then
;Deletes Config Because It Was Already Copied At Another Time
FileDelete($NewFileName)
FileDelete($OldFileName)
Else
;Moves Into Running Folder
FileMove($NewFileName, $RenamedConfigurationFilePath & "\Running Configs" & "\" & $SplitConfigLine[2] & "_" & $SplitConfigNameDot[1] & "_" & $ConfigChangeDate & ".config",1)
EndIf
EndIf
;Clears Config Change Date
$ConfigChangeDate = ""
;Moves Out of First Loop
ExitLoop
EndIf
WEnd
Next
EndFunc
Screenshots