spudw2k Posted July 27, 2016 Share Posted July 27, 2016 (edited) I put together a small tool to help inspect/navigate through Junos Configuration files. I uses a treeview control to create a nice hierarchical UI. edit: uploaded Example.conf to provide a more meaningful demo. Not everyone has Junos hardware/configs, so I figure I'd at least show the file structure and how the script uses it, Example.conf Spoiler expandcollapse popup#Region - Includes, Globals and Main Loop #include <WindowsConstants.au3> #include <GUIConstantsEx.au3> #include <FileConstants.au3> #include <MsgBoxConstants.au3> #include <StringConstants.au3> #include <GuiTreeView.au3> #include <Array.au3> Opt("GUIOnEventMode", 1) ;Turn on OnEvent Mode Global Const $sGUITitle = "Junos Config Explorer" ;Window Title Global Const $sAuthorDate = "05/16/2023" ;Publish Date Global $aGUI[1] = ["id/hWnd"] ;Array to Hold GUI Controls Enum $hGUI = 1, $idTreeView, $idImageList, $idMNUFile, $idMNUFileOpen, $idMNUFileExit, $idMNUHelp, $idMNUHelpAbout, $idGUILast ;Enums for GUI Array ReDim $aGUI[$idGUILast] ;ReSize GUI Array based on Enums _GUI($aGUI) ;Create GUI _OpenConfig() $aJunosConfig = 0 ;Cleanup Junos Configuartion Array While 1 ;Main Loop Sleep(10) WEnd _Exit() ;Terminate Script #EndRegion - Includes, Globals and Main Loop #Region - User Defined Functions Func _About() $sMessage = $sGUITitle & @CRLF & "Last Update: " & $sAuthorDate & @CRLF & @CRLF & _ "This tool is designed to provide an interactive, easy" & @CRLF & _ "to navigate UI to provide meaningful inspection of" & @CRLF & _ "Junos Configuration Files." & @CRLF & @CRLF MsgBox(64 + 8192, "About", $sMessage, 15) EndFunc Func _ErrOut($iErrorCode) ;Error Out Function If $iErrorCode Then ConsoleWrite("Error: " & $iErrorCode & @CRLF) ;Write Error Code to Console _Exit() ;Terminate Script EndFunc ;==>_ErrOut Func _Exit() ;Script Terminate Function Exit ;Exit Script EndFunc ;==>_Exit Func _OpenConfig() Local $aJunosConfig = _JunosConfigOpen() ;Open Junos Config File and Populate Array If @error Then _ErrOut(@error) ;If Junos Config Open Failed then Error Out _GUICtrlTreeView_DeleteAll($aGUI[$idTreeView]) ;Clear TreeView Control _PopulateGUI($aGUI, $aJunosConfig) ;Populate GUI Treeview with Junos Configuration Array $aJunosConfig = 0 ;Cleanup Junos Configuartion Array EndFunc Func _GUI(ByRef $aGUI) ;Build GUI Function $aGUI[$hGUI] = GUICreate($sGUITitle, 480, 480, -1, -1, BitOR($WS_SIZEBOX, $WS_SYSMENU, $WS_MINIMIZEBOX, $WS_MAXIMIZEBOX)) ;Create GUI Window $aGUI[$idTreeView] = _GUICtrlTreeView_Create($aGUI[$hGUI], 0, 0, 280, 280) ;Create TreeView Control $aGUI[$idMNUFile] = GUICtrlCreateMenu("&File") ;File Menu $aGUI[$idMNUFileOpen] = GUICtrlCreateMenuItem("&Open", $aGUI[$idMNUFile]) ; File Open GUICtrlSetOnEvent(-1, "_OpenConfig") ;File Open Function GUICtrlCreateMenuItem("", $aGUI[$idMNUFile]) ;File Menu Spacer $aGUI[$idMNUFileExit] = GUICtrlCreateMenuItem("E&xit", $aGUI[$idMNUFile]) ; File Exit GUICtrlSetOnEvent(-1, "_Exit") ;File Exit Function $aGUI[$idMNUHelp] = GUICtrlCreateMenu("&Help") ;Help Menu $aGUI[$idMNUHelpAbout] = GUICtrlCreateMenuItem("&About", $aGUI[$idMNUHelp]) ;Help About GUICtrlSetOnEvent(-1, "_About") ;Help About Function $aGUI[$idImageList] = _GUIImageList_Create(32, 32, 5, 1) ;Create ImageList for TreeView _GUIImageList_AddIcon($aGUI[$idImageList], "shell32.dll", 263, True) ;0 Default Image _GUIImageList_AddIcon($aGUI[$idImageList], "shell32.dll", 264, True) ;1 Groups _GUIImageList_AddIcon($aGUI[$idImageList], "shell32.dll", 252, True) ;2 System _GUIImageList_AddIcon($aGUI[$idImageList], "netshell.dll", 27, True) ;3 Interfaces _GUIImageList_AddIcon($aGUI[$idImageList], "shell32.dll", 244, True) ;4 Security _GUIImageList_AddIcon($aGUI[$idImageList], "shell32.dll", 71, True) ;5 Application _GUIImageList_AddIcon($aGUI[$idImageList], "shell32.dll", 20, True) ;6 Log _GUIImageList_AddIcon($aGUI[$idImageList], "mapi32.dll", 3, True) ;7 Address _GUIImageList_AddIcon($aGUI[$idImageList], "shell32.dll", 296, True) ;8 Permit _GUIImageList_AddIcon($aGUI[$idImageList], "shell32.dll", 109, True) ;9 Deny _GUIImageList_AddIcon($aGUI[$idImageList], "shell32.dll", 22, True) ;10 Match _GUIImageList_AddIcon($aGUI[$idImageList], "shell32.dll", 18, True) ;11 Node _GUIImageList_AddIcon($aGUI[$idImageList], "shell32.dll", 9, True) ;12 Port _GUIImageList_AddIcon($aGUI[$idImageList], "netshell.dll", 118, True) ;13 Source _GUIImageList_AddIcon($aGUI[$idImageList], "netshell.dll", 119, True) ;14 Destination _GUIImageList_AddIcon($aGUI[$idImageList], "netshell.dll", 29, True) ;15 Rout(e/ing) _GUIImageList_AddIcon($aGUI[$idImageList], "netshell.dll", 129, True) ;16 Interface _GUIImageList_AddIcon($aGUI[$idImageList], "netshell.dll", 24, True) ;17 Protocol _GUIImageList_AddIcon($aGUI[$idImageList], "shell32.dll", 69, True) ;18 Polic(y/ies) _GUIImageList_AddIcon($aGUI[$idImageList], "shell32.dll", 96, True) ;19 from-zone _GUIImageList_AddIcon($aGUI[$idImageList], "shell32.dll", 293, True) ;20 "Default Command (;)" _GUICtrlTreeView_SetNormalImageList($aGUI[$idTreeView], $aGUI[$idImageList]) ;Apply ImageList to TreeView GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit", $aGUI[$hGUI]) ;Set Close Event Function GUIRegisterMsg($WM_SIZE, "_WM_SIZE") ;Set Windows Size Message Function for Resizing TreeView Control Return GUISetState(@SW_SHOW, $aGUI[$hGUI]) ;Display GUI EndFunc ;==>_GUI Func _JunosConfigOpen() ;Open Junos Config File and Populate Array Local $sSrcFile = FileOpenDialog("Open Junos Configuration File",@ScriptDir,"Junos Conf File (*.conf)",BitOR($FD_FILEMUSTEXIST, $FD_PATHMUSTEXIST)) ;FileOpenDialog Prompt to Select & Open Junos Configuration If Not FileExists($sSrcFile) Then Return SetError(1, 0, 0) ;If Junos File Path Does Not Exist, Error Out Local $hFile = FileOpen($sSrcFile) ;Open Junos Configration File as Read-Only If $hFile = -1 Then Return SetError(2, 0, 0) ;If FileOpen Failed, Error Out Local $sFile = FileRead($hFile) ;Read File Contents into Variable FileClose($hFile) ;Close Junos Configuration File Local $aJunosConfig = StringSplit($sFile, Chr(10), $STR_ENTIRESPLIT) ;Create Array from Junos Configuration File If @error = 1 Then Return SetError(3, 0, 0) ;If StringSplit Failed, Error Out Return $aJunosConfig ;Return Junos Configuration Array EndFunc ;==>_JunosConfigOpen Func _PopulateGUI(ByRef $aGUI, ByRef $aJunosConfig) ;Poplate GUI from Junos Configuration Array WinSetTitle($aGUI[$hGUI], "", "Loading Configuration...") ;Set Win Title to Indicate Data Processing _GUICtrlTreeView_BeginUpdate($aGUI[$idTreeView]) ;Lock TreeView to prevent Drawing Updates While Populating Local $iLastItem = 0 ;Track Last TreeView Item Created Local $iLastLevel = 0 ;Track Last Heirachal Level Local $sValue = $aJunosConfig[4] ;First TreeView Item Local $iImageIndex = 0 ;TreeView Item Image Index $sValue = StringReplace($sValue, "{", "") ;String Formatting - Remove { from Junos Configuration Item $iLastItem = _GUICtrlTreeView_Add($aGUI[$idTreeView], $iLastItem, $sValue, 2, 2) ;Create First TreeView Item For $iX = 5 To $aJunosConfig[0] - 1 ;Loop Through Junos Configuration Array Local $iLevel = 0 ;Reset Heirarchal Level $sValue = $aJunosConfig[$iX] ;Get Next Junos Configuration Item Local $iTrim = StringLeft($sValue, 4) ;Check for Heirarchal Level If $iTrim == " " Then ;Measure Heirarchal Level While StringLeft($sValue, 4) == " " ;While Level Not Reached End $iLevel += 1 ;Increment Level $sValue = StringTrimLeft($sValue, 4) ;Shorten String by One Level WEnd EndIf $sValue = StringReplace($sValue, "{", "") ;String Formatting - Remove { from Junos Configuration Item $iImageIndex = 0 ;Reset TreeView Item Image Index If StringLeft($sValue, 1) <> "}" Then ;Check Item names to Apply Appropriate Image Index $sValue = StringReplace($sValue, "}", "") If StringInStr($sValue, "system") Then $iImageIndex = 2 ElseIf StringInStr($sValue, "routing") Or StringInStr($sValue, "route") Then $iImageIndex = 15 ElseIf StringInStr($sValue, "policy") or StringInStr($sValue, "policies") Then $iImageIndex = 18 ElseIf StringInStr($sValue, "zone") Then $iImageIndex = 19 ElseIf StringInStr($sValue, "interfaces") Then $iImageIndex = 3 ElseIf StringInStr($sValue, "security") Then $iImageIndex = 4 ElseIf StringInStr($sValue, "application") Or StringInStr($sValue, "services") Then $iImageIndex = 5 ElseIf StringInStr($sValue, "log ") or StringInstr($sValue, "-log") Then $iImageIndex = 6 ElseIf StringInStr($sValue, "source") or StringInstr($sValue, "host") Then $iImageIndex = 13 ElseIf StringInStr($sValue, "destination") Then $iImageIndex = 14 ElseIf StringInStr($sValue, "address") Then $iImageIndex = 7 ElseIf StringInStr($sValue, "permit;") Then $iImageIndex = 8 ElseIf StringInStr($sValue, "deny;") or StringInStr($sValue, "disable") Then $iImageIndex = 9 ElseIf StringInStr($sValue, "match") Then $iImageIndex = 10 ElseIf StringInStr($sValue, "node") Then $iImageIndex = 11 ElseIf StringInStr($sValue, "port") Then $iImageIndex = 12 ElseIf StringInStr($sValue, "interface") Then $iImageIndex = 16 ElseIf StringInStr($sValue, "protocol") Then $iImageIndex = 17 ElseIf StringInStr($sValue, ";") Then $iImageIndex = 20 EndIf Select ;Logic to Determine if Next Junos Configuration Item is Child, Sibling or Parent based on Current Item Level and Previous Level Case $iLevel > $iLastLevel ;If Current Item Level > Last Item Level $iLastItem = _GUICtrlTreeView_AddChild($aGUI[$idTreeView], $iLastItem, $sValue, $iImageIndex, $iImageIndex) ;Create New Child Item Case $iLevel = $iLastLevel ;If Current Item Level = Last Item Level $iLastItem = _GUICtrlTreeView_Add($aGUI[$idTreeView], $iLastItem, $sValue, $iImageIndex, $iImageIndex) ;Create New Sibling Item Case $iLevel < $iLastLevel ;If Current Item Level < Last Item Level Local $iLevelTemp = $iLevel ;Temp Variable to Decrement to the Proper Level / Parent Do ;Loop $iLastItem = _GUICtrlTreeView_GetParentHandle($aGUI[$idTreeView], $iLastItem) ;Get Parent Item $iLevelTemp -= 1 ;Decrement Level Until $iLevelTemp < $iLastLevel ;Until Temp Variable Decremented to Lower Level $iLastItem = _GUICtrlTreeView_Add($aGUI[$idTreeView], $iLastItem, $sValue, $iImageIndex, $iImageIndex) ;Create New Parent Item EndSelect ElseIf $iLevel < $iLastLevel Then ;If Item is Closing Bracket {, Decrement Level to Parent Local $iLevelTemp = $iLevel ;Temp Variable to Decrement to the Proper Level / Parent Do ;Loop $iLastItem = _GUICtrlTreeView_GetParentHandle($aGUI[$idTreeView], $iLastItem) ;Get Parent Item $iLevelTemp -= 1 ;Decrement Level Until $iLevelTemp < $iLastLevel ;Until Temp Variable Decremented to Lower Level EndIf $iLastLevel = $iLevel ;Track Last Heirarchal Level Next _GUICtrlTreeView_EndUpdate($aGUI[$idTreeView]) ;Unlock and Redraw TreeView WinSetTitle($aGUI[$hGUI], "", $sGUITitle) ;Reset Window Title EndFunc ;==>_PopulateGUI Func _WM_SIZE($hWnd, $Msg, $wParam, $lParam) ;Window Resize Function Local $iWidth = BitAND($lParam, 0x0000FFFF) ;Get GUI Width Local $iHeight = BitShift($lParam, 16) ;Get GUI Height _WinAPI_MoveWindow($aGUI[$idTreeView], 0, 0, $iWidth, $iHeight) ;Resize TreeView Control EndFunc ;==>_WM_SIZE #EndRegion - User Defined Functions Edited May 16, 2023 by spudw2k Spoiler Things I've Made: Always On Top Tool ◊ AU History ◊ Deck of Cards ◊ HideIt ◊ ICU ◊ Icon Freezer ◊ Ipod Ejector ◊ Junos Configuration Explorer ◊ Link Downloader ◊ MD5 Folder Enumerator ◊ PassGen ◊ Ping Tool ◊ Quick NIC ◊ Read OCR ◊ RemoteIT ◊ SchTasksGui ◊ SpyCam ◊ System Scan Report Tool ◊ System UpTime ◊ Transparency Machine ◊ VMWare ESX Builder Misc Code Snippets: ADODB Example ◊ CheckHover ◊ Detect SafeMode ◊ DynEnumArray ◊ GetNetStatData ◊ HashArray ◊ IsBetweenDates ◊ Local Admins ◊ Make Choice ◊ Recursive File List ◊ Remove Sizebox Style ◊ Retrieve PNPDeviceID ◊ Retrieve SysListView32 Contents ◊ Set IE Homepage ◊ Tickle Expired Password ◊ Transpose Array Projects: Drive Space Usage GUI ◊ LEDkIT ◊ Plasma_kIt ◊ Scan Engine Builder ◊ SpeeDBurner ◊ SubnetCalc Cool Stuff: AutoItObject UDF ◊ Extract Icon From Proc ◊ GuiCtrlFontRotate ◊ Hex Edit Funcs ◊ Run binary ◊ Service_UDF Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now