blib/lib/Cisco/CopyConfig.pm

filter: Statementstmt 27.4% 25 / 91 Branchbran 11.1% 4 / 36 Conditioncond 7.1% 1 / 14 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 30.0% 6 / 20 Podpod 28.5% 4 / 14 totaltotal 21.8% 40 / 183 CC 26 SCAR 34.4
CC
26
Cov
21%
_xfer
110.0
copy
20.0
merge
20.0
CRAP 31.1
↑ Summary
1 #!/usr/bin/perl -w
2 ###
3 ### $Id: CopyConfig.pm,v 1.3 2004/11/04 22:23:19 aaronsca Exp aaronsca $
4 ###
5 ### -- Manipulate running-config of devices running IOS
6 ###
7  
8 package Cisco::CopyConfig;
9 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Net::SNMP;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 $Cisco::CopyConfig::VERSION = sprintf "%d.%02d", q$Revision: 1.3 $ =~ /(\d+)/g;
13  
14 1 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
15 ###
16 ### -- Create a new CopyConfig object
17  
18 1 my($class) = shift; ## - Object class
19 1 my($self) = bless {
20 'err' => '', ## - Error message
21 'host' => '', ## - Default host
22 'comm' => '', ## - Default community
23 'tmout' => 2, ## - Default timeout
24 'retry' => 2 ## - Default retries
25 }, $class;
26 1 $self->_newarg(@_); ## - Parse arguments
27 1 srand(time() ^ ($$ + ($$ << 15))); ## - Seed random number
28 1 $self->{'snmp'} = $self->open(); ## - Get SNMP object
29 0 $self;
30 }
31  
32 1 ▶sub open {
Subroutine open: called CC=3 SCAR=13.5
Pod undocumented
33 ###
34 ### -- Create SNMP session and return object
35  
36 1 my($self) = shift;
37  
38 1 $self->_newarg(@_); ## - Parse arguments
39 1 ▶ unless(defined($self->{'host'}) && defined($self->{'comm'})){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"host"} and defined $self->{"comm"}) 0 1 1
Condition33%1/3
defined $self->{"host"} and defined $self->{"comm"}
!ll&&!rl&&r
001
MC/DC0%0/2
defined $self->{"host"} and defined $self->{"comm"}
defined $self->{"host"} defined $self->{"comm"}
Truth table33%1/3
A and B
A= defined $self->{"host"}B= defined $self->{"comm"}
ABresult
0X0
100
111
40 0 $self->{'err'} = 'missing hostname or community string';
41 0 return undef;
42 }
43 $self->{'snmp'} = Net::SNMP->session( ## - Create SNMP object
44 Hostname => $self->{'host'},
45 Community => $self->{'comm'},
46 Timeout => $self->{'tmout'},
47 1 Retries => $self->{'retry'},
48 Version => 1
49 );
50 }
51  
52 0 ▶sub close {
Subroutine close: not called CC=1 SCAR=6.9
Pod undocumented
53 ###
54 ### -- Shut down SNMP session and destroy SNMP object
55  
56 0 my($self) = shift;
57 0 my($status) = $self->{'snmp'}->close();
58  
59 0 $self->{'snmp'} = undef;
60 0 $status;
61 }
62  
63 0 ▶sub copy {
Subroutine copy: not called CC=4 SCAR=30.0
Pod documented
64 ###
65 ### -- Copy a running-config to a tftp server file
66  
67 0 my($self) = shift;
68 0 ▶ my($addr) = shift || return undef;
Condition0%0/2
shift() || (return undef)
l!l
00
MC/DC0%0/1
shift() || (return undef)
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
69 0 ▶ my($file) = shift || return undef;
Condition0%0/2
shift() || (return undef)
l!l
00
MC/DC0%0/1
shift() || (return undef)
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
70  
71 0 ▶ unless($self->_cktftp($addr, $file)){
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_cktftp($addr, $file)) 0 0 0
72 0 return undef;
73 }
74 0 $self->{'rand'} = int(rand(1 << 24));
75 0 $self->_xfer(
76 $self->ccCopyProtocol(1),
77 $self->ccCopySourceFileType(4),
78 $self->ccCopyDestFileType(1),
79 $self->ccCopyServerAddress($addr),
80 $self->ccCopyFileName($file),
81 $self->ccCopyEntryRowStatus(4)
82 );
83 }
84  
85 0 ▶sub merge {
Subroutine merge: not called CC=4 SCAR=30.0
Pod documented
86 ###
87 ### -- Merge a tftp server file into a running-config
88  
89 0 my($self) = shift;
90 0 ▶ my($addr) = shift || return undef;
Condition0%0/2
shift() || (return undef)
l!l
00
MC/DC0%0/1
shift() || (return undef)
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
91 0 ▶ my($file) = shift || return undef;
Condition0%0/2
shift() || (return undef)
l!l
00
MC/DC0%0/1
shift() || (return undef)
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
92  
93 0 ▶ unless($self->_cktftp($addr, $file)){
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_cktftp($addr, $file)) 0 0 0
94 0 return undef;
95 }
96 0 $self->{'rand'} = int(rand(1 << 24));
97 0 $self->_xfer(
98 $self->ccCopyProtocol(1),
99 $self->ccCopySourceFileType(1),
100 $self->ccCopyServerAddress($addr),
101 $self->ccCopyFileName($file),
102 $self->ccCopyDestFileType(4),
103 $self->ccCopyEntryRowStatus(4)
104 );
105 }
106  
107 0 ▶sub error {
Subroutine error: not called CC=2 SCAR=17.9
Pod documented
108 ###
109 ### -- Return last error message
110  
111 0 my($self) = shift;
112  
113 0 ▶ defined($self->{err}) ? $self->{err} : '' ;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'err'} ? : 0 0 0
114 }
115  
116 2 ▶sub _newarg {
Subroutine _newarg: called CC=6 SCAR=22.7
117 ###
118 ### -- Parse new object arguments
119  
120 2 my($self) = shift;
121 2 my(%arg) = @_;
122  
123 2 foreach(keys %arg){
124 2 ▶ $self->{'host'} = $arg{$_}, next if /^Host$/oi; ## - SNMP host
Branch: 1 branch
BranchTrueFalseTotal
if /^Host$/io 1 1 2
125 1 ▶ $self->{'comm'} = $arg{$_}, next if /^Comm$/oi; ## - SNMP community
Branch: 1 branch
BranchTrueFalseTotal
if /^Comm$/io 1 0 1
126 0 ▶ $self->{'tmout'} = $arg{$_}, next if /^tmout$/oi; ## - SNMP timeout
Branch: 1 branch
BranchTrueFalseTotal
if /^tmout$/io 0 0 0
127 0 ▶ $self->{'retry'} = $arg{$_}, next if /^Retry$/oi; ## - SNMP timeout
Branch: 1 branch
BranchTrueFalseTotal
if /^Retry$/io 0 0 0
128 }
129 }
130  
131 0 ▶sub _cktftp {
Subroutine _cktftp: not called CC=3 SCAR=24.8
132 ###
133 ### -- Check tftp arguments
134  
135 0 my($self) = shift;
136 0 my($addr) = shift;
137 0 my($file) = shift;
138  
139 0 ▶ if ($addr !~ /^[\d\.]+$/ || !defined(inet_aton($addr))) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($addr =~ /^[\d\.]+$/ and defined inet_aton($addr)) 0 0 0
Condition0%0/3
$addr =~ /^[\d\.]+$/ and defined inet_aton($addr)
!ll&&!rl&&r
000
MC/DC0%0/2
$addr =~ /^[\d\.]+$/ and defined inet_aton($addr)
$addr =~ /^[\d\.]+$/ defined inet_aton($addr)
Truth table0%0/3
A and B
A= $addr =~ /^[\d\.]+$/B= defined inet_aton($addr)
ABresult
0X0
100
111
140 0 $self->{err} = 'invalid tftp server address';
141 0 return 0;
142 }
143 0 1;
144 }
145  
146 0 ▶sub _xfer {
Subroutine _xfer: not called CC=10 SCAR=47.0
147 ###
148 ### -- Do actual tftp transfer
149  
150 0 my($self) = shift;
151 0 my(@oids) = @_; ## - OIDs to use
152 0 my($snmp) = $self->{'snmp'}; ## - Net::SNMP subclass
153 0 my($answer) = ''; ## - SNMP answer
154 0 my($status) = 0; ## - SNMP xfer status
155  
156 0 $snmp->set_request(@oids); ## - Start xfer
157 0 ▶ if ($snmp->error()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($snmp->error) 0 0 0
158 0 $self->{'err'} = $snmp->error();
159 0 return 0;
160 }
161 0 ▶ while($status <= 2){
Branch: 1 branch
BranchTrueFalseTotal
if ($status <= 2) 0 0 0
162 0 $answer = $snmp->get_request($self->ccCopyState());
163 0 $status = $answer->{$self->ccCopyState()};
164 0 ▶ if ($status == 3) { ## Xfer succeeded
Branch: 1 branch
BranchTrueFalseTotal
if ($status == 3) 0 0 0
165 0 last;
166 }
167 0 ▶ if ($status == 4) { ## Xfer failed
Branch: 1 branch
BranchTrueFalseTotal
if ($status == 4) 0 0 0
168 0 $answer = $snmp->get_request($self->ccCopyFailCause());
169 0 $status = $answer->{$self->ccCopyFailCause()};
170  
171 0 ▶ $self->{'err'} = 'unknown error' if $status == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $status == 1 0 0 0
172 0 ▶ $self->{'err'} = 'file access error' if $status == 2;
Branch: 1 branch
BranchTrueFalseTotal
if $status == 2 0 0 0
173 0 ▶ $self->{'err'} = 'tftp timeout' if $status == 3;
Branch: 1 branch
BranchTrueFalseTotal
if $status == 3 0 0 0
174 0 ▶ $self->{'err'} = 'out of memory' if $status == 4;
Branch: 1 branch
BranchTrueFalseTotal
if $status == 4 0 0 0
175 0 ▶ $self->{'err'} = 'no configuration' if $status == 5;
Branch: 1 branch
BranchTrueFalseTotal
if $status == 5 0 0 0
176 0 return 0;
177 }
178 0 sleep(2);
179 }
180 0 $snmp->set_request($self->ccCopyEntryRowStatus(6)); ## - Clear entry row
181 0 1;
182 }
183  
184 ### -- OIDs taken from CISCO-CONFIG-COPY-MIB-V1SMI.my
185 ###
186 0 ▶sub ccCopyProtocol {
Subroutine ccCopyProtocol: not called CC=1 SCAR=6.9
Pod undocumented
187 0 ('1.3.6.1.4.1.9.9.96.1.1.1.1.2.' . $_[0]->{'rand'}, INTEGER, $_[1])
188 }
189 0 ▶sub ccCopySourceFileType {
Subroutine ccCopySourceFileType: not called CC=1 SCAR=6.9
Pod undocumented
190 0 ('1.3.6.1.4.1.9.9.96.1.1.1.1.3.' . $_[0]->{'rand'}, INTEGER, $_[1])
191 }
192 0 ▶sub ccCopyDestFileType {
Subroutine ccCopyDestFileType: not called CC=1 SCAR=6.9
Pod undocumented
193 0 ('1.3.6.1.4.1.9.9.96.1.1.1.1.4.' . $_[0]->{'rand'}, INTEGER, $_[1])
194 }
195 0 ▶sub ccCopyServerAddress {
Subroutine ccCopyServerAddress: not called CC=1 SCAR=6.9
Pod undocumented
196 0 ('1.3.6.1.4.1.9.9.96.1.1.1.1.5.' . $_[0]->{'rand'}, IPADDRESS, $_[1])
197 }
198 0 ▶sub ccCopyFileName {
Subroutine ccCopyFileName: not called CC=1 SCAR=6.9
Pod undocumented
199 0 ('1.3.6.1.4.1.9.9.96.1.1.1.1.6.' . $_[0]->{'rand'}, OCTET_STRING, $_[1])
200 }
201 0 ▶sub ccCopyEntryRowStatus {
Subroutine ccCopyEntryRowStatus: not called CC=1 SCAR=6.9
Pod undocumented
202 0 ('1.3.6.1.4.1.9.9.96.1.1.1.1.14.' . $_[0]->{'rand'}, INTEGER, $_[1])
203 }
204 0 ▶sub ccCopyState {
Subroutine ccCopyState: not called CC=1 SCAR=6.9
Pod undocumented
205 0 '1.3.6.1.4.1.9.9.96.1.1.1.1.10.' . $_[0]->{'rand'}
206 }
207 0 ▶sub ccCopyFailCause {
Subroutine ccCopyFailCause: not called CC=1 SCAR=6.9
Pod undocumented
208 0 '1.3.6.1.4.1.9.9.96.1.1.1.1.13.' . $_[0]->{'rand'}
209 }
210 1 1; ## - Needed for module
211  
212 __END__
↑ Summary