我有以下输出:
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中。这是有道理的,但我如何确定哪个以太网端口是Tagged,哪个是Untagged?
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
答案 0 :(得分:0)
一种方式:
#!/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
答案 1 :(得分:0)
创建一个“flag”变量来记住端口类型(下例中的$ mode):
#!/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