line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Device::Network::ConfigParser; |
2
|
|
|
|
|
|
|
# ABSTRACT: A harness for parsing network device configuration. |
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
56770
|
use 5.006; |
|
1
|
|
|
|
|
3
|
|
6
|
1
|
|
|
1
|
|
5
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
19
|
|
7
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
45
|
|
8
|
1
|
|
|
1
|
|
347
|
use Modern::Perl; |
|
1
|
|
|
|
|
8124
|
|
|
1
|
|
|
|
|
7
|
|
9
|
1
|
|
|
1
|
|
719
|
use Getopt::Long; |
|
1
|
|
|
|
|
10991
|
|
|
1
|
|
|
|
|
6
|
|
10
|
1
|
|
|
1
|
|
484
|
use Pod::Usage; |
|
1
|
|
|
|
|
42887
|
|
|
1
|
|
|
|
|
191
|
|
11
|
1
|
|
|
1
|
|
531
|
use Perl6::Slurp; |
|
1
|
|
|
|
|
1652
|
|
|
1
|
|
|
|
|
9
|
|
12
|
1
|
|
|
1
|
|
389
|
use Module::Load; |
|
1
|
|
|
|
|
867
|
|
|
1
|
|
|
|
|
6
|
|
13
|
1
|
|
|
1
|
|
487
|
use Data::Dumper; |
|
1
|
|
|
|
|
4906
|
|
|
1
|
|
|
|
|
72
|
|
14
|
1
|
|
|
1
|
|
21
|
use Scalar::Util qw{reftype}; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
42
|
|
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
|
17
|
1
|
|
|
1
|
|
5
|
use Exporter qw{import}; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
518
|
|
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
our @EXPORT_OK = qw{app}; |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
sub app { |
23
|
0
|
|
|
0
|
1
|
|
local @ARGV = @_; |
24
|
0
|
|
|
|
|
|
my %args; |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
GetOptions( |
27
|
|
|
|
|
|
|
"vendor=s" => \$args{vendor}, |
28
|
|
|
|
|
|
|
"type=s" => \$args{type}, |
29
|
|
|
|
|
|
|
"format=s" => \$args{format}, |
30
|
|
|
|
|
|
|
"output=s" => \$args{output} |
31
|
0
|
0
|
|
|
|
|
) or pod2usage(2); |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
# Set the defaults |
34
|
0
|
|
0
|
|
|
|
$args{vendor} //= 'CheckPoint'; |
35
|
0
|
|
0
|
|
|
|
$args{type} //= 'Gaia'; |
36
|
0
|
|
0
|
|
|
|
$args{format} //= 'raw'; |
37
|
0
|
|
0
|
|
|
|
$args{output} //= '-'; # STDOUT |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
# Load the module specified by the command line parameters |
41
|
0
|
|
|
|
|
|
my $parser_module_name = "Device::Network::ConfigParser::$args{vendor}::$args{type}"; |
42
|
0
|
|
|
|
|
|
load $parser_module_name, qw{get_parser get_output_drivers parse_config post_process}; |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
# Check the exports |
45
|
0
|
0
|
0
|
|
|
|
if (!defined &get_parser || |
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
46
|
|
|
|
|
|
|
!defined &get_output_drivers || |
47
|
|
|
|
|
|
|
!defined &parse_config || |
48
|
|
|
|
|
|
|
!defined &post_process) { |
49
|
0
|
|
|
|
|
|
die "$parser_module_name does not export all required subroutines\n"; |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
# Retrieve the parser and the output drivers from the module. If 'raw' doesn't exist, |
53
|
|
|
|
|
|
|
# it's populated with the default sub from this package. |
54
|
|
|
|
|
|
|
# The active driver is then selected for use. |
55
|
0
|
|
|
|
|
|
my $parser = get_parser(); |
56
|
0
|
|
|
|
|
|
my $output_drivers = get_output_drivers(); |
57
|
0
|
|
0
|
|
|
|
$output_drivers->{raw} //= \&_default_raw_output_driver; |
58
|
0
|
|
|
|
|
|
my $active_output_driver = $output_drivers->{ $args{format} }; |
59
|
|
|
|
|
|
|
|
60
|
0
|
0
|
0
|
|
|
|
if (!defined $active_output_driver || reftype $active_output_driver ne 'CODE' ) { |
61
|
0
|
|
|
|
|
|
die "'$args{format}' is not a valid output driver for $parser_module_name\n"; |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
# If there are no files specified, we slurp from STDIN |
65
|
0
|
0
|
|
|
|
|
push @ARGV, \*STDIN if !@ARGV; |
66
|
|
|
|
|
|
|
|
67
|
0
|
|
|
|
|
|
for my $config_filename (@ARGV) { |
68
|
|
|
|
|
|
|
# Read in the configuration |
69
|
0
|
|
|
|
|
|
my $raw_config = slurp $config_filename; |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
# Change the name to something sensible so it isn't stringified to something like 'GLOB(0x17c7350)' |
72
|
0
|
0
|
0
|
|
|
|
$config_filename = 'STDIN' if reftype($config_filename) && reftype($config_filename) eq 'GLOB'; |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
# Call the parser imported from the module. |
75
|
0
|
|
|
|
|
|
my $parsed_config = parse_config($parser, $raw_config); |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
# Perform any post-processing. |
78
|
0
|
|
|
|
|
|
my $post_processed_config = post_process($parsed_config); |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
# Open the file, which could be STDOUT |
81
|
0
|
|
|
|
|
|
my $fh; |
82
|
0
|
0
|
|
|
|
|
if ($args{output} eq '-') { |
83
|
0
|
|
|
|
|
|
local *STDOUT; |
84
|
0
|
|
|
|
|
|
$fh = \*STDOUT; |
85
|
|
|
|
|
|
|
} else { |
86
|
|
|
|
|
|
|
# Replace the placeholder with the filename |
87
|
0
|
|
|
|
|
|
my ($outfile) = $args{output} =~ s{%in_file%}{$config_filename}rxms; |
88
|
0
|
0
|
|
|
|
|
open($fh, ">>:encoding(UTF-8)", $outfile) or die "Unable to open output file '$outfile': $!\n"; |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
# Call the output driver. |
92
|
0
|
|
|
|
|
|
$active_output_driver->($fh, $config_filename, $post_processed_config); |
93
|
|
|
|
|
|
|
|
94
|
0
|
0
|
|
|
|
|
close($fh) unless $args{output} eq '-'; |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
0
|
|
|
|
|
|
return 0; |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
sub _default_raw_output_driver { |
102
|
0
|
|
|
0
|
|
|
my ($fh, $filename, $parsed_config) = @_; |
103
|
0
|
|
|
|
|
|
print $fh Dumper($parsed_config); |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
1; # End of Device::Network::ConfigParser |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
__END__ |