| 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.03'; | ||
| 11 | our @EXPORT = qw "compare catch_cmd"; | ||
| 12 | |||
| 13 | sub usage () { | ||
| 14 | say "\n网络配置基线巡检模块使用说明:\n"; | ||
| 15 | say | ||
| 16 | "\n加载模块后,需传入compare函数(基线配置路径,设备运行配置路径)\n"; | ||
| 17 | say | ||
| 18 | "\n加载模块后,需传入catch_cmd函数(待捕捉的命令行,巡检输出)\n"; | ||
| 19 | say '\n使用过程如有任何疑问,请联系careline@126.com\n'; | ||
| 20 | exit(-1); | ||
| 21 | } | ||
| 22 | |||
| 23 | #------------------------------------------------------------------ | ||
| 24 | # 网络配置基线巡检功能函数 | ||
| 25 | #------------------------------------------------------------------ | ||
| 26 | sub compare { | ||
| 27 | my $line = shift; | ||
| 28 | my $config = shift; | ||
| 29 | |||
| 30 | unless ( $line && $config ) { | ||
| 31 | &usage(); | ||
| 32 | } | ||
| 33 | |||
| 34 | #处理属组元素换行符\n | ||
| 35 | my @line = map {s/\n//r} read_file($line); | ||
| 36 | my @config = map {s/\n//r} read_file($config); | ||
| 37 | |||
| 38 | #------------------------------------------------------------------ | ||
| 39 | # 重新组合包含代码块的基线检查 | ||
| 40 | #------------------------------------------------------------------ | ||
| 41 | #重新组合运行配置项 | ||
| 42 | my @ret = (); | ||
| 43 | my $check_ret = 0; | ||
| 44 | |||
| 45 | #检查基线命令行是否为代码块 | ||
| 46 | my $cli_code = ( grep {/^\s+[^(\+no|\~|\!|\#)]/} @line ) ? 1 : 0; | ||
| 47 | if ($cli_code) { | ||
| 48 | foreach my $cli (@config) { | ||
| 49 | |||
| 50 | #命中基线首行并设置代码块状态码 | ||
| 51 | if ( $cli eq $line[0] ) { | ||
| 52 | |||
| 53 | #将代码块内代码添加到属组中 | ||
| 54 | push @ret, $cli; | ||
| 55 | $check_ret = 1; | ||
| 56 | } | ||
| 57 | |||
| 58 | #匹配到全局代码行及时结束 | ||
| 59 | elsif ( $check_ret && $cli =~ /^(\S)/ ) { | ||
| 60 | |||
| 61 | #改写代码块状态 | ||
| 62 | $check_ret = 0; | ||
| 63 | last; | ||
| 64 | } | ||
| 65 | |||
| 66 | #将代码块内命令行重组,去除收尾空白字符 | ||
| 67 | elsif ($check_ret) { | ||
| 68 | $cli =~ s/^\s+//; | ||
| 69 | $cli =~ s/^\s*$//; | ||
| 70 | $cli =~ s/\s+$//; | ||
| 71 | next unless $cli; | ||
| 72 | |||
| 73 | #将代码块内代码添加到属组中 | ||
| 74 | push @ret, $cli if $cli; | ||
| 75 | } | ||
| 76 | } | ||
| 77 | } | ||
| 78 | |||
| 79 | #------------------------------------------------------------------ | ||
| 80 | # 基线命中检查逻辑 | ||
| 81 | #------------------------------------------------------------------ | ||
| 82 | my ( $regex, $result, $report ); | ||
| 83 | $report->{"cli_code"} = $cli_code; | ||
| 84 | |||
| 85 | foreach my $cmd (@line) { | ||
| 86 | |||
| 87 | #将代码块内命令行重组,去除收尾空白字符 | ||
| 88 | $cmd =~ s/^\s+//; | ||
| 89 | $cmd =~ s/^\s*$//; | ||
| 90 | $cmd =~ s/\s+$//; | ||
| 91 | next unless $cmd; | ||
| 92 | |||
| 93 | #策略比对结果数据结构 | ||
| 94 | $report->{"same"} ||= []; | ||
| 95 | $report->{"diff"} ||= []; | ||
| 96 | |||
| 97 | #跳过描述性命令行 | ||
| 98 | if ( $cmd =~ /^\s*(\!|\#).*/ ) { | ||
| 99 | next; | ||
| 100 | } | ||
| 101 | |||
| 102 | #捕捉模糊匹配命令行 | ||
| 103 | elsif ( $cmd =~ /^\s*\~\s*\/(?<matched>.*)\// ) { | ||
| 104 | $regex = $+{matched}; | ||
| 105 | |||
| 106 | if ( scalar @ret ) { | ||
| 107 | my $is_matched = 0; | ||
| 108 | foreach my $line (@ret) { | ||
| 109 | if ( $line =~ /$regex/ ) { | ||
| 110 | push @{ $report->{"same"} }, $line; | ||
| 111 | $is_matched = 1; | ||
| 112 | last; | ||
| 113 | } | ||
| 114 | } | ||
| 115 | |||
| 116 | #遍历元素仍未匹配则不存在该基线 | ||
| 117 | push @{ $report->{"diff"} }, $cmd unless $is_matched; | ||
| 118 | } | ||
| 119 | else { | ||
| 120 | my $is_matched = 0; | ||
| 121 | foreach my $line (@config) { | ||
| 122 | $line =~ s/^\s+//; | ||
| 123 | $line =~ s/^\s*$//; | ||
| 124 | $line =~ s/\s+$//; | ||
| 125 | |||
| 126 | if ( $line =~ /$regex/ ) { | ||
| 127 | push @{ $report->{"same"} }, $line; | ||
| 128 | $is_matched = 1; | ||
| 129 | last; | ||
| 130 | } | ||
| 131 | } | ||
| 132 | |||
| 133 | #遍历元素仍未匹配则不存在该基线 | ||
| 134 | push @{ $report->{"diff"} }, $cmd unless $is_matched; | ||
| 135 | } | ||
| 136 | } | ||
| 137 | |||
| 138 | #捕捉需要排除比对的命令 | ||
| 139 | elsif ( $cmd =~ /^\s*\+no\s+\/(?<exclude>.*)\/$/ ) { | ||
| 140 | $regex = $+{exclude}; | ||
| 141 | next; | ||
| 142 | } | ||
| 143 | |||
| 144 | #比对配置是否完全匹配 | ||
| 145 | else { | ||
| 146 | if ( scalar @ret ) { | ||
| 147 | my $is_matched = 0; | ||
| 148 | foreach my $line (@ret) { | ||
| 149 | if ( $cmd eq $line ) { | ||
| 150 | push @{ $report->{"same"} }, $line; | ||
| 151 | $is_matched = 1; | ||
| 152 | last; | ||
| 153 | } | ||
| 154 | } | ||
| 155 | push @{ $report->{"diff"} }, $cmd unless $is_matched; | ||
| 156 | } | ||
| 157 | else { | ||
| 158 | my $is_matched = 0; | ||
| 159 | foreach my $line (@config) { | ||
| 160 | $line =~ s/^\s+//; | ||
| 161 | $line =~ s/^\s*$//; | ||
| 162 | $line =~ s/\s+$//; | ||
| 163 | if ( $cmd eq $line ) { | ||
| 164 | push @{ $report->{"same"} }, $line; | ||
| 165 | $is_matched = 1; | ||
| 166 | last; | ||
| 167 | } | ||
| 168 | } | ||
| 169 | push @{ $report->{"diff"} }, $cmd unless $is_matched; | ||
| 170 | } | ||
| 171 | } | ||
| 172 | } | ||
| 173 | |||
| 174 | #输出报告结果 | ||
| 175 | $report->{"status"} | ||
| 176 | = ( scalar @{ $report->{"diff"} } > 0 ) ? "FAIL" : "PASS"; | ||
| 177 | return $report; | ||
| 178 | } | ||
| 179 | |||
| 180 | #------------------------------------------------------------------ | ||
| 181 | # 网络配置基线巡检-捕捉命令行输出 | ||
| 182 | #------------------------------------------------------------------ | ||
| 183 | sub catch_cmd { | ||
| 184 | |||
| 185 | #状态巡检命令输入 | ||
| 186 | my $cmd = shift; | ||
| 187 | |||
| 188 | #状态巡检命令输出 | ||
| 189 | my $config = shift; | ||
| 190 | |||
| 191 | #检查入参是否完整 | ||
| 192 | unless ( $cmd && $config ) { | ||
| 193 | &usage(); | ||
| 194 | } | ||
| 195 | |||
| 196 | #将命令行转数组 | ||
| 197 | my @config = map { $_ = s/\n//r } read_file($config); | ||
| 198 | |||
| 199 | #检验是否命中规则 | ||
| 200 | my $hit_cmd = ( grep {/$cmd\s?$/} @config ) ? 1 : 0; | ||
| 201 | |||
| 202 | #如果未命中命令行则跳出函数 | ||
| 203 | return unless $hit_cmd; | ||
| 204 | |||
| 205 | #巡检命令行命中计数器 | ||
| 206 | my ( $result, $hit_count ); | ||
| 207 | |||
| 208 | #遍历状态巡检输出 | ||
| 209 | foreach my $cli (@config) { | ||
| 210 | |||
| 211 | #跳转空白行 | ||
| 212 | next unless $cli; | ||
| 213 | |||
| 214 | #命中CLI后将配置写入并设置状态 | ||
| 215 | if ( $cli =~ /$cmd\s?$/ ) { | ||
| 216 | |||
| 217 | #将命中文本写入 | ||
| 218 | push @{$result}, $cli; | ||
| 219 | $hit_count = 1; | ||
| 220 | |||
| 221 | } | ||
| 222 | |||
| 223 | #匹配到描述性分隔符退出遍历 | ||
| 224 | elsif ( $hit_count && $cli =~ /^\<(.*)\>/ ) { | ||
| 225 | |||
| 226 | #改写命中状态 | ||
| 227 | $hit_count = 0; | ||
| 228 | |||
| 229 | #跳出循环 | ||
| 230 | last; | ||
| 231 | } | ||
| 232 | |||
| 233 | #将非空文本写入 | ||
| 234 | elsif ($hit_count) { | ||
| 235 | |||
| 236 | #将非空文本写入 | ||
| 237 | push @{$result}, $cli; | ||
| 238 | } | ||
| 239 | } | ||
| 240 | |||
| 241 | #返回结果 | ||
| 242 | return $result; | ||
| 243 | } | ||
| 244 | |||
| 245 | 1; | ||