Perl的哪条线路属于我有以下输出,

类别

:Perl的哪条线路属于我有以下输出,

dis vlan 100 

VLAN ID: 100

VLAN Type: static

Route Interface: configured

IP Address: x.x.x.

Subnet Mask: y.y.y.y

Description: Text

Name: SomeMoreText

Tagged Ports:

Bridge-Aggregation1 Bridge-Aggregation2 Bridge-Aggregation3

Bridge-Aggregation6

Ten-GigabitEthernet1/0/18

Ten-GigabitEthernet1/0/20

Ten-GigabitEthernet1/0/21

Untagged Ports:

Ten-GigabitEthernet1/0/1

Ten-GigabitEthernet1/0/2

,我试图映射哪些端口知道该VLAN,以什么样的方式(标记或未标记)。我在确定端口是标记还是未标记时遇到了一些问题。代码:

foreach my $specificVlanLine (split "\n",$getSpecificVlanValue){ 

chomp $specificVlanLine ;

if ($specificVlanLine =~ m/dis vlan/){

#do nothing

} else {

if (index($specificVlanLine, ":") != -1) {

my @specificVlanSplit = split(":", $specificVlanLine) ;

$specificVlanSplit[0] =~ s/\s+//g;

$data{$vlanID}{$specificVlanSplit[0]} = $specificVlanSplit[1] ;

} elsif ($specificVlanLine =~ m/\d+\/\d+\/\d+/) {

$specificVlanLine =~ s/\s+//g;

$data{$vlanID}{"TaggedPorts"} .= "$specificVlanLine \n" ;

}

}

$getSpecificVlanValue是我开始这个问题的完整输出。

问题是每个接口都放在taggedPorts中。这是有道理的,但我怎么能确定哪个以太网端口是标记的,哪些是未标记的?

TaggedPorts' => 'Ten-GigabitEthernet1/0/18 

Ten-GigabitEthernet1/0/20

Ten-GigabitEthernet1/0/21

Ten-GigabitEthernet2/0/18

Ten-GigabitEthernet1/0/1

Ten-GigabitEthernet1/0/2

预期输出:

TaggedPorts' => 'Ten-GigabitEthernet1/0/18 

Ten-GigabitEthernet1/0/20

Ten-GigabitEthernet1/0/21

Ten-GigabitEthernet2/0/18

UntaggedPorts' => 'Ten-GigabitEthernet1/0/1

Ten-GigabitEthernet1/0/2

回答:

创建一个 “标志” 变量记住端口类型(在下面的例子中$模式):

#!/usr/bin/perl

use strict;

use warnings;

my $vlan_id = 'id';

my %data;

my $mode;

while (<DATA>) {

chomp;

if (/Tagged Ports:/) {

$mode = 'tagged';

} elsif (/Untagged Ports:/) {

$mode = 'untagged';

} elsif (my ($key, $value) = /(.*):(.*)/) {

$data{$vlan_id}{$key} = $value;

} elsif (m{\d/\d+/\d} && $mode) {

push @{ $data{$vlan_id}{$mode} }, $_;

}

}

use Data::Dumper; print Dumper \%data;

__DATA__

dis vlan 100

VLAN ID: 100

VLAN Type: static

Route Interface: configured

IP Address: x.x.x.

Subnet Mask: y.y.y.y

Description: Text

Name: SomeMoreText

Tagged Ports:

Bridge-Aggregation1 Bridge-Aggregation2 Bridge-Aggregation3

Bridge-Aggregation6

Ten-GigabitEthernet1/0/18

Ten-GigabitEthernet1/0/20

Ten-GigabitEthernet1/0/21

Untagged Ports:

Ten-GigabitEthernet1/0/1

Ten-GigabitEthernet1/0/2

回答:

一种方式:

#!/usr/bin/perl 

use strict;

use warnings;

use Data::Dumper;

$\="\n";

sub getEmptyIfNull{

my $val = shift;

if (not defined $val){

return "";

}else{

return $val . "\n";

}

}

my %data;

my ($tagged,$untagged)=("","");

while(<DATA>){

chomp;

if (/^Tagged/){

$tagged="Y";

$untagged="N";

}elsif (/^Untagged/){

$untagged="Y";

$tagged="N";

}

if (/\d+\/\d+\/\d+/){

if ($tagged eq "Y"){

$data{"Tagged"} = getEmptyIfNull($data{"Tagged"}) . $_;

}elsif ($untagged eq "Y"){

$data{"Untagged"} = getEmptyIfNull($data{"Untagged"}) . $_;

}

}

}

print Dumper \%data;

__DATA__

dis vlan 100

VLAN ID: 100

VLAN Type: static

Route Interface: configured

IP Address: x.x.x.

Subnet Mask: y.y.y.y

Description: Text

Name: SomeMoreText

Tagged Ports:

Bridge-Aggregation1 Bridge-Aggregation2 Bridge-Aggregation3

Bridge-Aggregation6

Ten-GigabitEthernet1/0/18

Ten-GigabitEthernet1/0/20

Ten-GigabitEthernet1/0/21

Untagged Ports:

Ten-GigabitEthernet1/0/1

Ten-GigabitEthernet1/0/2

以上是 Perl的哪条线路属于我有以下输出, 的全部内容, 来源链接: utcz.com/qa/264506.html

回到顶部