Powershell и regex для анализа конфигурации и отображения интерфейса с настроенным ospf - PullRequest
0 голосов
/ 08 марта 2019

Помогите пожалуйста. Я хочу, чтобы вывод отображал

interface vlan1 is not configured with ospf
interface vlan3 is configured with ospf
interface vlan7 is configured with ospf

но вывод, который я получил при запуске этого скрипта ниже,

interface vlan1 interface vlan3 interface vlan7 is configured with ospf

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

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

1 Ответ

0 голосов
/ 08 марта 2019

Ваш подход слишком сложен в IMO,

  1. разбить файл конфигурации на части (интерфейсы), разделенные !,
  2. проверкой каждого отдельного интерфейса на наличие строки ospf,
  3. выберите первую строку текущего интерфейса и выведите результат теста.

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

Пример вывода:

> 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
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...