I'm cleaning up an
old script with newfound knowledge.
Part of this cleanup is to move user-modified parameters to an external file.
&CONFEXT is supposed to populate $timeout, @targets, and @command from the file ciscoconf.conf.
As the Dumps show, CONFINT isn't fetching all the info into the appropriate arrays.
I think my "push" syntax is good, and suspect my control structure is *not*.
Would any wise monk(s) care to show me the error of my ways?
- ciscoconf.pl - the script itself
- CONFINT - *working* function that reads config parameters from within script itself
- CONFEXT - *broken* function that tries to read same config parameters from external config file
- ciscoconf.conf - external config file
- ciscoconfint.dump - Data::Dumper output from CONFINT
- ciscoconfext.dump - Data::Dumper output from CONFEXT
Hopefully relevant sections of code follow:
tired groggy and quite loopy,
Don
striving for Perl Adept
(it's pronounced "why-bick")
######################################################################
+###
# Begin small slice of ciscoconf.pl
use Data::Dumper; #only for testing
use vars qw(
$section
$timeout @timeout
$target @targets
$command @commands
);
my $conf = 'ciscoconf.conf'; # list of target devices and
+commands
my $tmp = 'ciscoconf.tmp';
my $log = 'ciscoconf.log';
#&CONFINT();
&CONFEXT();
----------------------------------------------------------------------
+---
sub CONFINT {
$timeout = '15';
@targets = ('192.168.1.27','172.16.0.138' );
@commands = (
'sho clo',
'set leng 0',
'term leng 0',
'sho clo',
'disa',
);
open (DUMP, "> ciscoconfint.dump") or die "Error opening ciscocon
+fint.dump for write: $!";
print DUMP Dumper(@timeout), "\n";
print DUMP Dumper(@targets), "\n";
print DUMP Dumper(@commands), "\n";
close (DUMP) or die "Error closing ciscoconfint.dump after write:
+ $!";
}
----------------------------------------------------------------------
+---
sub CONFEXT {
open (CONF, "<$conf") or die "Error opening $conf for read: $!";
while (<CONF>) {
next if /^#/ or /^s.*$/ or /^!/; # ignore comment lines
chomp;
if (/^\[([^\]]+)\]/) {
$section = $1;
next;
}
else {
if ($section =~ /timeout/i) {
push @timeout,$_;
}
if ($section =~ /targets/i) {
push @targets,$_;
}
if ($section =~ /commands/i) {
push @commands,$_;
}
}
}
close (CONF) or die " ciscoconf.pl: Error closing $conf after re
+ad: $!";
open (DUMP, "> ciscoconfext.dump") or die "Error opening ciscocon
+fext.dump for write: $!";
print DUMP Dumper(@timeout), "\n";
print DUMP Dumper(@targets), "\n";
print DUMP Dumper(@commands), "\n";
close (DUMP) or die "Error closing ciscoconfext.dump after write:
+ $!";
}
# End small slice of ciscoconf.pl
######################################################################
+###
# begin ciscoconf.conf
[Timeout]
15
[Targets]
192.168.1.27
172.16.0.138
[Commands]
sho clo
set leng 0
term leng 0
disa
sho clo
# end ciscoconf.conf
######################################################################
+###
# begin ciscoconfext.dump
$VAR1 = 15;
$VAR2 = '';
$VAR1 = '192.168.1.27';
$VAR2 = '172.16.0.138';
$VAR3 = '';
$VAR1 = 'term leng 0';
$VAR2 = 'disa';
# end ciscoconfext.dump
######################################################################
+###
# begin ciscoconfint.dump
$VAR1 = 15;
$VAR1 = '192.168.1.27';
$VAR2 = '172.16.0.138';
$VAR1 = 'sho clo';
$VAR2 = 'set leng 0';
$VAR3 = 'term leng 0';
$VAR4 = 'sho clo';
$VAR5 = 'disa';
# end ciscoconfint.dump
######################################################################
+###