blib/lib/Net/Compare.pm

filter: Statementstmt 75.0% 6 / 8 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 81.8% 9 / 11 CC 1 SCAR 0.9
CC
1
Cov
75%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
↑ Summary
1 package Net::Compare;
2  
3 1 ▶use 5.012;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use File::Slurp;
Subroutine BEGIN: called CC=1 SCAR=2.6
6  
7 use Exporter;
8 use parent 'Exporter';
9  
10 our $VERSION = '0.02';
11 our @EXPORT = qw /compare/;
12  
13 sub usage () {
14 say "\n网络配置基线巡检模块使用说明:\n";
15 say
16 "\n加载模块后,需传入compare函数(基线配置路径,设备运行配置路径)\n";
17 say '\n使用过程如有任何疑问,请联系careline@126.com\n';
18 exit(-1);
19 }
20  
21 #------------------------------------------------------------------
22 # 网络配置基线巡检功能函数
23 #------------------------------------------------------------------
24 sub compare {
25 my $line = shift;
26 my $config = shift;
27  
28 unless ( $line && $config ) {
29 &usage();
30 }
31  
32 #处理属组元素换行符\n
33 my @line = map {s/\n//r} read_file($line);
34 my @config = map {s/\n//r} read_file($config);
35  
36 #------------------------------------------------------------------
37 # 重新组合包含代码块的基线检查
38 #------------------------------------------------------------------
39 #重新组合运行配置项
40 my @ret = ();
41 my $check_ret = 0;
42  
43 #检查基线命令行是否为代码块
44 my $cli_code = ( grep {/^\s+[^(\+no|\~|\!|\#)]/} @line ) ? 1 : 0;
45 if ($cli_code) {
46 foreach my $cli (@config) {
47  
48 #命中基线首行并设置代码块状态码
49 if ( $cli eq $line[0] ) {
50  
51 #将代码块内代码添加到属组中
52 push @ret, $cli;
53 $check_ret = 1;
54 }
55  
56 #匹配到全局代码行及时结束
57 elsif ( $check_ret && $cli =~ /^(\S)/ ) {
58  
59 #改写代码块状态
60 $check_ret = 0;
61 last;
62 }
63  
64 #将代码块内命令行重组,去除收尾空白字符
65 elsif ($check_ret) {
66 $cli =~ s/^\s+//;
67 $cli =~ s/^\s*$//;
68 $cli =~ s/\s+$//;
69 next unless $cli;
70  
71 #将代码块内代码添加到属组中
72 push @ret, $cli if $cli;
73 }
74 }
75 }
76  
77 #------------------------------------------------------------------
78 # 基线命中检查逻辑
79 #------------------------------------------------------------------
80 my ( $regex, $result, $report );
81 $report->{"cli_code"} = $cli_code;
82  
83 foreach my $cmd (@line) {
84  
85 #将代码块内命令行重组,去除收尾空白字符
86 $cmd =~ s/^\s+//;
87 $cmd =~ s/^\s*$//;
88 $cmd =~ s/\s+$//;
89 next unless $cmd;
90  
91 #策略比对结果数据结构
92 $report->{"same"} ||= [];
93 $report->{"diff"} ||= [];
94  
95 #跳过描述性命令行
96 if ( $cmd =~ /^\s*(\!|\#).*/ ) {
97 next;
98 }
99  
100 #捕捉模糊匹配命令行
101 elsif ( $cmd =~ /^\s*\~\s*\/(?<matched>.*)\// ) {
102 $regex = $+{matched};
103  
104 if ( scalar @ret ) {
105 my $is_matched = 0;
106 foreach my $line (@ret) {
107 if ( $line =~ /$regex/ ) {
108 push @{ $report->{"same"} }, $line;
109 $is_matched = 1;
110 last;
111 }
112 }
113  
114 #遍历元素仍未匹配则不存在该基线
115 push @{ $report->{"diff"} }, $cmd unless $is_matched;
116 }
117 else {
118 my $is_matched = 0;
119 foreach my $line (@config) {
120 $line =~ s/^\s+//;
121 $line =~ s/^\s*$//;
122 $line =~ s/\s+$//;
123  
124 if ( $line =~ /$regex/ ) {
125 push @{ $report->{"same"} }, $line;
126 $is_matched = 1;
127 last;
128 }
129 }
130  
131 #遍历元素仍未匹配则不存在该基线
132 push @{ $report->{"diff"} }, $cmd unless $is_matched;
133 }
134 }
135  
136 #捕捉需要排除比对的命令
137 elsif ( $cmd =~ /^\s*\+no\s+\/(?<exclude>.*)\/$/ ) {
138 $regex = $+{exclude};
139 next;
140 }
141  
142 #比对配置是否完全匹配
143 else {
144 if ( scalar @ret ) {
145 my $is_matched = 0;
146 foreach my $line (@ret) {
147 if ( $cmd eq $line ) {
148 push @{ $report->{"same"} }, $line;
149 $is_matched = 1;
150 last;
151 }
152 }
153 push @{ $report->{"diff"} }, $cmd unless $is_matched;
154 }
155 else {
156 my $is_matched = 0;
157 foreach my $line (@config) {
158 $line =~ s/^\s+//;
159 $line =~ s/^\s*$//;
160 $line =~ s/\s+$//;
161 if ( $cmd eq $line ) {
162 push @{ $report->{"same"} }, $line;
163 $is_matched = 1;
164 last;
165 }
166 }
167 push @{ $report->{"diff"} }, $cmd unless $is_matched;
168 }
169 }
170 }
171  
172 #输出报告结果
173 $report->{"status"}
174 = ( scalar @{ $report->{"diff"} } > 0 ) ? "FAIL" : "PASS";
175 return $report;
176 }
177  
178 1;
↑ Summary