本文介绍了从作为文本文件加载到 powershell 中的 Cisco IOS 配置中读取行的部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不是 powershell 方面的专家,但希望在 powershell 中编写函数来读取作为文本文件加载到 powershell 中的 Cisco IOS 配置中的行的部分.将有多个具有不同名称的部分,每个部分在配置中都有父行和子部分,如下所示.接口"部分有名称,对象"部分有名称,对象组"部分有名称来过滤或搜索.那么如何编写函数来获取行的每一部分,然后进一步解析以从该部分获取 IP.

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 配置示例:

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") }

但无法获得其子部分.如何编写函数来同时获取父段和子段.示例1

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)

应该返回以下

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

示例 2

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

应该返回这样的东西

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!

推荐答案

一种方法是使用状态变量和适当的正则表达式.这是一个示例函数:

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
      }
    }
  }
}

如果您的示例数据在文本文件中(例如,config.txt),您可以按如下方式提取 interface GigabitEthernet0/1 部分:

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'

输出将是:

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

这篇关于从作为文本文件加载到 powershell 中的 Cisco IOS 配置中读取行的部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 00:46