script/cisco-conf.pl untested

filter: Statementstmt 0.0% 0 / 63 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 33 MC/DCmcdc 0.0% 0 / 19 Subroutinesub 0.0% 0 / 5 Podpod n/a totaltotal 0.0% 0 / 144 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 #!/usr/local/bin/perl
2  
3 use strict;
4 use warnings;
5 use Getopt::Long qw(:config no_ignore_case); #bundling
6 use Pod::Usage;
7  
8 use Cisco::Management;
9  
10 my %opt;
11 my ($opt_help, $opt_man);
12  
13 GetOptions(
14 'community=s' => \$opt{community},
15 'dest=s' => \$opt{dest},
16 'source=s' => \$opt{source},
17 'tftp=s' => \$opt{tftp},
18 'write!' => \$opt{write},
19 'help!' => \$opt_help,
20 'man!' => \$opt_man
21 ) or pod2usage(-verbose => 0);
22  
23 pod2usage(-verbose => 1) if defined $opt_help;
24 pod2usage(-verbose => 2) if defined $opt_man;
25  
26 # Make sure at least one host was provided
27 if (!@ARGV) {
28 pod2usage(-verbose => 0, -message => "$0: host required\n")
29 }
30  
31 $opt{community} = $opt{community} || 'private';
32 $opt{dest} = $opt{dest} || 'start';
33 $opt{source} = $opt{source} || 'run';
34  
35 if (($opt{dest} !~ /^run(?:ning)?(?:-config)?$/i) &&
36 ($opt{dest} !~ /^start(?:up)?(?:-config)?$/i) &&
37 ($opt{source} !~ /^run(?:ning)?(?:-config)?$/i) &&
38 ($opt{source} !~ /^start(?:up)?(?:-config)?$/i)) {
39 print "$0: source or dest must be run or start\n";
40 exit 1
41 }
42  
43 if (((($opt{dest} !~ /^run(?:ning)?(?:-config)?$/i) &&
44 ($opt{dest} !~ /^start(?:up)?(?:-config)?$/i)) ||
45 (($opt{source} !~ /^run(?:ning)?(?:-config)?$/i) &&
46 ($opt{source} !~ /^start(?:up)?(?:-config)?$/i))) && (!defined($opt{tftp}))) {
47 print "$0: TFTP required for source or dest not run or start\n";
48 exit 1
49 }
50  
51 for (@ARGV) {
52 print "\n-- $_ --\n";
53  
54 my $cm;
55 if (!defined($cm = Cisco::Management->new(
56 hostname => $_,
57 community => $opt{community}
58 ))) {
59 printf "Error: %s\n", Cisco::Management->error;
60 next
61 }
62  
63 if (!defined($opt{tftp})) {
64 if (defined(my $conf = $cm->config_copy(
65 -source => $opt{source},
66 -dest => $opt{dest}
67 ))) {
68 print "$_: copy $opt{source} $opt{dest}\n"
69 } else {
70 printf "Error: %s\n", Cisco::Management->error
71 }
72 next
73 }
74  
75 if (defined(my $conf = $cm->config_copy(
76 -tftp => $opt{tftp},
77 -source => $opt{source},
78 -dest => $opt{dest}
79 ))) {
80 my $src = $opt{source};
81 my $dest = $opt{dest};
82 if (($opt{dest} !~ /^run(?:ning)?(?:-config)?$/i) &&
83 ($opt{dest} !~ /^start(?:up)?(?:-config)?$/i)) {
84 $dest = $opt{tftp} . ":/" . $opt{dest}
85 } else {
86 $src = $opt{tftp} . ":/" . $opt{source}
87 }
88 printf "$_: copy $src $dest\n";
89 if ($opt{write}) {
90 if (defined($conf = $cm->config_copy())) {
91 print "$_: copy run start\n"
92 } else {
93 printf "Error: %s\n", Cisco::Management->error
94 }
95 }
96 } else {
97 printf "Error: %s\n", Cisco::Management->error
98 }
99 $cm->close()
100 }
101  
102 __END__