首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Powershell和regex用于解析配置和显示接口,配置了ospf

Powershell和regex用于解析配置和显示接口,配置了ospf
EN

Stack Overflow用户
提问于 2019-03-08 04:25:30
回答 1查看 75关注 0票数 0

请帮帮忙。我要显示输出

代码语言:javascript
复制
interface vlan1 is not configured with ospf
interface vlan3 is configured with ospf
interface vlan7 is configured with ospf

但是我在运行下面的脚本时得到的输出是

代码语言:javascript
复制
interface vlan1 interface vlan3 interface vlan7 is configured with ospf
代码语言:javascript
复制
$interface = select-string -path c:\doc\config.txt -pattern "interface\vlan\d{1,3} -context 0, 3

$ospf = Select-string -inputobject $interface -pattern "ip ospf message-digest.*" | % {$_.matches.value}
if ($ospf -ne $null)
{
   $int = $ interface | select -expandproperty line #don't want to show line#
   write-host "$int is configured with ospf"
}
else
  {
    $int = $ interface | select -expandproperty line #don't want to show line#
     Write-host "$int is not configured with ospf"
  }
代码语言:javascript
复制
interface Vlan1
 no ip address
 shutdown
!
interface Vlan3
 ip ospf message-digest-key 100 md5 7 aa93naf
!
interface Vlan7
 standby 38 preempt
 ip ospf message-digest-key 100 md5 7 asf9394
EN

回答 1

Stack Overflow用户

发布于 2019-03-08 14:16:33

你的方法太复杂了,

  1. 将配置文件拆分为由!分隔的块(接口),
  2. 检查ospf字符串的每个接口,
  3. 选择当前接口的第一行并输出测试结果。
代码语言:javascript
复制
## Q:\Test\2019\03\08\SO_55056710.ps1
$interfaces = (Get-Content .\config.txt -Raw) -split "!`r?`n"

foreach($interface in $interfaces){
    if ($interface -match "ip ospf message-digest"){
        $ospf = "is"} else {$ospf = "is not"}
    "{0} {1} configured with ospf" -f ($interface -split "`r?`n")[0],$ospf
}

样本输出:

代码语言:javascript
复制
> Q:\Test\2019\03\08\SO_55056710.ps1
interface Vlan1 is not configured with ospf
interface Vlan3 is configured with ospf
interface Vlan7 is configured with ospf
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55056710

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档