Advertisement
fuhbahr

cnrancid

Aug 24th, 2015
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 18.87 KB | None | 0 0
  1. #! /usr/bin/perl
  2. ##
  3. ## $Id: mrvrancid.in 2246 2010-09-08 01:36:07Z heas $
  4. ##
  5. ## rancid 2.3.6
  6. ## Copyright (c) 1997-2008 by Terrapin Communications, Inc.
  7. ## All rights reserved.
  8. ##
  9. ## This code is derived from software contributed to and maintained by
  10. ## Terrapin Communications, Inc. by Henry Kilmer, John Heasley, Andrew Partan,
  11. ## Pete Whiting, Austin Schutz, and Andrew Fort.
  12. ##
  13. ## Redistribution and use in source and binary forms, with or without
  14. ## modification, are permitted provided that the following conditions
  15. ## are met:
  16. ## 1. Redistributions of source code must retain the above copyright
  17. ##    notice, this list of conditions and the following disclaimer.
  18. ## 2. Redistributions in binary form must reproduce the above copyright
  19. ##    notice, this list of conditions and the following disclaimer in the
  20. ##    documentation and/or other materials provided with the distribution.
  21. ## 3. All advertising materials mentioning features or use of this software
  22. ##    must display the following acknowledgement:
  23. ##        This product includes software developed by Terrapin Communications,
  24. ##        Inc. and its contributors for RANCID.
  25. ## 4. Neither the name of Terrapin Communications, Inc. nor the names of its
  26. ##    contributors may be used to endorse or promote products derived from
  27. ##    this software without specific prior written permission.
  28. ## 5. It is requested that non-binding fixes and modifications be contributed
  29. ##    back to Terrapin Communications, Inc.
  30. ##
  31. ## THIS SOFTWARE IS PROVIDED BY Terrapin Communications, INC. AND CONTRIBUTORS
  32. ## ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  33. ## TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  34. ## PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COMPANY OR CONTRIBUTORS
  35. ## BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  36. ## CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  37. ## SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  38. ## INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  39. ## CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. ## ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  41. ## POSSIBILITY OF SUCH DAMAGE.
  42. #
  43. # Amazingly hacked version of Hank's rancid - this one tries to
  44. # deal with MegaVision MRV.
  45. #
  46. #  RANCID - Really Awesome New Cisco confIg Differ
  47. #
  48. # usage: rancid [-dV] [-l] [-f filename | hostname]
  49. #
  50. use Getopt::Std;
  51. getopts('dflV');
  52. if ($opt_V) {
  53.     print "rancid 2.3.6\n";
  54.     exit(0);
  55. }
  56. $log = $opt_l;
  57. $debug = $opt_d;
  58. $file = $opt_f;
  59. $host = $ARGV[0];
  60. $ios = "IOS";
  61. $clean_run = 0;
  62. $found_end = 0;
  63. $found_version = 0;
  64. $found_env = 0;
  65. $found_diag = 0;
  66. $timeo = 90;                # clogin timeout in seconds
  67.  
  68. # force a terminal type so as not to confuse the POS
  69. $ENV{'TERM'} = "vt100";
  70.  
  71. my(@commandtable, %commands, @commands);# command lists
  72. my($aclsort) = ("ipsort");      # ACL sorting mode
  73. my($filter_commstr);            # SNMP community string filtering
  74. my($filter_pwds);           # password filtering mode
  75.  
  76. # This routine is used to print out the router configuration
  77. sub ProcessHistory {
  78.     my($new_hist_tag,$new_command,$command_string,@string) = (@_);
  79.     if ((($new_hist_tag ne $hist_tag) || ($new_command ne $command))
  80.     && scalar(%history)) {
  81.     print eval "$command \%history";
  82.     undef %history;
  83.     }
  84.     if (($new_hist_tag) && ($new_command) && ($command_string)) {
  85.     if ($history{$command_string}) {
  86.         $history{$command_string} = "$history{$command_string}@string";
  87.     } else {
  88.         $history{$command_string} = "@string";
  89.     }
  90.     } elsif (($new_hist_tag) && ($new_command)) {
  91.     $history{++$#history} = "@string";
  92.     } else {
  93.     print "@string";
  94.     }
  95.     $hist_tag = $new_hist_tag;
  96.     $command = $new_command;
  97.     1;
  98. }
  99.  
  100. sub numerically { $a <=> $b; }
  101.  
  102. # This is a sort routine that will sort numerically on the
  103. # keys of a hash as if it were a normal array.
  104. sub keynsort {
  105.     local(%lines) = @_;
  106.     local($i) = 0;
  107.     local(@sorted_lines);
  108.     foreach $key (sort numerically keys(%lines)) {
  109.     $sorted_lines[$i] = $lines{$key};
  110.     $i++;
  111.     }
  112.     @sorted_lines;
  113. }
  114.  
  115. # This is a sort routine that will sort on the
  116. # keys of a hash as if it were a normal array.
  117. sub keysort {
  118.     local(%lines) = @_;
  119.     local($i) = 0;
  120.     local(@sorted_lines);
  121.     foreach $key (sort keys(%lines)) {
  122.     $sorted_lines[$i] = $lines{$key};
  123.     $i++;
  124.     }
  125.     @sorted_lines;
  126. }
  127.  
  128. # This is a sort routine that will sort on the
  129. # values of a hash as if it were a normal array.
  130. sub valsort{
  131.     local(%lines) = @_;
  132.     local($i) = 0;
  133.     local(@sorted_lines);
  134.     foreach $key (sort values %lines) {
  135.     $sorted_lines[$i] = $key;
  136.     $i++;
  137.     }
  138.     @sorted_lines;
  139. }
  140.  
  141. # This is a numerical sort routine (ascending).
  142. sub numsort {
  143.     local(%lines) = @_;
  144.     local($i) = 0;
  145.     local(@sorted_lines);
  146.     foreach $num (sort {$a <=> $b} keys %lines) {
  147.     $sorted_lines[$i] = $lines{$num};
  148.     $i++;
  149.     }
  150.     @sorted_lines;
  151. }
  152.  
  153. # This is a sort routine that will sort on the
  154. # ip address when the ip address is anywhere in
  155. # the strings.
  156. sub ipsort {
  157.     local(%lines) = @_;
  158.     local($i) = 0;
  159.     local(@sorted_lines);
  160.     foreach $addr (sort sortbyipaddr keys %lines) {
  161.     $sorted_lines[$i] = $lines{$addr};
  162.     $i++;
  163.     }
  164.     @sorted_lines;
  165. }
  166.  
  167. # These two routines will sort based upon IP addresses
  168. sub ipaddrval {
  169.     my(@a) = ($_[0] =~ m#^(\d+)\.(\d+)\.(\d+)\.(\d+)$#);
  170.     $a[3] + 256 * ($a[2] + 256 * ($a[1] +256 * $a[0]));
  171. }
  172. sub sortbyipaddr {
  173.     &ipaddrval($a) <=> &ipaddrval($b);
  174. }
  175.  
  176. # This routine parses "show version"
  177. #sub ShowVersion {
  178. #    print STDERR "    In ShowVersion: $_" if ($debug);
  179. #
  180. #    while (<INPUT>) {
  181. #   tr/\015//d;
  182. #   if (/^$prompt/) { $found_version = 1; last};
  183. #   next if (/^(\s*|\s*$cmd\s*)$/);
  184. #   return(1) if /(Invalid input detected|Type help or )/;
  185. #   return(0) if ($found_version);      # Only do this routine once
  186. #   return(-1) if (/command authorization failed/i);
  187. #
  188. #   /copyright/i && next;
  189. #   /^up/ && next;
  190. #   # skip voltage
  191. #   /\s+\d+V\s*\:\s*\d+\.?\d*/ && next;
  192. #   /Internal\ Temperature/i && next;
  193. #   /u-boot/i &&
  194. #       ProcessHistory("COMMENTS","keysort","C1", "!ROM: $_") && next;
  195. #
  196. #   ProcessHistory("COMMENTS","keysort","B1", "!Image: $_") && next;
  197. #    }
  198. #    ProcessHistory("COMMENTS","","","!\n");
  199. #    return(0);
  200. #}
  201.  
  202. # This routine parses "show chassis" for the gsr
  203. # This will create arrays for hw info.
  204. sub ShowChassis {
  205.     # Skip if this is not a 1200n.
  206.     print STDERR "    In ShowChassis: $_" if ($debug);
  207.  
  208.    while (<INPUT>) {
  209.         tr/\015//d;
  210.         if (/^$prompt/) { $found_version = 1; last};
  211.         next if (/^(\s*|\s*$cmd\s*)$/);
  212.         return(1) if /(Invalid input detected|Type help or )/;
  213.         return(0) if ($found_version);          # Only do this routine once
  214.         return(-1) if (/command authorization failed/i);
  215.  
  216.         #/u-boot/i &&
  217.         #    ProcessHistory("COMMENTS","keysort","C1", "!ROM: $_") && next;
  218.  
  219.         ProcessHistory("COMMENTS","keysort","C1", "! $_") && next;
  220.     }
  221.  ProcessHistory("COMMENTS","","","!\n");
  222.     return(0);
  223. }
  224.  
  225. sub ShowSoftware {
  226.     # Skip if this is not a 1200n.
  227.     print STDERR "    In ShowSoftware: $_" if ($debug);
  228.  
  229.     while (<INPUT>) {
  230.         tr/\015//d;
  231.         last if (/^$prompt/);
  232.         next if (/^(\s*|\s*$cmd\s*)$/);
  233.         return(1) if /(Invalid input detected|Type help or )/;
  234.         return(-1) if (/command authorization failed/i);
  235.  
  236.         if (/Chassis +Model/ || /=======/) {
  237.             ProcessHistory("COMMENTS","keysort","C1","!$_");
  238.             next;
  239.         }
  240.         # Trim the temp from the end of the slot line
  241.     #    chomp; s/\s+$//;
  242.      #   s/\S+$//;
  243.         ProcessHistory("COMMENTS","keysort","C1","! $_");
  244.     }
  245.     # ! Zeichen vor jede Zeile setzen!
  246.     ProcessHistory("COMMENTS","","","!\n");
  247.     return(0);
  248. }
  249. sub ShowLicense {
  250.     # Skip if this is not a 1200n.
  251.     print STDERR "    In ShowSoftware: $_" if ($debug);
  252.  
  253.     while (<INPUT>) {
  254.         tr/\015//d;
  255.         last if (/^$prompt/);
  256.         next if (/^(\s*|\s*$cmd\s*)$/);
  257.         return(1) if /(Invalid input detected|Type help or )/;
  258.         return(-1) if (/command authorization failed/i);
  259.  
  260.         if (/Chassis +Model/ || /=======/) {
  261.             ProcessHistory("COMMENTS","keysort","C1","!$_");
  262.             next;
  263.         }
  264.         # Trim the temp from the end of the slot line
  265.     #    chomp; s/\s+$//;
  266.      #   s/\S+$//;
  267.         ProcessHistory("COMMENTS","keysort","C1","! $_");
  268.     }
  269.         # ! Zeichen vor jede Zeile setzen!
  270.     ProcessHistory("COMMENTS","","","!\n");
  271.     return(0);
  272. }
  273.  
  274. sub ShowXcvr {
  275.     # Skip if this is not a 1200n.
  276.     print STDERR "    In ShowXcvr: $_" if ($debug);
  277.  
  278.     while (<INPUT>) {
  279.         tr/\015//d;
  280.         last if (/^$prompt/);
  281.         next if (/^(\s*|\s*$cmd\s*)$/);
  282.         return(1) if /(Invalid input detected|Type help or )/;
  283.         return(-1) if (/command authorization failed/i);
  284.  
  285.         if (/Chassis +Model/ || /=======/) {
  286.             ProcessHistory("COMMENTS","keysort","C1","!$_");
  287.             next;
  288.         }
  289.         # Trim the temp from the end of the slot line
  290.     #    chomp; s/\s+$//;
  291.     #    s/\S+$//;
  292.         ProcessHistory("COMMENTS","keysort","C1","! $_");
  293.     }
  294.     ProcessHistory("COMMENTS","","","!\n");
  295.     return(0);
  296. }
  297.  
  298. # This routine parses "show slots".
  299. sub ShowSlots {
  300.     print STDERR "    In ShowSlots: $_" if ($debug);
  301.  
  302.     while (<INPUT>) {
  303.     tr/\015//d;
  304.     return if (/^\s*\^$/);
  305.     last if (/online diag status/i);
  306.     last if (/^$prompt/);
  307.     next if (/^(\s*|\s*$cmd\s*)$/);
  308.     return(-1) if (/command authorization failed/i);
  309.  
  310.     ProcessHistory("SLOTS","","","!$_");
  311.     next;
  312.     }
  313.     ProcessHistory("SLOTS","","","!\n");
  314.  
  315.     return(0);
  316. }
  317.  
  318. # This routine processes a "write term"
  319. sub WriteTerm {
  320.     print STDERR "    In WriteTerm: $_"; # if ($debug);
  321.     my($lineauto,$comment,$linecnt) = (0,0,0);
  322.  
  323.     while (<INPUT>) {
  324.     tr/\015//d;
  325.     last if (/^$prompt/);
  326.         return(1) if /Line has invalid autocommand /;
  327.     return(1) if (/(Invalid input detected|Type help or )/i);
  328.     return(0) if ($found_end);      # Only do this routine once
  329.     return(-1) if (/command authorization failed/i);
  330.  
  331.     /Non-Volatile memory is in use/  && return(-1); # NvRAM is locked
  332.     $linecnt++;
  333.     $lineauto = 0 if (/^[^ ]/);
  334.     # skip the crap
  335.     #if (/^(##+$|(Building|Current) configuration)/i) {
  336.     #    while (<INPUT>) {
  337. #       next if (/^Current configuration\s*:/i);
  338. #       next if (/^:/);
  339. #       next if (/^([%!].*|\s*)$/);
  340. #       last;
  341. #       }
  342. #       tr/\015//d;
  343. #   }
  344.  
  345.     # skip consecutive comment lines to avoid oscillating extra comment
  346.     # line on some access servers.  grrr.
  347.     if (/^!/) {
  348.         next if ($comment);
  349.         ProcessHistory("","","",$_);
  350.         $comment++;
  351.         next;
  352.     }
  353.     $comment = 0;
  354.  
  355.     # Dog gone Cool matches to process the rest of the config
  356.     /^tftp-server flash /   && next; # kill any tftp remains
  357.     /^ntp clock-period /    && next; # kill ntp clock-period
  358.     /^ length /     && next; # kill length on serial lines
  359.     /^ width /      && next; # kill width on serial lines
  360.     $lineauto = 1 if /^ modem auto/;
  361.     /^ speed / && $lineauto && next; # kill speed on serial lines
  362.     if (/^(enable )?(password|passwd)( level \d+)? / && $filter_pwds >= 1) {
  363.         ProcessHistory("ENABLE","","","!$1$2$3 <removed>\n");
  364.         next;
  365.     }
  366.     if (/^(enable secret) / && $filter_pwds >= 2) {
  367.         ProcessHistory("ENABLE","","","!$1 <removed>\n");
  368.         next;
  369.     }
  370.     if (/^username (\S+)(\s.*)? password (encrypted \S+|\S+)(\sclass .*$)/){
  371.         if ($filter_pwds >= 2) {
  372.         ProcessHistory("USER","keysort","$1",
  373.                    "!username $1$2 password <removed>$4\n");
  374.         } else {
  375.         ProcessHistory("USER","keysort","$1","$_");
  376.         }
  377.         next;
  378.     }
  379.     if (/^(\s*)password / && $filter_pwds >= 1) {
  380.         ProcessHistory("LINE-PASS","","","!$1password <removed>\n");
  381.         next;
  382.     }
  383.  
  384.     # filter out any RCS/CVS tags to avoid confusing local CVS storage
  385.     s/\$(Revision|Id):/ $1:/;
  386.     # order access-lists
  387.     /^access-list\s+(\d\d?)\s+(\S+)\s+(\S+)/ &&
  388.         ProcessHistory("ACL $1 $2","$aclsort","$3","$_") && next;
  389.     # order extended access-lists
  390.     /^access-list\s+(\d\d\d)\s+(\S+)\s+ip\s+host\s+(\S+)/ &&
  391.         ProcessHistory("EACL $1 $2","$aclsort","$3","$_") && next;
  392.     /^access-list\s+(\d\d\d)\s+(\S+)\s+ip\s+(\d\S+)/ &&
  393.         ProcessHistory("EACL $1 $2","$aclsort","$3","$_") && next;
  394.     /^access-list\s+(\d\d\d)\s+(\S+)\s+ip\s+any/ &&
  395.         ProcessHistory("EACL $1 $2","$aclsort","0.0.0.0","$_") && next;
  396.     # order arp lists
  397.     /^arp\s+(\d+\.\d+\.\d+\.\d+)\s+/ &&
  398.         ProcessHistory("ARP","$aclsort","$1","$_") && next;
  399.     /^ip prefix-list\s+(\S+)\s+seq\s+(\d+)\s+(permit|deny)\s+(\d\S+)(\/.*)$/ &&
  400.         ProcessHistory("PACL $1 $3","$aclsort","$4","ip prefix-list $1 $3 $4$5\n")
  401.         && next;
  402.     # order logging statements
  403.     /^logging (\d+\.\d+\.\d+\.\d+)/ &&
  404.         ProcessHistory("LOGGING","ipsort","$1","$_") && next;
  405.     # order/prune snmp-server host statements
  406.     # we only prune lines of the form
  407.     # snmp-server host a.b.c.d <community>
  408.     if (/^snmp-server host (\d+\.\d+\.\d+\.\d+) /) {
  409.         if ($filter_commstr) {
  410.         my($ip) = $1;
  411.         my($line) = "snmp-server host $ip";
  412.         my(@tokens) = split(' ', $');
  413.         my($token);
  414.         while ($token = shift(@tokens)) {
  415.             if ($token eq 'version') {
  416.             $line .= " " . join(' ', ($token, shift(@tokens)));
  417.             if ($token eq '3') {
  418.                 $line .= " " . join(' ', ($token, shift(@tokens)));
  419.             }
  420.             } elsif ($token eq 'vrf') {
  421.             $line .= " " . join(' ', ($token, shift(@tokens)));
  422.             } elsif ($token =~ /^(informs?|traps?|(no)?auth)$/) {
  423.             $line .= " " . $token;
  424.             } else {
  425.             $line = "!$line " . join(' ', ("<removed>", join(' ',@tokens)));
  426.             last;
  427.             }
  428.         }
  429.         ProcessHistory("SNMPSERVERHOST","ipsort","$ip","$line\n");
  430.         } else {
  431.         ProcessHistory("SNMPSERVERHOST","ipsort","$1","$_");
  432.         }
  433.         next;
  434.     }
  435.     if (/^(snmp-server community) (\S+)/) {
  436.         if ($filter_commstr) {
  437.         ProcessHistory("SNMPSERVERCOMM","keysort","$_",
  438.                    "!$1 <removed>$'") && next;
  439.         } else {
  440.         ProcessHistory("SNMPSERVERCOMM","keysort","$_","$_") && next;
  441.         }
  442.     }
  443.     # prune tacacs/radius server keys
  444.     # radius-server host <1-5> IP SECRET [<ip port> [TIMEOUT [RETRY]]]
  445.     if (/^((tacacs|radius)-server)\s(\d+\.\d+\.\d+\.\d+)\s(\w*)/
  446.         && $filter_pwds >= 1) {
  447.         ProcessHistory("","","","!$1 $2 <removed>$'"); next;
  448.     }
  449.     # order ntp peers/servers
  450.     if (/^ntp (server|peer) (\d+)\.(\d+)\.(\d+)\.(\d+)/) {
  451.         $sortkey = sprintf("$1 %03d%03d%03d%03d",$2,$3,$4,$5);
  452.         ProcessHistory("NTP","keysort",$sortkey,"$_");
  453.         next;
  454.     }
  455.  
  456.     # catch anything that wasnt matched above.
  457.     ProcessHistory("","","","$_");
  458.     }
  459.  
  460.     # The MRV MCC lacks a definitive "end of config" marker.  If we have seen
  461.     # at least 5 lines of show config output, we can be reasonably sure that
  462.     # we got the config.
  463.     warn "$linecnt<<<<---- Line Count!!\n";
  464.  
  465.    if ($linecnt > 2) {
  466.     $found_end = 1;
  467. #   return(0);
  468.     }
  469. #    $found_end = 1;
  470.     return(0);
  471. }
  472.  
  473. # dummy function
  474. sub DoNothing {print STDOUT;}
  475.  
  476. # Main
  477. @commandtable = (
  478.     {'chassis show attributes'          => 'ShowChassis'},
  479.     {'software license show'            => 'ShowLicense'},
  480.     {'software show'        => 'ShowSoftware'},
  481.     {'port xcvr show'       => 'ShowXcvr'},
  482.     {'configuration show'       => 'WriteTerm'},
  483.     {'software show'        => 'ShowSoftware'},
  484. );
  485. # Use an array to preserve the order of the commands and a hash for mapping
  486. # commands to the subroutine and track commands that have been completed.
  487. @commands = map(keys(%$_), @commandtable);
  488. %commands = map(%$_, @commandtable);
  489.  
  490. $cisco_cmds = join(";",@commands);
  491. $cmds_regexp = join("|", map quotemeta($_), @commands);
  492.  
  493. if (length($host) == 0) {
  494.     if ($file) {
  495.     print(STDERR "Too few arguments: file name required\n");
  496.     exit(1);
  497.     } else {
  498.     print(STDERR "Too few arguments: host name required\n");
  499.     exit(1);
  500.     }
  501. }
  502. open(OUTPUT,">$host.new") || die "Can't open $host.new for writing: $!\n";
  503. select(OUTPUT);
  504. # make OUTPUT unbuffered if debugging
  505. if ($debug) { $| = 1; }
  506.  
  507. if ($file) {
  508.     print STDERR "opening file $host\n" if ($debug);
  509.     print STDOUT "opening file $host\n" if ($log);
  510.     open(INPUT,"<$host") || die "open failed for $host: $!\n";
  511. } else {
  512.     print STDERR "executing cnlogin -t $timeo -c\"$cisco_cmds\" $host\n" if ($debug);
  513.     print STDOUT "executing cnlogin -t $timeo -c\"$cisco_cmds\" $host\n" if ($log);
  514.     if (defined($ENV{NOPIPE})) {
  515.     system "/var/lib/rancid/bin/cnlogin -t $timeo -c \"$cisco_cmds\" $host </dev/null > $host.raw 2>&1" || die "cnlogin failed for $host: $!\n";
  516.     open(INPUT, "< $host.raw") || die "cnlogin failed for $host: $!\n";
  517.     } else {
  518.     open(INPUT,"/var/lib/rancid/bin/cnlogin -t $timeo -c \"$cisco_cmds\" $host </dev/null |") || die "cnlogin failed for $host: $!\n";
  519.     }
  520. }
  521.  
  522. # determine ACL sorting mode
  523. if ($ENV{"ACLSORT"} =~ /no/i) {
  524.     $aclsort = "";
  525. }
  526. # determine community string filtering mode
  527. if (defined($ENV{"NOCOMMSTR"}) &&
  528.     ($ENV{"NOCOMMSTR"} =~ /yes/i || $ENV{"NOCOMMSTR"} =~ /^$/)) {
  529.     $filter_commstr = 1;
  530. } else {
  531.     $filter_commstr = 0;
  532. }
  533. # determine password filtering mode
  534. if ($ENV{"FILTER_PWDS"} =~ /no/i) {
  535.     $filter_pwds = 0;
  536. } elsif ($ENV{"FILTER_PWDS"} =~ /all/i) {
  537.     $filter_pwds = 2;
  538. } else {
  539.     $filter_pwds = 1;
  540. }
  541.  
  542. ProcessHistory("","","","!RANCID-CONTENT-TYPE: Ciena\n!\n");
  543. ProcessHistory("COMMENTS","keysort","B0","!\n");
  544. ProcessHistory("COMMENTS","keysort","C0","!\n");
  545. TOP: while(<INPUT>) {
  546.     tr/\015//d;
  547.     if (/^Error:/) {
  548.     print STDOUT ("$host clogin error: $_");
  549.     print STDERR ("$host clogin error: $_") if ($debug);
  550.     $clean_run = 0;
  551.     last;
  552.     }
  553.  
  554.     while (/>\s*($cmds_regexp)\s*$/) {
  555.     $cmd = $1;
  556.     if (!defined($prompt)) {
  557.         $prompt = ($_ =~ /^([^>]+>)/)[0];
  558.         $prompt =~ s/([][}{)(\\])/\\$1/g;
  559.         print STDERR ("PROMPT MATCH: $prompt\n") if ($debug);
  560.     }
  561.     print STDERR ("HIT COMMAND:$_") if ($debug);
  562.  
  563.     if (! defined($commands{$cmd})) {
  564.         print STDERR "$host: found unexpected command - \"$cmd\"\n";
  565.         $clean_run = 0;
  566.         last TOP;
  567.     }
  568.  
  569.     $rval = &{$commands{$cmd}};
  570.     delete($commands{$cmd});
  571.     if ($rval == -1) {
  572.         $clean_run = 0;
  573.         last TOP;
  574.     }
  575.     }
  576.  
  577.     if (/[>#]\s*exit$/) {
  578.     $clean_run = 1;
  579.     last;
  580.     }
  581. }
  582. print STDOUT "Done $logincmd: $_\n" if ($log);
  583. # Flush History
  584. ProcessHistory("","","","");
  585. # Cleanup
  586. close(INPUT);
  587. close(OUTPUT);
  588.  
  589. if (defined($ENV{NOPIPE})) {
  590.     unlink("$host.raw") if (! $debug);
  591. }
  592.  
  593. # check for completeness
  594. if (scalar(%commands) || !$clean_run || !$found_end) {
  595.     if (scalar(%commands)) {
  596.     printf(STDOUT "$host: missed cmd(s): %s\n", join(',', keys(%commands)));
  597.     printf(STDERR "$host: missed cmd(s): %s\n", join(',', keys(%commands))) if ($debug);
  598.     }
  599.     if (!$clean_run || !$found_end) {
  600.     print STDOUT "$host: End of run not found\n";
  601.     print STDERR "$host: End of run not found\n" if ($debug);
  602.     warn "$clean_run<<<<<<----- Clean Run!!";
  603.     warn "$found_end<<<<<<<<<------- Found End!!!";
  604.     system("/usr/bin/tail -1 $host.new");
  605.     }
  606.     unlink "$host.new" if (! $debug);
  607. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement