line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Net::Autoconfig::Template; |
2
|
|
|
|
|
|
|
|
3
|
6
|
|
|
6
|
|
25281
|
use 5.008008; |
|
6
|
|
|
|
|
24
|
|
|
6
|
|
|
|
|
300
|
|
4
|
6
|
|
|
6
|
|
36
|
use strict; |
|
6
|
|
|
|
|
15
|
|
|
6
|
|
|
|
|
208
|
|
5
|
6
|
|
|
6
|
|
29
|
use warnings; |
|
6
|
|
|
|
|
12
|
|
|
6
|
|
|
|
|
327
|
|
6
|
|
|
|
|
|
|
|
7
|
6
|
|
|
6
|
|
43
|
use base "Net::Autoconfig"; |
|
6
|
|
|
|
|
32
|
|
|
6
|
|
|
|
|
1461
|
|
8
|
6
|
|
|
6
|
|
38
|
use Log::Log4perl qw(:levels); |
|
6
|
|
|
|
|
12
|
|
|
6
|
|
|
|
|
43
|
|
9
|
6
|
|
|
6
|
|
23363
|
use Data::Dumper; |
|
6
|
|
|
|
|
56288
|
|
|
6
|
|
|
|
|
598
|
|
10
|
6
|
|
|
6
|
|
4430
|
use version; our $VERSION = version->new('v1.1.3'); |
|
6
|
|
|
|
|
9571
|
|
|
6
|
|
|
|
|
43
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
################################################################################# |
13
|
|
|
|
|
|
|
## Constants and Global Variables |
14
|
|
|
|
|
|
|
################################################################################# |
15
|
|
|
|
|
|
|
|
16
|
6
|
|
|
6
|
|
779
|
use constant TRUE => 1; |
|
6
|
|
|
|
|
13
|
|
|
6
|
|
|
|
|
391
|
|
17
|
6
|
|
|
6
|
|
32
|
use constant FALSE => 0; |
|
6
|
|
|
|
|
12
|
|
|
6
|
|
|
|
|
362
|
|
18
|
6
|
|
|
6
|
|
35
|
use constant DEFAULT_TIMEOUT => 10; |
|
6
|
|
|
|
|
12
|
|
|
6
|
|
|
|
|
2632
|
|
19
|
|
|
|
|
|
|
|
20
|
6
|
|
|
|
|
6186
|
use constant DEFAULT_CMD => { |
21
|
|
|
|
|
|
|
cmd => "", |
22
|
|
|
|
|
|
|
regex => "", |
23
|
|
|
|
|
|
|
timeout => DEFAULT_TIMEOUT, |
24
|
|
|
|
|
|
|
required => TRUE, |
25
|
6
|
|
|
6
|
|
43
|
}; |
|
6
|
|
|
|
|
13
|
|
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
# directives = keywords that the parser looks for. The corresponding hash value |
28
|
|
|
|
|
|
|
# is the regex to use to look for corresponding data. |
29
|
|
|
|
|
|
|
my $file_directives = { |
30
|
|
|
|
|
|
|
cmd => '.+', |
31
|
|
|
|
|
|
|
'wait' => '\d+', |
32
|
|
|
|
|
|
|
regex => '.*', |
33
|
|
|
|
|
|
|
default => '', |
34
|
|
|
|
|
|
|
required => '', |
35
|
|
|
|
|
|
|
optional => '', |
36
|
|
|
|
|
|
|
device => '\w+', |
37
|
|
|
|
|
|
|
host => '\w+', |
38
|
|
|
|
|
|
|
hostname => '\w+', |
39
|
|
|
|
|
|
|
end => '', |
40
|
|
|
|
|
|
|
}; |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
################################################################################# |
45
|
|
|
|
|
|
|
# Methods |
46
|
|
|
|
|
|
|
################################################################################# |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
############################################################ |
49
|
|
|
|
|
|
|
# Public Methods |
50
|
|
|
|
|
|
|
############################################################ |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
######################################## |
53
|
|
|
|
|
|
|
# new |
54
|
|
|
|
|
|
|
# public method |
55
|
|
|
|
|
|
|
# |
56
|
|
|
|
|
|
|
# create a new Net::Autoconfig::Template |
57
|
|
|
|
|
|
|
# |
58
|
|
|
|
|
|
|
# Takes a filename |
59
|
|
|
|
|
|
|
# |
60
|
|
|
|
|
|
|
# Returns: |
61
|
|
|
|
|
|
|
# a Net::Autoconfig::Template object |
62
|
|
|
|
|
|
|
######################################## |
63
|
|
|
|
|
|
|
sub new { |
64
|
1
|
|
|
1
|
|
28
|
my $invocant = shift; # calling class |
65
|
1
|
|
|
|
|
3
|
my $filename = shift; |
66
|
1
|
|
33
|
|
|
10
|
my $class = ref($invocant) || $invocant; |
67
|
1
|
|
|
|
|
4
|
my $self = {}; |
68
|
1
|
|
|
|
|
14
|
my $log = Log::Log4perl->get_logger('Net::Autoconfig'); |
69
|
1
|
|
|
|
|
48
|
my $template_data; # a hash ref to the template data from the file |
70
|
|
|
|
|
|
|
|
71
|
1
|
|
|
|
|
6
|
$log->debug("Creating new template object"); |
72
|
1
|
|
|
|
|
361
|
$template_data = _get_template_data($filename); |
73
|
0
|
|
0
|
|
|
|
$self = $template_data || {}; |
74
|
|
|
|
|
|
|
|
75
|
0
|
0
|
|
|
|
|
if ($log->is_trace()) |
76
|
|
|
|
|
|
|
{ |
77
|
0
|
|
|
|
|
|
$log->info(Dumper($template_data)); |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
|
80
|
0
|
|
|
|
|
|
return bless $self, $class; |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
############################################################ |
84
|
|
|
|
|
|
|
# Private Methods |
85
|
|
|
|
|
|
|
############################################################ |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
######################################## |
88
|
|
|
|
|
|
|
# _get_template_data |
89
|
|
|
|
|
|
|
# private method |
90
|
|
|
|
|
|
|
# |
91
|
|
|
|
|
|
|
# Load the file and extract data from it. |
92
|
|
|
|
|
|
|
# Returns: |
93
|
|
|
|
|
|
|
# array context => a hash of the template data |
94
|
|
|
|
|
|
|
# scalar context => a hash ref of the template data |
95
|
|
|
|
|
|
|
# failure => undef |
96
|
|
|
|
|
|
|
######################################## |
97
|
|
|
|
|
|
|
sub _get_template_data { |
98
|
|
|
|
|
|
|
my $filename = shift; |
99
|
|
|
|
|
|
|
my $template_data = {}; |
100
|
|
|
|
|
|
|
my $log = Log::Log4perl->get_logger("Net::Autoconfig"); |
101
|
|
|
|
|
|
|
my $current_device; # the name of the current device |
102
|
|
|
|
|
|
|
my $set_defaults_flag; # set if we've seen a "default" directive |
103
|
|
|
|
|
|
|
my $skip_push_cmd; # flag indicates if we shouldn't push this to the list of commands |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
$filename or return; |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
eval |
108
|
|
|
|
|
|
|
{ |
109
|
|
|
|
|
|
|
open(TEMPLATE, "<$filename") || die "Could not open '$filename' for reading: $!"; |
110
|
|
|
|
|
|
|
}; |
111
|
|
|
|
|
|
|
if ($@) |
112
|
|
|
|
|
|
|
{ |
113
|
|
|
|
|
|
|
$log->warn("Loading Template - $@"); |
114
|
|
|
|
|
|
|
return; |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
$template_data->{default} = DEFAULT_CMD; |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
while (my $line = ) |
120
|
|
|
|
|
|
|
{ |
121
|
|
|
|
|
|
|
my $cmds; # the command or the directives + commands |
122
|
|
|
|
|
|
|
$log->trace("Template line: $line"); |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
# Only skip the line if the first character is a "#". |
125
|
|
|
|
|
|
|
# Someone may want to send a " #..." cmd. |
126
|
|
|
|
|
|
|
# Probably not, but you never know. |
127
|
|
|
|
|
|
|
next if $line =~ /^#/; |
128
|
|
|
|
|
|
|
$cmds = _get_template_directives($line); |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
if ($cmds->{default}) |
131
|
|
|
|
|
|
|
{ |
132
|
|
|
|
|
|
|
$set_defaults_flag = TRUE; |
133
|
|
|
|
|
|
|
delete $cmds->{default}; |
134
|
|
|
|
|
|
|
$cmds->{'cmd'} = ''; |
135
|
|
|
|
|
|
|
} |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
foreach my $hostname qw(device host hostname) |
139
|
|
|
|
|
|
|
{ |
140
|
|
|
|
|
|
|
if ($cmds->{$hostname}) |
141
|
|
|
|
|
|
|
{ |
142
|
|
|
|
|
|
|
$current_device = $cmds->{$hostname}; |
143
|
|
|
|
|
|
|
$template_data->{$current_device}->{hostname} = $current_device; |
144
|
|
|
|
|
|
|
delete $cmds->{$hostname}; |
145
|
|
|
|
|
|
|
$skip_push_cmd = TRUE; # If this exists, then don't push this onto the cmd list/stack |
146
|
|
|
|
|
|
|
$log->debug("Now using template named '$current_device'"); |
147
|
|
|
|
|
|
|
} |
148
|
|
|
|
|
|
|
} |
149
|
|
|
|
|
|
|
if ( $cmds->{end} ) |
150
|
|
|
|
|
|
|
{ |
151
|
|
|
|
|
|
|
undef $current_device; |
152
|
|
|
|
|
|
|
undef $set_defaults_flag; |
153
|
|
|
|
|
|
|
} |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
# Add the data to the right place |
156
|
|
|
|
|
|
|
if ( $set_defaults_flag and $current_device ) |
157
|
|
|
|
|
|
|
{ |
158
|
|
|
|
|
|
|
# Add the existing data to the default data. |
159
|
|
|
|
|
|
|
# New data overwrites old data. |
160
|
|
|
|
|
|
|
# Vivify the default data if it does not exist |
161
|
|
|
|
|
|
|
my $default_data; |
162
|
|
|
|
|
|
|
if (not $template_data->{$current_device}->{default}) |
163
|
|
|
|
|
|
|
{ |
164
|
|
|
|
|
|
|
if ($template_data->{default}) |
165
|
|
|
|
|
|
|
{ |
166
|
|
|
|
|
|
|
$template_data->{$current_device}->{default} = $template_data->{default}; |
167
|
|
|
|
|
|
|
} |
168
|
|
|
|
|
|
|
else |
169
|
|
|
|
|
|
|
{ |
170
|
|
|
|
|
|
|
$template_data->{$current_device}->{default} = DEFAULT_CMD; |
171
|
|
|
|
|
|
|
} |
172
|
|
|
|
|
|
|
} |
173
|
|
|
|
|
|
|
$default_data = $template_data->{$current_device}->{default}; |
174
|
|
|
|
|
|
|
$template_data->{$current_device}->{default} = { %$default_data, %$cmds }; |
175
|
|
|
|
|
|
|
$template_data->{$current_device}->{default}->{cmd} = ''; |
176
|
|
|
|
|
|
|
$log->trace("Device Default Data: " . Dumper($template_data->{$current_device}->{default})); |
177
|
|
|
|
|
|
|
} |
178
|
|
|
|
|
|
|
elsif ($set_defaults_flag and not $current_device) |
179
|
|
|
|
|
|
|
{ |
180
|
|
|
|
|
|
|
# Add the existing data to the global template default data. |
181
|
|
|
|
|
|
|
# New data overwrites old data. |
182
|
|
|
|
|
|
|
# Vivify the default data if it does not exist |
183
|
|
|
|
|
|
|
if (not exists $template_data->{default}) |
184
|
|
|
|
|
|
|
{ |
185
|
|
|
|
|
|
|
$template_data->{default} = DEFAULT_CMD; |
186
|
|
|
|
|
|
|
} |
187
|
|
|
|
|
|
|
$template_data->{default} = { %{$template_data->{default}}, %$cmds }; |
188
|
|
|
|
|
|
|
$template_data->{default}->{cmd} = ''; |
189
|
|
|
|
|
|
|
$log->trace("Template Default Data: " . Dumper($template_data->{default})); |
190
|
|
|
|
|
|
|
} |
191
|
|
|
|
|
|
|
elsif ($current_device) |
192
|
|
|
|
|
|
|
{ |
193
|
|
|
|
|
|
|
# Add the commands to the current device. |
194
|
|
|
|
|
|
|
# If there aren't any commands yet, make them |
195
|
|
|
|
|
|
|
if ($skip_push_cmd) |
196
|
|
|
|
|
|
|
{ |
197
|
|
|
|
|
|
|
undef $skip_push_cmd; |
198
|
|
|
|
|
|
|
next; |
199
|
|
|
|
|
|
|
} |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
my $cmds_array; # an array ref to the device commands |
202
|
|
|
|
|
|
|
if ( not defined $template_data->{$current_device}->{cmds}) |
203
|
|
|
|
|
|
|
{ |
204
|
|
|
|
|
|
|
$template_data->{$current_device}->{cmds} = []; |
205
|
|
|
|
|
|
|
} |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
# Create a ref to the cmds array to make it easier to read and write |
208
|
|
|
|
|
|
|
$cmds_array = $template_data->{$current_device}->{cmds}; |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
if (exists $template_data->{$current_device}->{default}) |
211
|
|
|
|
|
|
|
{ |
212
|
|
|
|
|
|
|
$cmds = { %{$template_data->{$current_device}->{default}}, %$cmds }; |
213
|
|
|
|
|
|
|
} |
214
|
|
|
|
|
|
|
else |
215
|
|
|
|
|
|
|
{ |
216
|
|
|
|
|
|
|
$cmds = { %{$template_data->{default}}, %$cmds }; |
217
|
|
|
|
|
|
|
} |
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
# Check to see if someone wanted to copy another template |
220
|
|
|
|
|
|
|
# into this one. I.e. this is useful for entering the same |
221
|
|
|
|
|
|
|
# commands on different devices. E.g. c2960 and c2960g can |
222
|
|
|
|
|
|
|
# use the same commands. |
223
|
|
|
|
|
|
|
# |
224
|
|
|
|
|
|
|
# Procedure: 1) Copy the data into a new hash |
225
|
|
|
|
|
|
|
# 2) Point the current device template to the new data |
226
|
|
|
|
|
|
|
# Notes: |
227
|
|
|
|
|
|
|
# a) This prevents a change in 1 affecting both of them. |
228
|
|
|
|
|
|
|
# b) This allows for extra commands to be added to the current device |
229
|
|
|
|
|
|
|
# without affecting the other device. |
230
|
|
|
|
|
|
|
if ($cmds->{cmd} =~ /^/i) |
231
|
|
|
|
|
|
|
{ |
232
|
|
|
|
|
|
|
my $other_device = lc $1; # The other template to copy into this one |
233
|
|
|
|
|
|
|
my %other_template = %{ $template_data->{$other_device} }; |
234
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
$template_data->{$current_device} = \%other_template; |
236
|
|
|
|
|
|
|
|
237
|
|
|
|
|
|
|
$log->trace("Duplicating template '$other_device' to '$current_device'"); |
238
|
|
|
|
|
|
|
} |
239
|
|
|
|
|
|
|
else |
240
|
|
|
|
|
|
|
{ |
241
|
|
|
|
|
|
|
$log->trace("Adding commands to $current_device: " . Dumper($cmds)); |
242
|
|
|
|
|
|
|
push(@$cmds_array, $cmds); |
243
|
|
|
|
|
|
|
} |
244
|
|
|
|
|
|
|
} |
245
|
|
|
|
|
|
|
else |
246
|
|
|
|
|
|
|
{ |
247
|
|
|
|
|
|
|
# Ignore blank space. But if it looks like someone tried to put a command |
248
|
|
|
|
|
|
|
# outside of a device or default directive, then let them know about it. |
249
|
|
|
|
|
|
|
if ($cmds->{cmd} or ((scalar keys %$cmds) > 1)) |
250
|
|
|
|
|
|
|
{ |
251
|
|
|
|
|
|
|
$log->warn("Unknown device or default object to add commands to."); |
252
|
|
|
|
|
|
|
$log->warn(Dumper($cmds)); |
253
|
|
|
|
|
|
|
} |
254
|
|
|
|
|
|
|
} |
255
|
|
|
|
|
|
|
|
256
|
|
|
|
|
|
|
} |
257
|
|
|
|
|
|
|
|
258
|
|
|
|
|
|
|
if ($log->is_trace()) |
259
|
|
|
|
|
|
|
{ |
260
|
|
|
|
|
|
|
$log->info(Dumper($template_data)); |
261
|
|
|
|
|
|
|
} |
262
|
|
|
|
|
|
|
|
263
|
|
|
|
|
|
|
close(TEMPLATE); |
264
|
|
|
|
|
|
|
|
265
|
|
|
|
|
|
|
# Clean-up the template (i.e. remove the default config entries) |
266
|
|
|
|
|
|
|
delete $template_data->{default}; |
267
|
|
|
|
|
|
|
foreach my $device (keys %$template_data) |
268
|
|
|
|
|
|
|
{ |
269
|
|
|
|
|
|
|
delete $template_data->{$device}->{default}; |
270
|
|
|
|
|
|
|
} |
271
|
|
|
|
|
|
|
|
272
|
|
|
|
|
|
|
return wantarray ? %$template_data : $template_data; |
273
|
|
|
|
|
|
|
} |
274
|
|
|
|
|
|
|
|
275
|
|
|
|
|
|
|
######################################## |
276
|
|
|
|
|
|
|
# _get_template_directives |
277
|
|
|
|
|
|
|
# private method |
278
|
|
|
|
|
|
|
# |
279
|
|
|
|
|
|
|
# For a given string, extract the different |
280
|
|
|
|
|
|
|
# colon separated fields and return a hash |
281
|
|
|
|
|
|
|
# of the applicable directives |
282
|
|
|
|
|
|
|
# |
283
|
|
|
|
|
|
|
# Force all directives to lowercase to prevent |
284
|
|
|
|
|
|
|
# typos or having to remember capitilization. |
285
|
|
|
|
|
|
|
# |
286
|
|
|
|
|
|
|
# Preserve case on commands. |
287
|
|
|
|
|
|
|
# |
288
|
|
|
|
|
|
|
# Returns: |
289
|
|
|
|
|
|
|
# array context => hash of the commands |
290
|
|
|
|
|
|
|
# scalar context => hash ref of the commands |
291
|
|
|
|
|
|
|
# failure => undef |
292
|
|
|
|
|
|
|
######################################## |
293
|
|
|
|
|
|
|
sub _get_template_directives { |
294
|
|
|
|
|
|
|
my $line = shift; |
295
|
|
|
|
|
|
|
my $cmds = {}; # a hash ref of the current commands |
296
|
|
|
|
|
|
|
my $log = Log::Log4perl->get_logger("Net::Autoconfig"); |
297
|
|
|
|
|
|
|
|
298
|
|
|
|
|
|
|
chomp($line); |
299
|
|
|
|
|
|
|
|
300
|
|
|
|
|
|
|
if ($line =~ /^:.*:$/) |
301
|
|
|
|
|
|
|
{ |
302
|
|
|
|
|
|
|
# directives only |
303
|
|
|
|
|
|
|
my @line_directives; |
304
|
|
|
|
|
|
|
$line =~ s/^://; |
305
|
|
|
|
|
|
|
$line =~ s/:$//; |
306
|
|
|
|
|
|
|
$line =~ s/\\:/~!~/g; |
307
|
|
|
|
|
|
|
$line = lc $line; |
308
|
|
|
|
|
|
|
|
309
|
|
|
|
|
|
|
@line_directives = split(":", $line); |
310
|
|
|
|
|
|
|
|
311
|
|
|
|
|
|
|
foreach my $directive (@line_directives) |
312
|
|
|
|
|
|
|
{ |
313
|
|
|
|
|
|
|
&_add_directive($directive, $cmds); |
314
|
|
|
|
|
|
|
} |
315
|
|
|
|
|
|
|
} |
316
|
|
|
|
|
|
|
elsif ($line =~ /^:/) |
317
|
|
|
|
|
|
|
{ |
318
|
|
|
|
|
|
|
# directives followed by a command |
319
|
|
|
|
|
|
|
my @line_directives; |
320
|
|
|
|
|
|
|
my $param; |
321
|
|
|
|
|
|
|
$line =~ s/^://; |
322
|
|
|
|
|
|
|
$line =~ s/\\:/~!~/g; |
323
|
|
|
|
|
|
|
|
324
|
|
|
|
|
|
|
@line_directives = split(":", $line); |
325
|
|
|
|
|
|
|
|
326
|
|
|
|
|
|
|
for (my $elem_count = 0; $elem_count < scalar(@line_directives); $elem_count++) |
327
|
|
|
|
|
|
|
{ |
328
|
|
|
|
|
|
|
$log->trace("Directive $elem_count of " . scalar(@line_directives) |
329
|
|
|
|
|
|
|
. " = " . $line_directives[$elem_count]); |
330
|
|
|
|
|
|
|
|
331
|
|
|
|
|
|
|
# The last value of the array is a command. |
332
|
|
|
|
|
|
|
if ($elem_count == (scalar(@line_directives) - 1)) |
333
|
|
|
|
|
|
|
{ |
334
|
|
|
|
|
|
|
my $cmd; |
335
|
|
|
|
|
|
|
my $param; |
336
|
|
|
|
|
|
|
$cmd = "cmd"; |
337
|
|
|
|
|
|
|
$param = $line_directives[$elem_count]; |
338
|
|
|
|
|
|
|
$param =~ s/~!~/:/g; |
339
|
|
|
|
|
|
|
$param =~ s/^\s*//; |
340
|
|
|
|
|
|
|
$param =~ s/\s*$//; |
341
|
|
|
|
|
|
|
$cmds->{cmd} = $param; |
342
|
|
|
|
|
|
|
$log->trace("Added command: $param"); |
343
|
|
|
|
|
|
|
last; |
344
|
|
|
|
|
|
|
} |
345
|
|
|
|
|
|
|
else |
346
|
|
|
|
|
|
|
{ |
347
|
|
|
|
|
|
|
&_add_directive(lc $line_directives[$elem_count], $cmds); |
348
|
|
|
|
|
|
|
} |
349
|
|
|
|
|
|
|
} |
350
|
|
|
|
|
|
|
} |
351
|
|
|
|
|
|
|
else |
352
|
|
|
|
|
|
|
{ |
353
|
|
|
|
|
|
|
$line =~ s/^\s*//; |
354
|
|
|
|
|
|
|
$line =~ s/\s*$//; |
355
|
|
|
|
|
|
|
$cmds->{cmd} = $line; |
356
|
|
|
|
|
|
|
} |
357
|
|
|
|
|
|
|
|
358
|
|
|
|
|
|
|
return wantarray ? %$cmds : $cmds; |
359
|
|
|
|
|
|
|
} |
360
|
|
|
|
|
|
|
|
361
|
|
|
|
|
|
|
######################################## |
362
|
|
|
|
|
|
|
# _add_directive |
363
|
|
|
|
|
|
|
# private method |
364
|
|
|
|
|
|
|
# |
365
|
|
|
|
|
|
|
# For a given directive, add it to the speified |
366
|
|
|
|
|
|
|
# hash ref. If the directive is invalid, undef |
367
|
|
|
|
|
|
|
# If the directive is valid, return TRUE. |
368
|
|
|
|
|
|
|
# |
369
|
|
|
|
|
|
|
# The return value is only for error checking. The has |
370
|
|
|
|
|
|
|
# ref is modified directly. |
371
|
|
|
|
|
|
|
# |
372
|
|
|
|
|
|
|
# Returns: |
373
|
|
|
|
|
|
|
# valid directive => TRUE |
374
|
|
|
|
|
|
|
# invalid directive => undef |
375
|
|
|
|
|
|
|
######################################## |
376
|
|
|
|
|
|
|
sub _add_directive { |
377
|
|
|
|
|
|
|
my $directive = shift; |
378
|
|
|
|
|
|
|
my $hash = shift; |
379
|
|
|
|
|
|
|
my $log = Log::Log4perl->get_logger("Net::Autoconfig"); |
380
|
|
|
|
|
|
|
my $cmd; |
381
|
|
|
|
|
|
|
my $param; |
382
|
|
|
|
|
|
|
$directive =~ s/~!~/:/g; |
383
|
|
|
|
|
|
|
|
384
|
|
|
|
|
|
|
$log->trace("Directive = '$directive'"); |
385
|
|
|
|
|
|
|
$directive =~ /(\w+)\s*(.*)/; |
386
|
|
|
|
|
|
|
$cmd = $1; |
387
|
|
|
|
|
|
|
$2 and $param = $2; |
388
|
|
|
|
|
|
|
if ($param) |
389
|
|
|
|
|
|
|
{ |
390
|
|
|
|
|
|
|
$param =~ s/^\s*//; |
391
|
|
|
|
|
|
|
$param =~ s/\s*$//; |
392
|
|
|
|
|
|
|
} |
393
|
|
|
|
|
|
|
|
394
|
|
|
|
|
|
|
if (exists $file_directives->{$cmd}) |
395
|
|
|
|
|
|
|
{ |
396
|
|
|
|
|
|
|
my $regex = $file_directives->{$cmd}; |
397
|
|
|
|
|
|
|
if (not $regex) |
398
|
|
|
|
|
|
|
{ |
399
|
|
|
|
|
|
|
# no parameter expected |
400
|
|
|
|
|
|
|
# Special case for required/optional since it toggles a value |
401
|
|
|
|
|
|
|
if ($cmd =~ /required/i) |
402
|
|
|
|
|
|
|
{ |
403
|
|
|
|
|
|
|
$hash->{'required'} = TRUE; |
404
|
|
|
|
|
|
|
$log->trace("Setting command to required: ". TRUE); |
405
|
|
|
|
|
|
|
} |
406
|
|
|
|
|
|
|
elsif ($cmd =~ /optional/i) |
407
|
|
|
|
|
|
|
{ |
408
|
|
|
|
|
|
|
$hash->{'required'} = FALSE; |
409
|
|
|
|
|
|
|
$log->trace("Setting command to optional: ". FALSE); |
410
|
|
|
|
|
|
|
} |
411
|
|
|
|
|
|
|
else |
412
|
|
|
|
|
|
|
{ |
413
|
|
|
|
|
|
|
$hash->{$cmd} = TRUE; |
414
|
|
|
|
|
|
|
} |
415
|
|
|
|
|
|
|
return TRUE; |
416
|
|
|
|
|
|
|
} |
417
|
|
|
|
|
|
|
elsif (not $param) |
418
|
|
|
|
|
|
|
{ |
419
|
|
|
|
|
|
|
$log->warn("No parameter specified for command '$cmd'."); |
420
|
|
|
|
|
|
|
return; |
421
|
|
|
|
|
|
|
} |
422
|
|
|
|
|
|
|
elsif ($param =~ /$regex/) |
423
|
|
|
|
|
|
|
{ |
424
|
|
|
|
|
|
|
if ($cmd =~ /wait/i) |
425
|
|
|
|
|
|
|
{ |
426
|
|
|
|
|
|
|
$hash->{'cmd'} = 'wait'; |
427
|
|
|
|
|
|
|
$hash->{'timeout'} = $param; |
428
|
|
|
|
|
|
|
} |
429
|
|
|
|
|
|
|
else |
430
|
|
|
|
|
|
|
{ |
431
|
|
|
|
|
|
|
$hash->{$cmd} = $param; |
432
|
|
|
|
|
|
|
} |
433
|
|
|
|
|
|
|
$log->trace("Comand '$cmd' with param '$param'"); |
434
|
|
|
|
|
|
|
return TRUE; |
435
|
|
|
|
|
|
|
} |
436
|
|
|
|
|
|
|
else |
437
|
|
|
|
|
|
|
{ |
438
|
|
|
|
|
|
|
$param or $param = ""; |
439
|
|
|
|
|
|
|
$log->warn("Invalid parameter '$param' for command '$cmd'"); |
440
|
|
|
|
|
|
|
return; |
441
|
|
|
|
|
|
|
} |
442
|
|
|
|
|
|
|
} |
443
|
|
|
|
|
|
|
else |
444
|
|
|
|
|
|
|
{ |
445
|
|
|
|
|
|
|
$param or $param = ""; |
446
|
|
|
|
|
|
|
$log->warn("Unknown command: '$cmd' with parameter '$param'"); |
447
|
|
|
|
|
|
|
return; |
448
|
|
|
|
|
|
|
} |
449
|
|
|
|
|
|
|
return; |
450
|
|
|
|
|
|
|
} |
451
|
|
|
|
|
|
|
|
452
|
|
|
|
|
|
|
######################################## |
453
|
|
|
|
|
|
|
# _prefix_to_netmask |
454
|
|
|
|
|
|
|
# |
455
|
|
|
|
|
|
|
# Given a prefix, return the corresponding |
456
|
|
|
|
|
|
|
# netmask. |
457
|
|
|
|
|
|
|
# |
458
|
|
|
|
|
|
|
# Returns: |
459
|
|
|
|
|
|
|
# netmask upon success |
460
|
|
|
|
|
|
|
# undef upon failure |
461
|
|
|
|
|
|
|
######################################## |
462
|
|
|
|
|
|
|
sub _prefix_to_netmask { |
463
|
|
|
|
|
|
|
my $prefix = shift; |
464
|
|
|
|
|
|
|
my $prefix_octets; |
465
|
|
|
|
|
|
|
my $prefix_remainder; |
466
|
|
|
|
|
|
|
my @netmask; |
467
|
|
|
|
|
|
|
|
468
|
|
|
|
|
|
|
($prefix) or return; |
469
|
|
|
|
|
|
|
($prefix =~ /\/\d{1,2}$/) or return; |
470
|
|
|
|
|
|
|
|
471
|
|
|
|
|
|
|
$prefix =~ s/\///; |
472
|
|
|
|
|
|
|
|
473
|
|
|
|
|
|
|
$prefix_octets = int($prefix / 8); |
474
|
|
|
|
|
|
|
$prefix_remainder = ($prefix % 8); |
475
|
|
|
|
|
|
|
|
476
|
|
|
|
|
|
|
my $prefix_values = { |
477
|
|
|
|
|
|
|
0 => "0", |
478
|
|
|
|
|
|
|
1 => "128", |
479
|
|
|
|
|
|
|
2 => "192", |
480
|
|
|
|
|
|
|
3 => "224", |
481
|
|
|
|
|
|
|
4 => "240", |
482
|
|
|
|
|
|
|
5 => "248", |
483
|
|
|
|
|
|
|
6 => "252", |
484
|
|
|
|
|
|
|
7 => "254", |
485
|
|
|
|
|
|
|
8 => "255", |
486
|
|
|
|
|
|
|
}; |
487
|
|
|
|
|
|
|
|
488
|
|
|
|
|
|
|
foreach my $octet (1..4) |
489
|
|
|
|
|
|
|
{ |
490
|
|
|
|
|
|
|
if ($prefix_octets > 0) |
491
|
|
|
|
|
|
|
{ |
492
|
|
|
|
|
|
|
$prefix_octets--; |
493
|
|
|
|
|
|
|
push(@netmask, $prefix_values->{8}); |
494
|
|
|
|
|
|
|
} |
495
|
|
|
|
|
|
|
elsif ($prefix_remainder) |
496
|
|
|
|
|
|
|
{ |
497
|
|
|
|
|
|
|
push(@netmask, $prefix_values->{$prefix_remainder}); |
498
|
|
|
|
|
|
|
$prefix_remainder = 0; |
499
|
|
|
|
|
|
|
} |
500
|
|
|
|
|
|
|
else |
501
|
|
|
|
|
|
|
{ |
502
|
|
|
|
|
|
|
push(@netmask, $prefix_values->{0}); |
503
|
|
|
|
|
|
|
} |
504
|
|
|
|
|
|
|
} |
505
|
|
|
|
|
|
|
return wantarray ? @netmask : join(".", @netmask); |
506
|
|
|
|
|
|
|
} |
507
|
|
|
|
|
|
|
|
508
|
|
|
|
|
|
|
|
509
|
|
|
|
|
|
|
######################################## |
510
|
|
|
|
|
|
|
# _netmask_to_prefix |
511
|
|
|
|
|
|
|
# |
512
|
|
|
|
|
|
|
# Given a netmask, return the corresponding |
513
|
|
|
|
|
|
|
# prefix "/\d{1,2}" |
514
|
|
|
|
|
|
|
# |
515
|
|
|
|
|
|
|
# Returns: |
516
|
|
|
|
|
|
|
# prefix upon success |
517
|
|
|
|
|
|
|
# undef upon failure |
518
|
|
|
|
|
|
|
######################################## |
519
|
|
|
|
|
|
|
sub _netmask_to_prefix { |
520
|
|
|
|
|
|
|
my $netmask = shift; |
521
|
|
|
|
|
|
|
my @netmask; # the octets of the netmask |
522
|
|
|
|
|
|
|
my $prefix = 0; # the prefix form of the netmask |
523
|
|
|
|
|
|
|
my $log = Log::Log4perl->get_logger('Net::Autoconfig'); |
524
|
|
|
|
|
|
|
|
525
|
|
|
|
|
|
|
my %netmask_values = { |
526
|
|
|
|
|
|
|
255 => "8", |
527
|
|
|
|
|
|
|
254 => "7", |
528
|
|
|
|
|
|
|
252 => "6", |
529
|
|
|
|
|
|
|
248 => "5", |
530
|
|
|
|
|
|
|
240 => "4", |
531
|
|
|
|
|
|
|
224 => "3", |
532
|
|
|
|
|
|
|
192 => "2", |
533
|
|
|
|
|
|
|
128 => "1", |
534
|
|
|
|
|
|
|
0 => "0", |
535
|
|
|
|
|
|
|
}; |
536
|
|
|
|
|
|
|
|
537
|
|
|
|
|
|
|
if (! $netmask) |
538
|
|
|
|
|
|
|
{ |
539
|
|
|
|
|
|
|
$log->info("No netmask was specified."); |
540
|
|
|
|
|
|
|
return; |
541
|
|
|
|
|
|
|
} |
542
|
|
|
|
|
|
|
|
543
|
|
|
|
|
|
|
@netmask = split(/\./, $netmask); |
544
|
|
|
|
|
|
|
|
545
|
|
|
|
|
|
|
if ( @netmask != 4) |
546
|
|
|
|
|
|
|
{ |
547
|
|
|
|
|
|
|
$log->info("Invalid netmask. '" . $netmask . "'"); |
548
|
|
|
|
|
|
|
return; |
549
|
|
|
|
|
|
|
} |
550
|
|
|
|
|
|
|
|
551
|
|
|
|
|
|
|
foreach my $octet (@netmask) |
552
|
|
|
|
|
|
|
{ |
553
|
|
|
|
|
|
|
($octet > 255) and $log->info("Netmask octect > 255"); |
554
|
|
|
|
|
|
|
($octet < 0) and $log->info("Netmask octect < 0"); |
555
|
|
|
|
|
|
|
|
556
|
|
|
|
|
|
|
$prefix += $netmask_values{$octet}; |
557
|
|
|
|
|
|
|
} |
558
|
|
|
|
|
|
|
return $prefix; |
559
|
|
|
|
|
|
|
} |
560
|
|
|
|
|
|
|
|
561
|
|
|
|
|
|
|
# Modules must return true. |
562
|
|
|
|
|
|
|
TRUE; |
563
|
|
|
|
|
|
|
|
564
|
|
|
|
|
|
|
|
565
|
|
|
|
|
|
|
__END__ |