#!@PERLV_PATH@ ## ## ## Copyright (C) 1997-2001 by Henry Kilmer. ## All rights reserved. ## ## This software may be freely copied, modified and redistributed without ## fee for non-commerical purposes provided that this copyright notice is ## preserved intact on all copies and modified copies. ## ## There is no warranty or other guarantee of fitness of this software. ## It is provided solely "as is". The author(s) disclaim(s) all ## responsibility and liability with respect to this software's usage ## or its effect upon hardware, computer systems, other software, or ## anything else. ## ## # # RANCID - Really Awesome New Cisco confIg Differ # # usage: rancid [-d] [-l] [-f filename | $host] # use Getopt::Std; getopts('dflm'); $log = $opt_l; $debug = $opt_d; $file = $opt_f; $host = $ARGV[0]; $clean_run = 0; $found_end = 0; $timeo = 90; # clogin timeout in seconds # This routine is used to print out the router configuration sub ProcessHistory { my($new_hist_tag,$new_command,$command_string,@string)=(@_); if((($new_hist_tag ne $hist_tag) || ($new_command ne $command)) && defined %history) { print eval "$command \%history"; undef %history; } if (($new_hist_tag) && ($new_command) && ($command_string)) { if ($history{$command_string}) { $history{$command_string} = "$history{$command_string}@string"; } else { $history{$command_string} = "@string"; } } elsif (($new_hist_tag) && ($new_command)) { $history{++$#history} = "@string"; } else { print "@string"; } $hist_tag = $new_hist_tag; $command = $new_command; 1; } sub numerically { $a <=> $b; } # This is a sort routing that will sort numerically on the # keys of a hash as if it were a normal array. sub keynsort { local(%lines)=@_; local($i) = 0; local(@sorted_lines); foreach $key (sort numerically keys(%lines)) { $sorted_lines[$i] = $lines{$key}; $i++; } @sorted_lines; } # This is a sort routing that will sort on the # keys of a hash as if it were a normal array. sub keysort { local(%lines)=@_; local($i) = 0; local(@sorted_lines); foreach $key (sort keys(%lines)) { $sorted_lines[$i] = $lines{$key}; $i++; } @sorted_lines; } # This is a sort routing that will sort on the # values of a hash as if it were a normal array. sub valsort{ local(%lines)=@_; local($i) = 0; local(@sorted_lines); foreach $key (sort values %lines) { $sorted_lines[$i] = $key; $i++; } @sorted_lines; } # This is a numerical sort routing (ascending). sub numsort { local(%lines)=@_; local($i) = 0; local(@sorted_lines); foreach $num (sort {$a <=> $b} keys %lines) { $sorted_lines[$i] = $lines{$num}; $i++; } @sorted_lines; } # This is a sort routine that will sort on the # ip address when the ip address is anywhere in # the strings. sub ipsort { local(%lines)=@_; local($i) = 0; local(@sorted_lines); foreach $addr (sort sortbyipaddr keys %lines) { $sorted_lines[$i] = $lines{$addr}; $i++; } @sorted_lines; } # These two routines will sort based upon IP addresses sub ipaddrval { my(@a) = ($_[0] =~ m#^(\d+)\.(\d+)\.(\d+)\.(\d+)$#); $a[3]+256*($a[2]+256*($a[1]+256*$a[0])); } sub sortbyipaddr { &ipaddrval($a) <=> &ipaddrval($b); } # This routine parses "show version" sub ShowVersion { print STDERR " In ShowVersion: $_" if ($debug); while () { tr/\015//d; last if(/^$prompt/); next if(/^(\s*|\s*$cmd\s*)$/); /^\S+ Serial Number:/i && ProcessHistory("COMMENTS","keysort","A0","#$_") && next; /^(\S+) Power Supply ([^:]+):\s+(.*)/i && ProcessHistory("COMMENTS","keysort","B0","#Power: $1 $2 $3\n") && next; /^Image\s*:\s*(.*)/i && ProcessHistory("COMMENTS","keysort","C0","#Image: $1\n") && next; #heas: need to collect this from show vers for ShowSlot where rev #s are excluded #SLOT 1 : 702005-06 0025S-00877 CPLD Rev #SLOT 2 : 702005-06 0021S-00131 CPLD Rev 02 #SLOT 3 : 702009-06 0024S-00170 CPLD Rev #SLOT 4 : 702009-06 0024S-00319 CPLD Rev } return(0); } # This routine parses "show memory" sub ShowMemory { print STDERR " In ShowMemory: $_" if ($debug); while () { tr/\015//d; last if(/^$prompt/); next if(/^(\s*|\s*$cmd\s*)$/); /^Total DRAM Size: (.*)/ && ProcessHistory("COMMENTS","keysort","A0","#\n#Memory: $1\n") } return(0); } # This routine parses "show install active" sub ShowSlot { print STDERR " In ShowSlot: $_" if ($debug); while () { tr/\015//d; last if (/^$prompt/); next if (/^(\s*|\s*$cmd\s*)$/); if (/^Slot\s+(\d+)\s+/i) { my($slot) = $1; my($hwtype, $conftype, $sn, $state); ProcessHistory("SLOT","keysort","$slot","#\n"); while () { tr/\015//d; last if (/^$prompt/ || /^\s*$/); if (/State:\s+(.*)$/i) { $state = $1; } if (/serial number:\s+(.*)$/i) { $sn = $1; } if (/hw module type:\s+(.*)$/i) { $hwtype = $1; } if (/configured type:\s+(.*)$/i) { $conftype = $1; } } ProcessHistory("SLOT","keysort","$slot","#Slot $slot: type $hwtype," . " $conftype\n#Slot $slot: serial $sn\n#Slot $slot: state " . " $state\n"); return if (/^$prompt/); next; } } return(0); } # This routine processes a "write term" sub WriteTerm { print STDERR " In WriteTerm: $_" if ($debug); while () { tr/\015//d; last if(/^$prompt/); next if(/^\s*$/); # the pager can not be disabled per-session on the PIX s/^<-+ More -+>\s*//; # Dog gone Cool matches to process the rest of the config /^(# \S+ Configuration) generated/i && ProcessHistory("","","","$1\n") && next; /^(config bgp (neighbor|peer-group) \S+ password encrypted)/i && ProcessHistory("","","","# $1 \n") && next; # /^( ip ospf authentication-key) / && # ProcessHistory("","","","!$1 \n") && next; # /^( ip ospf message-digest-key \d+ md5) / && # ProcessHistory("","","","!$1 \n") && next; # # sort route-maps # if (/^route-map (\S+)/) { # my($key) = $1; # my($routemap) = $_; # while () { # tr/\015//d; # last if (/^$prompt/ || ! /^(route-map |[ !])/); # if (/^route-map (\S+)/) { # ProcessHistory("ROUTEMAP","keysort","$key","$routemap"); # $key = $1; # $routemap = $_; # } else { # $routemap .= $_; # } # } # ProcessHistory("ROUTEMAP","keysort","$key","$routemap"); # } # filter out any RCS/CVS tags to avoid confusing local CVS storage s/\$(Revision|Id):/ $1:/; # # order access-lists # /^access-list\s+(\d\d?)\s+(\S+)\s+(\S+)/ && # ProcessHistory("ACL $1 $2","ipsort","$3","$_") && next; # # order extended access-lists # /^access-list\s+(\d\d\d)\s+(\S+)\s+ip\s+host\s+(\S+)/ && # ProcessHistory("EACL $1 $2","ipsort","$3","$_") && next; # /^access-list\s+(\d\d\d)\s+(\S+)\s+ip\s+(\d\S+)/ && # ProcessHistory("EACL $1 $2","ipsort","$3","$_") && next; # /^access-list\s+(\d\d\d)\s+(\S+)\s+ip\s+any/ && # ProcessHistory("EACL $1 $2","ipsort","0.0.0.0","$_") && next; # # order arp lists # /^arp\s+(\d+\.\d+\.\d+\.\d+)\s+/ && # ProcessHistory("ARP","ipsort","$1","$_") && next; # /^ip prefix-list\s+(\S+)\s+seq\s+(\d+)\s+(permit|deny)\s+(\d\S+)(\/.*)$/ && # ProcessHistory("PACL $1 $3","ipsort","$4","ip prefix-list $1 $3 $4$5\n") # && next; # order logging statements /^configure syslog add logging (\d+\.\d+\.\d+\.\d+)/ && ProcessHistory("LOGGING","ipsort","$1","$_") && next; # order/prune snmp-server host statements # we only prune lines of the form # configure snmp add trapreceiver a.b.c.d if (/^(configure snmp add trapreceiver )(\d+\.\d+\.\d+\.\d+) (community) \S+ (.*)/) { if (defined($ENV{'NOCOMMSTR'})) { ProcessHistory("SNMPSVRHOST","ipsort","$2","# $1$2 $3 $4\n"); } else { ProcessHistory("SNMPSVRHOST","ipsort","$2","$_\n"); } next; } if (/^(configure snmp add community (readonly|readwrite) \S+) (\S+)/) { if (defined($ENV{'NOCOMMSTR'})) { ProcessHistory("SNMPSVRCOMM","keysort","$_","#$1 $'"); next; } else { ProcessHistory("SNMPSVRCOMM","keysort","$_","$_") && next; } } # order/prune tacacs/radius server statements /^(configure radius (primary|secondary) (tacacs-server|radius-server) shared-secret encrypted)/ && ProcessHistory("","","","# $1 \n") && next; # catch anything that wasnt match above. ProcessHistory("","","","$_"); # end of config if (/^# End of configuration file/i) { printf STDERR " End WriteTerm: $_" if ($debug); $found_end = 1; return(1); } } return(0); } # dummy function sub DoNothing {print STDOUT;} # Main %commands=( 'show version' => "ShowVersion", 'show memory' => "ShowMemory", 'show slot' => "ShowSlot", 'show configuration' => "WriteTerm" ); # keys() doesnt return things in the order entered and the order of the # cmds is important (show version first and write term last). pita @commands=( "show version", "show memory", "show slot", "show configuration" ); $cisco_cmds=join(";",@commands); $cmds_regexp=join("|",@commands); open(OUTPUT,">$host.new") || die "Can't open $host.new for writing: $!\n"; select(OUTPUT); # make OUTPUT unbuffered if debugging if ($debug) { $| = 1; } if ($file) { print STDERR "opening file $host\n" if ($debug); print STDOUT "opening file $host\n" if ($log); open(INPUT,"<$host") || die "open failed for $host: $!\n"; } else { print STDERR "executing clogin -t $timeo -c\"$cisco_cmds\" $host\n" if ($debug); print STDOUT "executing clogin -t $timeo -c\"$cisco_cmds\" $host\n" if ($log); if (defined($ENV{NOPIPE})) { system "clogin -t $timeo -c \"$cisco_cmds\" $host $host.raw 2>&1" || die "clogin failed for $host: $!\n"; open(INPUT, "< $host.raw") || die "clogin failed for $host: $!\n"; } else { open(INPUT,"clogin -t $timeo -c \"$cisco_cmds\" $host ) { tr/\015//d; # note: this match sucks rocks, but the currently the extreme bits are # unreliable about echoing the 'exit\n' command. this match might really # be a bad idea, but instead rely upon WriteTerm's found_end? if (/$prompt\s?(exit$|Connection closed)/ && $found_end) { $clean_run=1; last; } if (/^Error:/) { print STDOUT ("$host clogin error: $_"); print STDERR ("$host clogin error: $_") if ($debug); $clean_run=0; last; } while (/$prompt\s*($cmds_regexp)\s*$/) { $cmd = $1; if (!defined($prompt)) { $prompt = ($_ =~ /^([^#]+#)/)[0]; $prompt =~ s/:(\d+ ?)#/:\\d+ ?#/; print STDERR ("PROMPT MATCH:$prompt\n") if ($debug); } print STDERR ("HIT COMMAND:$_") if ($debug); if (! defined($commands{$cmd})) { print STDERR "found unexpected command - \"$cmd\"\n"; $clean_run = 0; last TOP; } $rval = &{$commands{$cmd}}; delete($commands{$cmd}); if ($rval == -1) { printf STDERR "rval = -1\n" if (debug); $clean_run = 0; last TOP; } } } print STDOUT "Done $logincmd: $_\n" if ($log); # Flush History ProcessHistory("","","",""); # Cleanup close(INPUT); close(OUTPUT); if (defined($ENV{NOPIPE})) { unlink("$host.raw") if (! $debug); } # check for completeness if (scalar(%commands) || !$clean_run || !$found_end) { if (scalar(%commands)) { printf(STDOUT "missed cmd(s): %s\n", join(',', keys(%commands))); printf(STDERR "missed cmd(s): %s\n", join(',', keys(%commands))) if ($debug); } if (!$clean_run || !$found_end) { print STDOUT "End of run not found\n"; print STDERR "End of run not found\n" if ($debug); system("/usr/bin/tail -1 $host.new"); } unlink "$host.new" if (! $debug); }