blib/lib/Net/Compare.pm

filter: Statementstmt 77.7% 7 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 83.3% 10 / 12 CC 1 SCAR 0.9
CC
1
Cov
78%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
↑ Summary
1 package Net::Compare;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 0 ▶use File::Slurp;
Subroutine BEGIN: called CC=1 SCAR=2.6
6  
7 use Exporter;
8 use parent 'Exporter';
9  
10 our $VERSION = '0.04';
11 our @EXPORT = qw "compare catch_cmd";
12  
13 sub usage () {
14 say "\n网络配置基线巡检模块使用说明:\n";
15 say "\n加载模块后,需传入compare函数(基线配置路径,设备运行配置路径)\n";
16 say "\n加载模块后,需传入catch_cmd函数(待捕捉的命令行,巡检输出)\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 #------------------------------------------------------------------
179 # 网络配置基线巡检-捕捉命令行输出
180 #------------------------------------------------------------------
181 sub catch_cmd {
182  
183 #状态巡检命令输入
184 my $cmd = shift;
185  
186 #状态巡检命令输出
187 my $config = shift;
188  
189 #检查入参是否完整
190 unless ( $cmd && $config ) {
191 &usage();
192 }
193  
194 #将命令行转数组
195 my @config = map { $_ = s/\n//r } read_file($config);
196  
197 #检验是否命中规则
198 my $hit_cmd = ( grep {/$cmd\s?$/} @config ) ? 1 : 0;
199  
200 #如果未命中命令行则跳出函数
201 return unless $hit_cmd;
202  
203 #巡检命令行命中计数器
204 my ( $result, $hit_count );
205  
206 #遍历状态巡检输出
207 foreach my $cli (@config) {
208  
209 #跳转空白行
210 next unless $cli;
211  
212 #命中CLI后将配置写入并设置状态
213 if ( $cli =~ /$cmd\s?$/ ) {
214  
215 #将命中文本写入
216 push @{$result}, $cli;
217 $hit_count = 1;
218  
219 }
220  
221 #匹配到描述性分隔符退出遍历
222 elsif ( $hit_count && $cli =~ /^(\<(.*)\>|\[.*\])/ ) {
223  
224 #改写命中状态
225 $hit_count = 0;
226  
227 #跳出循环
228 last;
229 }
230  
231 #将非空文本写入
232 elsif ($hit_count) {
233  
234 #将非空文本写入
235 push @{$result}, $cli;
236 }
237 }
238  
239 #返回结果
240 return $result;
241 }
242  
243 1;
↑ Summary