Skip to content

Instantly share code, notes, and snippets.

@ninmonkey
Last active August 26, 2026 02:32
Show Gist options
  • Select an option

  • Save ninmonkey/663e652650ccbe74bf592df153510cbd to your computer and use it in GitHub Desktop.

Select an option

Save ninmonkey/663e652650ccbe74bf592df153510cbd to your computer and use it in GitHub Desktop.
Parsing-CiscoTemplateFiles.ps1
#requires -Modules Pansies
#requires -PSEdition Core
remove-module PSFrameWork, psutil, string -ea ignore
<#
.SYNOPSIS
parse a cisco router config from 10 years ago. split into groups. Show depths. Show warnings if any exit contained within
.NOTES
I used colors for a screenshot
but if you use Num and DisplayNum you'd be able to have colors and still filter like
$summary | ? GroupNum -eq 4
$summary | ? GroupNum -ge 4 | ? GroupNum -le 7
$summary | ? Exit | Ft
$summary | ? -Not Exit | Ft
#>
$pathToParse = Join-Path $PSScriptRoot 'Examples/Switch_Configuration_Example_01.txt'
filter ColorBool {
# Colorize bools
param( [switch] $NoColor )
if( $NoColor ) { return $_ }
$fg = if( [bool] $_ ) { 'green' } else { 'red' }
$_ | New-Text -fg $fg
}
filter ColorDepth {
<#
.SYNOPSIS
if not an int, return without changing. Otherwise clamp it within: [0, depth)
#>
param(
[int] $MaxDepth = 6,
[rgbColor] $StartColor = '#000000',
[rgbColor] $EndColor = '#dddddd',
[switch] $NoColor
)
if( $NoColor ) { return $_ }
$num = $_ -as [int]
if( -not $num ) { return $_ }
$maxDepth = [math]::Max( 3, $MaxDepth ) # Gradients must be at least >= 3
$clamped = [math]::Min( $num, ( $MaxDepth -1 ) )
$grads = get-gradient -StartColor $StartColor -EndColor $EndColor -Width $MaxDepth
$curGrad = $grads[ $clamped ]
$num | New-Text -bg $curGrad -fg ( Pansies\Get-Complement $curGrad -HighContrast )
}
function ParseGroup {
<#
.SYNOPSIS
parse depth as tree
#>
param(
[string] $Path
)
$Content = Get-Content -Raw -Path $Path
$pattern = @'
(?mxs)
# peek before, for a line with just '!'
(?<=
^\s*!\s*$
\r?\n
)
# the content
(?<Body>
.*?
)
# peek after for a single !
# or the end of file
(?=
(?:\r?\n)?
( ^\s*!\s*$ )
| \Z
)
'@
[int] $GroupNum = 0
$records = [regex]::Matches($text, $pattern) | ForEach-Object {
$block = $_.Value
$body = $_.Groups['Body'].Value
[pscustomobject]@{
GroupNum = $GroupNum++
Body = $Body
HasExit = $Body -match 'exit'
}
}
$records
}
function ParseLine {
param(
[object[]] $ParsedGroup
)
foreach( $Group in $ParsedGroup ) {
$LineNum = 0
$numLinesInGroup = ($Group.Body -split '\r?\n').Count
foreach( $Line in $Group.Body -split '\r?\n' ) {
$null = $Line -match '^\s*'
$IndentDepth = $Matches[0].length
[pscustomobject]@{
Exit = if( $Group.HasExit ) {
'!' | New-Text -bg Magenta
}
GroupNum = $Group.GroupNum
| ColorDepth -MaxDepth $Group.Count
LineNum = $LineNum++
| ColorDepth -MaxDepth $numLinesInGroup
Depth = $IndentDepth
| ColorDepth -MaxDepth 4 -StartColor 'white' 'magenta'
Line = $Line
# RawGroup = $Group
}
}
}
}
$byGroup = ParseGroup -Path $pathToParse
$summary = ParseLine -ParsedGroup $byGroup
$byGroup | ft -AutoSize
$summary | ft -AutoSize
# find groups that *only* have exits in them, somewhere
$onlyExits = $Summary | ? Exit
$onlyExits|Ft
'
example:
$ByGroup[6].Body # each index is a group
$summary[20..40] # each index is a single line
$Summary | ? Exit # only show groups that have exits in them
' | Write-Host -fg 'yellow'
remove-module PSFrameWork, psutil, string -ea ignore
#region define regex templates
$Regex = [ordered]@{}
$Regex.Blocks = @'
(?msxi)
^\s*!\s*$\r?\n
(?<section>
.*?
)(?=
^\s*!\s*$
|
\z
)
'@
#endregion define regex templates
[System.Text.RegularExpressions.RegexOptions] $regexOptions = 'Multiline,SingleLine,IgnoreCase,IgnorePatternWhitespace'
$regexOptions = 'Multiline,IgnoreCase,IgnorePatternWhitespace'
$pathExportMd = Join-Path $PSScriptRoot 'summary.md'
$pathToParse = Join-Path $PSScriptRoot 'Examples/Switch_Configuration_Example_01.txt'
# ($found = Select-String -LiteralPath $pathToParse -Pattern $Regex.Blocks )
$content = gc -raw $pathToParse
$found = [regex]::Matches( <# input: #> $content, <# pattern: #> $regex.Blocks, <# options: #> $regexOptions )
$found | ft -auto
function Md.CodeBlock {
# markdown code blocks, optional with language
param( [string] $LanguageName = '' )
@(
"`n"
'```{0}' -f $LanguageName
$input
'```'
"`n"
) | Join-String -sep "`n"
}
function Md.Header {
# markdown header at a depth
param( [string] $Text, [Alias('Depth')] [int] $Level = 1 )
$prefix = '#' * $Level -join ''
"`n${prefix} ${Text}`n"
}
function Md.Details {
param( [string] $Title = 'Details' )
# markdown collapsible <details><summary> element
$Content = $Input | Md.CodeBlock
@"
<details><summary>${Title} (Click to Expand)
</summary>
${content}
</details>
"@
}
function WriteSummary {
<#
.SYNOPSIS
Dynamically build a markdown file that splits each chunk into a code block and adds a TOC
#>
param(
$Records,
[Alias('Path')]
[string] $ExportPath = $pathExportMd
)
[int] $curId = 0
$totalMatches = $found.count
@(
# write table of contents. example string:
# '- [Index: 9](#index-9)'
'# Results TOC'
foreach( $curId in 0..( $totalMatches - 1 ) ) {
'- [Index: {0}](#index-{0})' -f $curId
}
$curId = 0
Md.Header -Text "Summary: ${ExportPath}"
# todo(fix): hardcoded, should be handled by regex func
gc $pathToParse
| Md.Details -Title "Raw file"
foreach( $item in @( $found.Value ) ) {
$label = "Index: ${curId}"
$curId++
Md.Header -Text $Label -Depth 2
$item | Md.CodeBlock
}
) | Set-Content -Path $pathExportMd
}
WriteSummary -Records $found -ExportPath $PathExportMd

Results TOC

Summary: C:\data\2026-08\pwsh\Parsing-CiscoTemplateFiles\summary.md

Raw file (Click to Expand)
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!                                                                                                   !
! Author: Peter Paskowsky                                                                           !
!                                                                                                   !
! This is a template for Cisco switches                                                             !
! The sample device is a Cisco 3750-X 24-P with ipbase software                                     !
! This configuration has four vlans: transfer, user, server, and guest (which is isolated)          !
! The switch is running a DHCP server for the connected vlans                                       !
! There are trunk ports configured for use with wireless access points                              !
! All traffic is routed to an upstream router, 192.168.0.1                                          !
!                                                                                                   !
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
enable
configure terminal
hostname test1                                                         !sets hostname
enable secret enable
!
service password-encryption 	                                         !enable password encryption in configuration files
service tcp-keepalives-in                                              !kill timed out sessions inbound
service tcp-keepalives-out                                             !kill timed out sessions outbound
no service tcp-small-servers                                           !disable unneeded services
no service udp-small-servers                                           !disable unneeded services
no ip http server                                                      !turn off http configuration server
no ip http secure-server                                               !turn off https configuration server
no ip source-route                                                     !do not allow hosts to specify routes
!
ip name-server 8.8.8.8 8.8.4.4                                         !sets google DNS as name server
ntp server pool.ntp.org                                                !sets pool.ntp.org as NTP server
ip dhcp snooping                                                       !prevents rouge DHCP servers from asigning IP addresses
ip dhcp snooping vlan 101,102,103
login block-for 100 attempts 5 within 100                              !block users from loging in for 100 seconds after 5 invlaid attempts in 100 seconds
logging buffered 4096                                                  !set logging buffer to 4096
no lldp run                                                            !disables lldp
no service pad                                                         !disable unneeded service
no ip finger                                                           !disable unneeded service
no service config                                                      !disable autloading of config files over the network
no boot host dhcp                                                      !disable autloading of config files over the network
scheduler interval 100                                                 !control the maximum amount of time that can elapse without running system processes
vtp mode off                                                           !disbles VLAN Trunking Protocol
logging trap 4                                                         !sets log level
!
snmp-server community mycommunity RO                                   !sets SNMP comminity name
!
aaa new-model                                                          !enables aaa
aaa authentication login default local                                 !sets auth mode to local
ip domain name example.com                                             !sets the domain name
crypto key generate rsa modulus 2048
IP SSH version 2                                                       !enables ssh v2
ip scp server enable                                                   !enables scp
username root privilege 0 secret root
!
line vty 0 15                                                          !configures virtual terminal lines
transport input ssh                                                    !specifies ssh only
exit
!
vlan 100                                                               !creates vlan
name "Transfer Network"                                                !names vlan
exit
!
vlan 101
name "User Network"
exit
!
vlan 102
name "Server Network"
exit
!
vlan 103
name "Guest Network"
exit
!
interface Vlan1
 shutdown                                                              !disables defualt vlan 1
exit
!
interface Vlan100                                                      !creates switch vlan interface and assigns IP
 description "Transfer Network"
 ip address 192.168.0.2 255.255.255.0
exit
!
interface Vlan101
 description "User Network"
 ip address 192.168.1.1 255.255.255.0
exit
!
interface Vlan102
 description "Server Network"
 ip address 192.168.2.1 255.255.255.0
exit
!
interface Vlan103
description "Guest Network"
ip address 192.168.3.1 255.255.255.0
ip access-group 101 in                                                 !prevents guest network from accessing any private addresses
!
access-list 101 deny ip any 10.0.0.0 0.255.255.255
access-list 101 deny ip any 192.168.0.0 0.0.255.255
access-list 101 deny ip any 172.16.0.0 0.15.255.255
access-list 101 permit ip any any
!
ip dhcp excluded-address 192.168.1.0 192.168.1.100                     !prevents dhcp server from assigning addresses 1-100
ip dhcp excluded-address 192.168.2.0 192.168.2.100
ip dhcp excluded-address 192.168.3.0 192.168.3.100
!
ip dhcp pool users                                                     !configures cisco dhcp server, sets network, default gateway, dns server, and search domain
 network 192.168.1.0 255.255.255.0
 default-router 192.168.1.1
 dns-server 8.8.8.8 8.8.4.4
 domain-name example.com
exit
!
ip dhcp pool server
network 192.168.2.0 255.255.255.0
 default-router 192.168.2.1
 dns-server 8.8.8.8 8.8.4.4
 domain-name example.com
exit
!
ip dhcp pool guest
network 192.168.3.0 255.255.255.0
 default-router 192.168.3.1
 dns-server 8.8.8.8 8.8.4.4
 domain-name example.com
exit
!
ip routing                                                                      !enables routing
ip route 0.0.0.0 0.0.0.0 192.168.0.1                                            !sets defualt route to upstream router
!
!
int gi 1/0/1                                                                    !configures ports
 description "Transfer Network Uplink"
 switchport access vlan 100
 switchport mode access
!
int range gi 1/0/2-12
 description "User Port"
 switchport access vlan 101
 switchport mode access
!
int range gi 1/0/13-20
 description "Server Port"
 switchport access vlan 102
 switchport mode access
!
int range gi 1/0/22-24                                                           !configures trunk port for wireless ap, with access to user, server, and guest networks
 description "Wireless AP"
 switchport trunk encapsulation dot1q
 switchport trunk native vlan 102
 switchport trunk allowed vlan 101,102,103
 switchport mode trunk
!

Index: 0

!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!                                                                                                   !
! Author: Peter Paskowsky                                                                           !
!                                                                                                   !
! This is a template for Cisco switches                                                             !
! The sample device is a Cisco 3750-X 24-P with ipbase software                                     !
! This configuration has four vlans: transfer, user, server, and guest (which is isolated)          !
! The switch is running a DHCP server for the connected vlans                                       !
! There are trunk ports configured for use with wireless access points                              !
! All traffic is routed to an upstream router, 192.168.0.1                                          !
!                                                                                                   !
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Index: 1

!
enable
configure terminal
hostname test1                                                         !sets hostname
enable secret enable

Index: 2

!
service password-encryption 	                                         !enable password encryption in configuration files
service tcp-keepalives-in                                              !kill timed out sessions inbound
service tcp-keepalives-out                                             !kill timed out sessions outbound
no service tcp-small-servers                                           !disable unneeded services
no service udp-small-servers                                           !disable unneeded services
no ip http server                                                      !turn off http configuration server
no ip http secure-server                                               !turn off https configuration server
no ip source-route                                                     !do not allow hosts to specify routes

Index: 3

!
ip name-server 8.8.8.8 8.8.4.4                                         !sets google DNS as name server
ntp server pool.ntp.org                                                !sets pool.ntp.org as NTP server
ip dhcp snooping                                                       !prevents rouge DHCP servers from asigning IP addresses
ip dhcp snooping vlan 101,102,103
login block-for 100 attempts 5 within 100                              !block users from loging in for 100 seconds after 5 invlaid attempts in 100 seconds
logging buffered 4096                                                  !set logging buffer to 4096
no lldp run                                                            !disables lldp
no service pad                                                         !disable unneeded service
no ip finger                                                           !disable unneeded service
no service config                                                      !disable autloading of config files over the network
no boot host dhcp                                                      !disable autloading of config files over the network
scheduler interval 100                                                 !control the maximum amount of time that can elapse without running system processes
vtp mode off                                                           !disbles VLAN Trunking Protocol
logging trap 4                                                         !sets log level

Index: 4

!
snmp-server community mycommunity RO                                   !sets SNMP comminity name

Index: 5

!
aaa new-model                                                          !enables aaa
aaa authentication login default local                                 !sets auth mode to local
ip domain name example.com                                             !sets the domain name
crypto key generate rsa modulus 2048
IP SSH version 2                                                       !enables ssh v2
ip scp server enable                                                   !enables scp
username root privilege 0 secret root

Index: 6

!
line vty 0 15                                                          !configures virtual terminal lines
transport input ssh                                                    !specifies ssh only
exit

Index: 7

!
vlan 100                                                               !creates vlan
name "Transfer Network"                                                !names vlan
exit

Index: 8

!
vlan 101
name "User Network"
exit

Index: 9

!
vlan 102
name "Server Network"
exit

Index: 10

!
vlan 103
name "Guest Network"
exit

Index: 11

!
interface Vlan1
 shutdown                                                              !disables defualt vlan 1
exit

Index: 12

!
interface Vlan100                                                      !creates switch vlan interface and assigns IP
 description "Transfer Network"
 ip address 192.168.0.2 255.255.255.0
exit

Index: 13

!
interface Vlan101
 description "User Network"
 ip address 192.168.1.1 255.255.255.0
exit

Index: 14

!
interface Vlan102
 description "Server Network"
 ip address 192.168.2.1 255.255.255.0
exit

Index: 15

!
interface Vlan103
description "Guest Network"
ip address 192.168.3.1 255.255.255.0
ip access-group 101 in                                                 !prevents guest network from accessing any private addresses

Index: 16

!
access-list 101 deny ip any 10.0.0.0 0.255.255.255
access-list 101 deny ip any 192.168.0.0 0.0.255.255
access-list 101 deny ip any 172.16.0.0 0.15.255.255
access-list 101 permit ip any any

Index: 17

!
ip dhcp excluded-address 192.168.1.0 192.168.1.100                     !prevents dhcp server from assigning addresses 1-100
ip dhcp excluded-address 192.168.2.0 192.168.2.100
ip dhcp excluded-address 192.168.3.0 192.168.3.100

Index: 18

!
ip dhcp pool users                                                     !configures cisco dhcp server, sets network, default gateway, dns server, and search domain
 network 192.168.1.0 255.255.255.0
 default-router 192.168.1.1
 dns-server 8.8.8.8 8.8.4.4
 domain-name example.com
exit

Index: 19

!
ip dhcp pool server
network 192.168.2.0 255.255.255.0
 default-router 192.168.2.1
 dns-server 8.8.8.8 8.8.4.4
 domain-name example.com
exit

Index: 20

!
ip dhcp pool guest
network 192.168.3.0 255.255.255.0
 default-router 192.168.3.1
 dns-server 8.8.8.8 8.8.4.4
 domain-name example.com
exit

Index: 21

!
ip routing                                                                      !enables routing
ip route 0.0.0.0 0.0.0.0 192.168.0.1                                            !sets defualt route to upstream router

Index: 22

!

Index: 23

!
int gi 1/0/1                                                                    !configures ports
 description "Transfer Network Uplink"
 switchport access vlan 100
 switchport mode access

Index: 24

!
int range gi 1/0/2-12
 description "User Port"
 switchport access vlan 101
 switchport mode access

Index: 25

!
int range gi 1/0/13-20
 description "Server Port"
 switchport access vlan 102
 switchport mode access

Index: 26

!
int range gi 1/0/22-24                                                           !configures trunk port for wireless ap, with access to user, server, and guest networks
 description "Wireless AP"
 switchport trunk encapsulation dot1q
 switchport trunk native vlan 102
 switchport trunk allowed vlan 101,102,103
 switchport mode trunk

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment