read section of lines from Cisco IOS configuration

2019-01-20 19:24发布

问题:

I'm not expert in powershell but looking to write function in powershell to read section of lines from Cisco IOS configuration loaded in as text file in powershell. there will be multiple sections with different names, each section have parent line with child section as below in configuration. "interface" section have names, "object" section have names and "object-group" section have names to filter them or search. so how to write function to get each section of lines and than parse further to get IPs from that section.

IOS Configuration Example:

interface GigabitEthernet0/0
 description XXX
 speed 1000
 duplex full
 nameif XXX
 security-level 100
 ip address 1.1.1.1 255.255.255.0
!
interface GigabitEthernet0/1
 description YYY
 speed 1000
 duplex full
 nameif YYY
 security-level 100
 ip address 2.2.2.2 255.255.255.0
!
...
object network APP_NETWORK
 subnet 10.10.10.1 255.255.255.0
object network WEB_NETWORK
 host 10.10.10.2
object network DB_NETWORK
 range 10.10.10.3 10.10.10.5
...
object-group network APP_GROUP
 network-object host 10.10.20.1
 network-object host 10.10.20.2
 network-object host 10.10.20.3
object-group network WEB_GROUP
 network-object host 10.10.30.1
 network-object host 10.10.30.2
 network-object host 10.10.30.3
...

For Example I tried following to read all "object-group network" parent sections:

$config = Get-Content $runconfig -ErrorAction stop
$config | where { $_.Contains("object-group network") }

But not able to get its child section along with. how can write function to get parent and child section both. Example1

Get-Section(object-group network APP_GROUP)

should return following

object-group network APP_GROUP
 network-object host 10.10.20.1
 network-object host 10.10.20.2
 network-object host 10.10.20.3

Example2

Get-Section(nameif XXX) OR Get-Section(interface GigabitEthernet0/0)

should return something like this

interface GigabitEthernet0/0
 description XXX
 speed 1000
 duplex full
 nameif XXX
 security-level 100
 ip address 1.1.1.1 255.255.255.0
!

I have searched many blogs, your help or hints will be really appreciated! Thank you!

回答1:

One way to do this is to use a state variable and an appropriate regular expression. Here's an example function:

function Get-Section {
  param(
    [String[]] $configData,
    [String] $sectionName
  )
  $pattern = '(?:^(!)\s*$)|(?:^[\s]+(.+)$)'
  $inSection = $false
  foreach ( $line in $configData ) {
    # Skip empty lines
    if ( $line -match '^\s*$' ) {
      continue
    }
    if ( $line -eq $sectionName ) {
      $inSection = $true
      continue
    }
    if ( $inSection ) {
      if ( $line -match $pattern ) {
        [Regex]::Matches($line, $pattern) | ForEach-Object {
          if ( $_.Groups[1].Success ) {
            $_.Groups[1].Value
          }
          else {
            $_.Groups[2].Value
          }
        }
      }
      else {
        $inSection = $false
      }
      if ( -not $inSection ) {
        break
      }
    }
  }
}

If your example data is in a text file (e.g., config.txt), you could extract the interface GigabitEthernet0/1 section as follows:

$configData = Get-Content "config.txt"
Get-Section $configData 'interface GigabitEthernet0/1'

The output would be:

description YYY
speed 1000
duplex full
nameif YYY
security-level 100
ip address 2.2.2.2 255.255.255.0
!

The function doesn't output the section's name because you already know it (you passed it to the function).



回答2:

If you know the number of lines following the APP_GROUP, which in this case is 3, you can use that value for the -Context switch on Select-String:

$config | Select-String -Pattern '(object-group network APP_GROUP)' -Context 0,3

Which would give:

object-group network APP_GROUP
   network-object host 10.10.20.1
   network-object host 10.10.20.2
   network-object host 10.10.20.3

Edit: Alternative regex solution below since no of lines is dynamic

$section = 'APP_GROUP'
$regex = "(?:object-group\snetwork\s$section\n)(\snetwork-object\shost\s.*\n)+(?=object-group)"

$oneline = Get-Content C:\temp\cisco.txt | Out-String
$oneline -match $regex 
$matches[0]

network-object host 10.10.20.1
network-object host 10.10.20.2
network-object host 10.10.20.3