Codebase list cisco-torch / master include / SNMPDOWNLOAD.pm
master

Tree @master (Download .tar.gz)

SNMPDOWNLOAD.pm @masterraw · history · blame

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
#------------------------------------------------------------------------------- 
# Based Pancho (www.pancho.org)  
# patched for cisco-torch by Arhont Team 
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
$opts ="download";
%types = (
              cisco   => 'Cisco Systems',
            );
$name = "cisco";



sub fetch_conf {
#Initial setup 
my $target =shift;
my $community = shift;
my $port = 161;
my $filename = "$target.conf";           


open(FILEHANDLE, "> $tftprootdir/$filename") || die("cannot open file:". $!);

close(FILEHANDLE);

## resolve fqdn/hostname for tftpserver
if ($tftpserver =~ /[a-zA-Z]/) {
  my $i = gethostbyname($tftpserver);
  $tftpserver = inet_ntoa($i);
}

%args = ( Hostname => $target, Community => $community, Port => $port,
 src => "run", dst => "tftp", tftp => $tftpserver, file => $filename );
 
discover_vendor ($args);
}

sub create_snmp {
my $args = shift;



## initiate session with v1/2c values
$new_session = Net::SNMP->session(Hostname => $args{Hostname}, Community => $args{Community}, 
Port => $args{Port});
$new_session->timeout(2);
$new_session->retries(1);
return $new_session;
}

sub discover_vendor {
  my $args = shift;

#*********************************************************************************	






  ## sysdescr OID
  my %oid = ( version => ".1.3.6.1.2.1.1.1.0" );
  
  ## set up initial parameters for this nodes snmp session(s)
  $s = create_snmp($args);

  ## test for successful presence of a net::snmp session
  if (!$s) {
    $args->{err} = "SNMP session failed to be created for $args{host}!";
    print "SNMP session failed to be created for $args{host}!";    
    return 1;
  }
    ## grab the ios major revision number
  my $vendor_description = $s->get_request($oid{version});

  ## grab an error if it exists
  $args->{err} = $s->error;

  ## close the snmp session
  $s->close;
 
  ## if the ios is undeterminable log it to screen and skip
  if ($args->{err}) {
    ## if the remote device is not able to be queried
         print " remote device is not able to be queried $args{err}!";
    ## after logging the error, skip to next host
    return 1;
  }
       ## place sysDescr return into $args value
       $args->{desc} = $vendor_description->{$oid{version}};
        
 
     ## place our vendor into our dialogue hash
    $args->{vndr} = device_description($args->{desc});

     process_device($args);
     ## run post commands

 
}



#------------------------------------------------------------------------------- 
# device_description
# IN : scalar containing sysdescr
# OUT: returns scalar containing description or 'Unknown' if it doesn't exist
#------------------------------------------------------------------------------- 
sub device_description {
   my $name = shift || return 0;
   my $sn = '';
   
   if ($sn = (grep { $name =~ m/$_/gi } keys %types)[0]) {

     return $types{$sn};
    
   } else {
      return 'Unknown';

   }
}

#------------------------------------------------------------------------------- 
# process_device - figures out what device type we have and tries to
# operate on it based on args given
# IN : args - hash ref containing various program args
#      opts - hash ref of options passed into program
#------------------------------------------------------------------------------- 
sub process_device {
$args = shift;
 
  if ($opts =="download") {
     
    ## test to see which os is on remote node
    if ($args->{desc} =~ /Version 1(?:1|0)/) {

      ## place our vendor into our dialogue hash
      $args->{vndr} = 'Cisco (IOS 10.x/11.x)';

      ## run for 10.x and 11.x
      &cisco_transfer_deprecated($args);

      ## detect Catalyst 2950/3550 with 12.1(12) or higher
    } elsif ($args->{desc} =~ /C(?:35|29)50.*Version 12.[1-9]\(1[2-9]/) {

      ## place our vendor into our dialogue hash
      $args->{vndr} = 'Cisco IOS 12.1(12+) C3550|C2950';

      &cisco_transfer_cccopy($args);

      ## detect Catalyst 2900XL/3500XL
    } elsif ($args->{desc} =~ / C(?:(?:800)|35(?:(?:50)|(?:00XL))) /) {
                                                                                
      ## place our vendor into our dialogue hash
      $args->{vndr} = 'Cisco C3500XL/C2950/C3550/C800';
         
      &cisco_transfer_deprecated($args);

    } elsif ($args->{desc} =~ /Version 12/) {

      ## place our vendor into our dialogue hash
      $args->{vndr} = 'Cisco (IOS 12.x)';

      ## run for 12.x
      &cisco_transfer_cccopy($args);

    } elsif ($args->{desc} =~ /(?:Catalyst)|(?:WS-)/i) {

      ## place our vendor into our dialogue hash
      $args->{vndr} = 'Cisco CatalystOS';

      ## run for Catalysts
      &cisco_transfer_catalyst($args);

    } else {
      ## create error showing that cisco device is not supported
   
       log_print ("The hardware for target is not currently supported. \n" ,"c");
      ## log the error
    

    }
       log_print ("The target vendor $args->{vndr}. \n" ,"c");
  }



  &cisco_commit($args) if ($opts->{commit});

  &cisco_reload($args) if ($opts->{reload});

}

sub cisco_transfer_catalyst { 
  my $args = shift;

print "Start  $args->{vndr}. transfer \n";
  ## set up oid to be used in this routine
  my %oid = (

                ## catalyst switch mibs
                cat_ipaddress   => '.1.3.6.1.4.1.9.5.1.5.1.0',
                cat_filename    => '.1.3.6.1.4.1.9.5.1.5.2.0',
                cat_module      => '.1.3.6.1.4.1.9.5.1.5.3.0',
                cat_action      => '.1.3.6.1.4.1.9.5.1.5.4.0',
                cat_result      => '.1.3.6.1.4.1.9.5.1.5.5.0',
                cat_mod2stdbystatus => '.1.3.6.1.4.1.9.5.1.3.1.1.21.2',

            );
  # interface cards will report other (1) as standby status
  # other possible values are active (2), standby (3), error (4) 

  my %tftpResult =        ( 1     => 'inProgress',
                            2     => 'success',
                            3     => 'No Response',
                            4     => 'Too Many Retries',
                            5     => 'No Buffers',
                            6     => 'No Processes',
                            7     => 'Bad Checksum',
                            8     => 'Bad Length',
                            9     => 'Bad Flash',
                            10    => 'Server Error',
                            11    => 'User Canceled',
                            12    => 'Wrong Code',
                            13    => 'File Not Found',
                            14    => 'Invalid Tftp Host',
                            15    => 'Invalid Tftp Module',
                            16    => 'Access Violation',
                            17    => 'Unknown Status : Check TFTP Server',
                            18    => 'Invalid Storage Device',
                            19    => 'Insufficient Space On Storage Device',
                            20    => 'Insufficient Dram Size',
                            21    => 'Incompatible Image',
                          );

  if (($args->{src} eq 'start') or ($args->{dst} eq 'start')) {
    print "\nCopying configurations to and from startup-config\nis not possible using the CatOS.\n\n";

  } else {

    my $tftpmod = 1;

    ## determine the mib value for where the file will be sent
    my $i;
    if ($args->{src} eq 'tftp') { $i = '2'; } else { $i = '3'; }

    ## create the session
    my $s = create_snmp($args);

    ## check to see if sup in slot 2 is active
    my $mod_standby_state = $s->get_request($oid{cat_mod2stdbystatus});
    if (!$s->error) {
       if ($mod_standby_state->{$oid{cat_mod2stdbystatus}} eq '2') {
          $tftpmod = 2;
       }
    }

    ## set up the request
    $s->set_request	( ## set the tftp server value
			  $oid{cat_ipaddress}, OCTET_STRING, $args{tftp},

		     	  ## set up the config file name
			  $oid{cat_filename}, OCTET_STRING, "$args{file}",
 
		     	  ## prep the module to go
			  $oid{cat_module}, INTEGER, $tftpmod,

		     	  ## send config
			  $oid{cat_action}, INTEGER, $i,		
		   	);

    ## put error into hash
    $args->{err} = $s->error;
    print "$args->{err} \n";
    if (!$args->{err}) {

      ## set default status as "running"
      my $result = '1';

      ## check for the results status
      while (defined($result) && $result == '1') {

        ## get the current status of the tftp server's action
        my $current_state = $s->get_request ($oid{cat_result});

        $result = $current_state->{$oid{cat_result}};

      ## end while
      }


      ## failure!
      if (!defined($result)) {
        $args->{err} = 'SNMP Session failed during transfer';
        log_print ("Error:  $args->{err} \n", "c");
      } elsif ($result != '2') {

        ## add error message into $args hash
        $args->{err} = $tftpResult{$result};
           log_print ("Error:  $args->{err} \n", "c");
      ## endif
      }

    ## endif
    }

    ## close snmp session
    $s->close;

    ## log output to screen and possibly external file
     log_print ("$args->{log} \n", "c" );
      
  }

}



sub cisco_transfer_deprecated {
  my $args = shift;
 

  ## build oid list for subroutine
  my %oid = (	## deprecated lsystem mibs
                wrnet           => '.1.3.6.1.4.1.9.2.1.55.',
                confnet         => '.1.3.6.1.4.1.9.2.1.53.',
            );


  if (($args->{src} eq 'start') or ($args->{dst} eq 'start')) {
    print "\nCopying configurations to and from startup-config\nis not possible using deprecated mibs.\n\n";    
 
  } else {
    my $mib;

    ## set up proper value for $mib
    if ($args->{src} eq 'tftp') {
      $mib = $oid{confnet};
    } else {
      $mib = $oid{wrnet};
    }

    $mib = "$mib$args->{tftp}"; 

    my $s = create_snmp($args);

    ## set up the request
    $s->set_request($mib, OCTET_STRING, "$args->{file}");

    ## put error into hash
    $args->{err} = $s->error;

    ## close snmp session
    $s->close;

    ## log output to screen and possibly external file
    $args->{log}->log_action($args);

  }
}

sub cisco_transfer_cccopy {
  my $args = shift;

 
  ##
  ## NOTES TO SELF ON INCLUDING SCP TRANSPORT PROTOCOL
  ## 	ccCopyUserName AND ccCopyUserPassword
  ##


  ## set up oid to be used in this routine
  my %oid = (
                ## 
                method          => '.1.3.6.1.4.1.9.9.96.1.1.1.1.2',

		##
                source          => '.1.3.6.1.4.1.9.9.96.1.1.1.1.3',

		##
                destination     => '.1.3.6.1.4.1.9.9.96.1.1.1.1.4',

		##
                ipaddress       => '.1.3.6.1.4.1.9.9.96.1.1.1.1.5',

		##
                filename        => '.1.3.6.1.4.1.9.9.96.1.1.1.1.6',

		##
                rowstatus       => '.1.3.6.1.4.1.9.9.96.1.1.1.1.14',

		##
                state           => '.1.3.6.1.4.1.9.9.96.1.1.1.1.10',

		##
                cause           => '.1.3.6.1.4.1.9.9.96.1.1.1.1.13',
             );

  my %filelocation =      ( tftp          => '1',
                            start         => '3',
                            run           => '4',
                          );

  my %state =             ( waiting       => '1',
                            running       => '2',
                            success       => '3',
                            failed        => '4',
                          );

  my %cause =             ( 1     => 'Unknown Copy Failure',
                            2     => 'Bad File Name',
                            3     => 'Network timeout',
                            4     => 'Not Enough Memory',
                            5     => 'Source Configuration doesnt exist.',
                          );


  ## generate random number used for mib instances
  srand(time | $$);
  my $rand = int(rand(900))+10;

  ## start snmp session
  my $s = create_snmp($args);

  ## copy files across network
  $s->set_request   (  ## select method of transfer
                       "$oid{method}.$rand", INTEGER, 1,

                       ## select source file location
                       "$oid{source}.$rand", INTEGER, $filelocation{$args{src}},

                       ## select destination file location
                       "$oid{destination}.$rand", INTEGER, $filelocation{$args{dst}},

                       ## set tftpserver ip address
                       "$oid{ipaddress}.$rand", IPADDRESS, $args{tftp},

                       ## set the filename being written
                       "$oid{filename}.$rand", OCTET_STRING, "/$args->{file}",

                       ## set the session status
                       "$oid{rowstatus}.$rand", INTEGER, 4,
                    );

  ## add error message into $args hash
  $args->{err} = $s->error;
   print "$args->{err}\n";
  ## if no error...
  if (!$args->{err}) {

    ## set default status as "running"
    my $result = '1';

    ## check for the results status
    while( 
           ( defined($result) ) 
             && 
           (
             ($result == "$state{running}") 
               || 
             ($result == "$state{waiting}")
           ) 
         ) {

      ## get the current status of the tftp server's action
      my $current_state = $s->get_request ("$oid{state}.$rand");

      $result = $current_state->{"$oid{state}.$rand"};

    ## end while
    }

    ## failure!
    if (!defined($result)) {
      $args->{err} = 'SNMP Session failed during transfer';

    } elsif ($result != '3') {
 
      ## send snmp reqest to find cause of problem
      my $cause_req = $s->get_request ("$oid{cause}.$rand");

      ## assign result to the value returned from the query
      $result = $cause_req->{"$oid{cause}.$rand"};

      ## add error message into $args hash
      $args->{err} = $cause{$result};

    ## endif
    }

    ## clear the rowstatus for the remote device
    $s->set_request ("$oid{rowstatus}.$rand", INTEGER, 6);
    
  ## endif
  }

  ## close the snmp session
  $s->close;

  ## log output to screen and possibly external file
   log_print ("$args->{log} \n", "c"); 

}




# this must be here or else it won't return true
1;