Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
599 views
in Technique[技术] by (71.8m points)

read section of lines from Cisco IOS configuration loaded as text file in powershell

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...