PageRenderTime 37ms CodeModel.GetById 38ms RepoModel.GetById 0ms app.codeStats 0ms

/bin/f10rancid.in

https://gitlab.com/exectek/rancid-git
Autoconf | 724 lines | 553 code | 50 blank | 121 comment | 226 complexity | c5bbcb0bf9644be9da5b91eccfd821e4 MD5 | raw file
  1. #! @PERLV_PATH@
  2. ##
  3. ## $Id: f10rancid.in 2279 2011-01-31 22:41:00Z heas $
  4. ##
  5. ## @PACKAGE@ @VERSION@
  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. # This version of rancid tries to deal with Force10s.
  44. #
  45. # RANCID - Really Awesome New Cisco confIg Differ
  46. #
  47. # usage: rancid [-dV] [-l] [-f filename | hostname]
  48. #
  49. use Getopt::Std;
  50. getopts('dflV');
  51. if ($opt_V) {
  52. print "@PACKAGE@ @VERSION@\n";
  53. exit(0);
  54. }
  55. $log = $opt_l;
  56. $debug = $opt_d;
  57. $file = $opt_f;
  58. $host = $ARGV[0];
  59. $clean_run = 0;
  60. $found_end = 0;
  61. $timeo = 90; # clogin timeout in seconds
  62. my(@commandtable, %commands, @commands);# command lists
  63. my($aclsort) = ("ipsort"); # ACL sorting mode
  64. my($filter_commstr); # SNMP community string filtering
  65. my($filter_pwds); # password filtering mode
  66. # This routine is used to print out the router configuration
  67. sub ProcessHistory {
  68. my($new_hist_tag,$new_command,$command_string,@string) = (@_);
  69. if ((($new_hist_tag ne $hist_tag) || ($new_command ne $command))
  70. && scalar(%history)) {
  71. print eval "$command \%history";
  72. undef %history;
  73. }
  74. if (($new_hist_tag) && ($new_command) && ($command_string)) {
  75. if ($history{$command_string}) {
  76. $history{$command_string} = "$history{$command_string}@string";
  77. } else {
  78. $history{$command_string} = "@string";
  79. }
  80. } elsif (($new_hist_tag) && ($new_command)) {
  81. $history{++$#history} = "@string";
  82. } else {
  83. print "@string";
  84. }
  85. $hist_tag = $new_hist_tag;
  86. $command = $new_command;
  87. 1;
  88. }
  89. sub numerically { $a <=> $b; }
  90. # This is a sort routine that will sort numerically on the
  91. # keys of a hash as if it were a normal array.
  92. sub keynsort {
  93. local(%lines) = @_;
  94. local($i) = 0;
  95. local(@sorted_lines);
  96. foreach $key (sort numerically keys(%lines)) {
  97. $sorted_lines[$i] = $lines{$key};
  98. $i++;
  99. }
  100. @sorted_lines;
  101. }
  102. # This is a sort routine that will sort on the
  103. # keys of a hash as if it were a normal array.
  104. sub keysort {
  105. local(%lines) = @_;
  106. local($i) = 0;
  107. local(@sorted_lines);
  108. foreach $key (sort keys(%lines)) {
  109. $sorted_lines[$i] = $lines{$key};
  110. $i++;
  111. }
  112. @sorted_lines;
  113. }
  114. # This is a sort routine that will sort on the
  115. # values of a hash as if it were a normal array.
  116. sub valsort{
  117. local(%lines) = @_;
  118. local($i) = 0;
  119. local(@sorted_lines);
  120. foreach $key (sort values %lines) {
  121. $sorted_lines[$i] = $key;
  122. $i++;
  123. }
  124. @sorted_lines;
  125. }
  126. # This is a numerical sort routine (ascending).
  127. sub numsort {
  128. local(%lines) = @_;
  129. local($i) = 0;
  130. local(@sorted_lines);
  131. foreach $num (sort {$a <=> $b} keys %lines) {
  132. $sorted_lines[$i] = $lines{$num};
  133. $i++;
  134. }
  135. @sorted_lines;
  136. }
  137. # This is a sort routine that will sort on the
  138. # ip address when the ip address is anywhere in
  139. # the strings.
  140. sub ipsort {
  141. local(%lines) = @_;
  142. local($i) = 0;
  143. local(@sorted_lines);
  144. foreach $addr (sort sortbyipaddr keys %lines) {
  145. $sorted_lines[$i] = $lines{$addr};
  146. $i++;
  147. }
  148. @sorted_lines;
  149. }
  150. # These two routines will sort based upon IP addresses
  151. sub ipaddrval {
  152. my(@a) = ($_[0] =~ m#^(\d+)\.(\d+)\.(\d+)\.(\d+)$#);
  153. $a[3] + 256 * ($a[2] + 256 * ($a[1] +256 * $a[0]));
  154. }
  155. sub sortbyipaddr {
  156. &ipaddrval($a) <=> &ipaddrval($b);
  157. }
  158. # This routine parses "show version"
  159. sub ShowVersion {
  160. print STDERR " In ShowVersion: $_" if ($debug);
  161. while (<INPUT>) {
  162. tr/\015//d;
  163. last if(/^$prompt/);
  164. next if(/^(\s*|\s*$cmd\s*)$/);
  165. return(-1) if (/command authorization failed/i);
  166. / Type: / && chop && chop &&
  167. ProcessHistory("COMMENTS","keysort","A1", "!$_\n");
  168. /^.* Version.*$/ &&
  169. ProcessHistory("COMMENTS","keysort","F1", "!Image: $_") && next;
  170. /^Build .*$/ &&
  171. ProcessHistory("COMMENTS","keysort","F1", "!Image: $_") && next;
  172. /^System image file is "([^\"]*)"$/ &&
  173. ProcessHistory("COMMENTS","keysort","F5", "!Image: $1\n") && next;
  174. if (/^(.*\s+Processor)( \d)?:(.*) with (\d+[kK]?) bytes/) {
  175. my($cpu) = "$1$2:$3";
  176. my($mem) = int($4 / (1024 * 1024));
  177. my($device) = "Force10";
  178. ProcessHistory("COMMENTS","keysort","B1",
  179. "!Memory: $1$2: $mem" . "M\n");
  180. ProcessHistory("COMMENTS","keysort","A3","!CPU: $cpu\n");
  181. next;
  182. }
  183. # E-Series and C-Series use NVRAM
  184. /^(\d+[kK]) bytes of non-volatile/ &&
  185. ProcessHistory("COMMENTS","keysort","B3", "!Memory: NVRAM $1\n") &&
  186. next;
  187. # S-Series uses boot flash
  188. /^(\d+[mM]) bytes of .oot..lash/ &&
  189. ProcessHistory("COMMENTS","keysort","B3", "!Memory: Flash $1\n") &&
  190. next;
  191. }
  192. return(0);
  193. }
  194. # This routine parses "show boot"
  195. sub ShowBoot {
  196. print STDERR " In ShowBoot: $_" if ($debug);
  197. while (<INPUT>) {
  198. tr/\015//d;
  199. last if (/^$prompt/);
  200. next if (/^(\s*|\s*$cmd\s*)$/);
  201. return(1) if /(Invalid input|Type help or )/;
  202. return(1) if /( *\^$)/;
  203. return(-1) if (/command authorization failed/i);
  204. ProcessHistory("COMMENTS","keysort","H0","!Boot Variables: $_");
  205. }
  206. ProcessHistory("COMMENTS","","","!\n");
  207. return(0);
  208. }
  209. # This routine parses "show chassis"
  210. sub ShowChassis {
  211. print STDERR " In ShowChassis: $_" if ($debug);
  212. while (<INPUT>) {
  213. tr/\015//d;
  214. last if (/^$prompt/);
  215. next if (/^(\s*|\s*$cmd\s*)$/);
  216. return(1) if /(Invalid input|Type help or )/;
  217. return(1) if /( *\^$)/;
  218. return(-1) if (/command authorization failed/i);
  219. /-----------------------------/ && next;
  220. # general stuff that changes
  221. /Next Boot/ && next;
  222. /Up Time/ && next;
  223. /Last Restart/ && next;
  224. /Switch Fabric State/ && next;
  225. /active / && next;
  226. /online / && next;
  227. /offline / && next;
  228. # E600 AC PSMs
  229. /Status : up/ && next;
  230. /High line/ && next;
  231. if (/Power\s+Supplies/i) {
  232. ProcessHistory("COMMENTS","keysort","CHASSIS","!Chassis: $_");
  233. while(<INPUT>) {
  234. tr/\015//d;
  235. last if (/^$/);
  236. # remove any trailing WS
  237. s/\s+$//;
  238. next if (/^----+$/);
  239. # S4048-ON
  240. /^(Unit\s+Bay\s+Status\s+Type\s+FanStatus)\s+FanSpeed/ && ProcessHistory("COMMENTS",
  241. keysort","CHASSIS","!Chassis: $1\n") &&
  242. next;
  243. /^(\s+\d+\s+\d+\s+(absent|\S+\s+\S+)\s+\S+)\s+\d+/ &&
  244. ProcessHistory("COMMENTS","keysort","CHASSIS",
  245. "!Chassis: $1\n") && next;
  246. # S4810
  247. /^(Unit\s+Bay\s+Status\s+Type\s+FanStatus)/ && ProcessHistory("COMMENTS",
  248. "keysort","CHASSIS","!Chassis: $1\n") &&
  249. next;
  250. /^(\s+\d+\s+\d+\s+(absent|\S+\s+\S+)\s+\S+)/ &&
  251. ProcessHistory("COMMENTS","keysort","CHASSIS",
  252. "!Chassis: $1\n") && next;
  253. print STDERR " In ShowChassis: $_" if ($debug);
  254. )
  255. }
  256. # fans
  257. if (/Fan\s+Status/i) {
  258. ProcessHistory("COMMENTS","keysort","CHASSIS","!Chassis: $_");
  259. while (<INPUT>) {
  260. tr/\015//d;
  261. if (/^$prompt/) {
  262. goto OUT;
  263. }
  264. last if (/^$/);
  265. # remove any trailing WS
  266. s/\s+$//;
  267. next if (/^----+$/);
  268. # c150 / c300 style
  269. /FanNumber\s+Speed\s+Status/ && ProcessHistory("COMMENTS",
  270. "keysort","CHASSIS","!Chassis: FanNumber\tStatus\n") &&
  271. next;
  272. /^\s+(\d)\s+\d+\s+(\S+)/ && ProcessHistory("COMMENTS",
  273. "keysort","CHASSIS","!Chassis: $1\t\t$2\n") && next;
  274. # e300
  275. /^Status\s+$/ && next;
  276. /^\s+(\S+)\s*$/ && ProcessHistory("COMMENTS","keysort",
  277. "CHASSIS","!Chassis: $1\n") && next;
  278. # e600 style
  279. /Status\s+Temp\s+Fan1\s+Fan2\s+Fan3\s+Serial\s/ && next;
  280. /^\s+(\S+)\s+\d+C\s+\d+ RPM\s+/ && ProcessHistory("COMMENTS",
  281. "keysort","CHASSIS","!Chassis: $1\n") && next;
  282. # e1200 style
  283. /Tray\s+Status\s+Temp\s+/ && ProcessHistory("COMMENTS",
  284. "keysort","CHASSIS","!Chassis: Tray\tStatus\n") &&
  285. next;
  286. /^\s+(\d+)\s+(\S+)\s+(or down|(< )?\d+C\s+)/ &&
  287. ProcessHistory("COMMENTS","keysort","CHASSIS",
  288. "!Chassis: $1\t$2\n") && next;
  289. # s50n / s50v style
  290. if (/Unit\s+TrayStatus\s+Fan0\s+Fan1\s+/) {
  291. ProcessHistory("COMMENTS","keysort","CHASSIS",
  292. "!Chassis: $_\n");
  293. # consume the following blank/separator line
  294. $_ = <INPUT>;
  295. # the s50n botches its fan status line with the "---..."
  296. # separator line w/o a CR, followed by the fan status. we
  297. # would normally skip the sparator line, so hack it.
  298. s/^------+(\s+)/$1/;
  299. tr/\015//d;
  300. # the s50v has a blank line following the table header,
  301. # loop for that one and continue for the S50n
  302. /^\s+$/ && next;
  303. }
  304. /^\s+\d+\s+\S+\s+\S+\s+\S+\s+\S+\s+/ &&
  305. ProcessHistory("COMMENTS","keysort","CHASSIS",
  306. "!Chassis: $_") && next;
  307. }
  308. next;
  309. }
  310. ProcessHistory("COMMENTS","keysort","CHASSIS","!Chassis: $_");
  311. }
  312. OUT:
  313. ProcessHistory("COMMENTS","keysort","CHASSIS","!\n");
  314. return(0);
  315. }
  316. # This routine parses "dir /all (flash|slotN):"
  317. sub DirSlotN {
  318. print STDERR " In DirSlotN: $_" if ($debug);
  319. my($dev) = (/\s([^\s]+):/);
  320. while (<INPUT>) {
  321. tr/\015//d;
  322. last if (/^$prompt/);
  323. next if (/^(\s*|\s*$cmd\s*)$/);
  324. # return(1) if ($type !~ /^(12[40]|7|36)/);
  325. return(1) if /^\s*\^\s*$/;
  326. return(1) if /(Invalid input|Type help or )/;
  327. return(1) if /No such device/i;
  328. return(1) if /\% ?Error: No such file or directory/;
  329. return(1) if /\% ?Error: The file device is not present/;
  330. return(1) if /\% ?Error: The specified file or directory does not exist/;
  331. return(1) if /No space information available/;
  332. return(-1) if (/command authorization failed/i);
  333. return(1) if /(Open device \S+ failed|Error opening \S+:)/;
  334. /Directory of/ && next;
  335. # . & ..
  336. / \.{1,2}\s+$/ && next;
  337. # clean up totals line
  338. if (/.* (\d+) bytes total/) {
  339. my($tmp) = int($1 / (1024 * 1024));
  340. s/.* $1 bytes total/$tmp\M total/;
  341. }
  342. if (/.*\((\d+) bytes free\)/) {
  343. my($tmp) = int($1 / (1024 * 1024));
  344. s/$1 bytes free/$tmp\M free/;
  345. }
  346. s/ +$//g;
  347. ProcessHistory("FLASH","","","!Flash: $dev: $_");
  348. }
  349. ProcessHistory("","","","!\n");
  350. return(0);
  351. }
  352. # This routine parses "show inventory"
  353. sub ShowInventory {
  354. print STDERR " In ShowInventory: $_" if ($debug);
  355. while (<INPUT>) {
  356. tr/\015//d;
  357. last if (/^$prompt/);
  358. next if (/^(\s*|\s*$cmd\s*)$/);
  359. return(1) if /(Invalid input|Type help or )/;
  360. return(-1) if (/command authorization failed/i);
  361. /-----------------------------/ && next;
  362. ProcessHistory("COMMENTS","keysort","INVENTORY","!Inventory: $_");
  363. }
  364. ProcessHistory("COMMENTS","keysort","INVENTORY","!\n");
  365. return(0);
  366. }
  367. # This routine parses "show vlan"
  368. sub ShowVLAN {
  369. print STDERR " In ShowVLAN: $_" if ($debug);
  370. while (<INPUT>) {
  371. tr/\015//d;
  372. last if (/^$prompt/);
  373. next if (/^(\s*|\s*$cmd\s*)$/);
  374. return(1) if /(Invalid input|Type help or )/;
  375. return(-1) if (/command authorization failed/i);
  376. ProcessHistory("COMMENTS","keysort","VLAN","!VLAN: $_");
  377. }
  378. ProcessHistory("COMMENTS","keysort","VLAN","!\n");
  379. return(0);
  380. }
  381. # This routine processes a "write term"
  382. sub WriteTerm {
  383. print STDERR " In WriteTerm: $_" if ($debug);
  384. while (<INPUT>) {
  385. tr/\015//d;
  386. last if(/^$prompt/);
  387. return(-1) if (/command authorization failed/i);
  388. # skip crap
  389. /^Current Configuration/ && next;
  390. # Dog gone Cool matches to process the rest of the config
  391. /^tftp-server flash / && next; # kill any tftp remains
  392. /^ntp clock-period / && next; # kill ntp clock-period
  393. /^ length / && next; # kill length on serial lines
  394. /^ width / && next; # kill width on serial lines
  395. /^ clockrate / && next; # kill clockrate on serial interfaces
  396. if (/^(enable )?(password|passwd) / && $filter_pwds >= 1) {
  397. ProcessHistory("ENABLE","","","!$1$2 <removed>\n");
  398. next;
  399. }
  400. if (/^(enable secret) / && $filter_pwds >= 2) {
  401. ProcessHistory("ENABLE","","","!$1 <removed>\n");
  402. next;
  403. }
  404. if (/^(enable restricted) / && $filter_pwds >= 2) {
  405. ProcessHistory("ENABLE","","","!$1 <removed>\n");
  406. next;
  407. }
  408. if (/^username (\S+)(\s.*)? secret /) {
  409. if ($filter_pwds >= 2) {
  410. ProcessHistory("USER","keysort","$1","!username $1$2 secret <removed>\n");
  411. } else {
  412. ProcessHistory("USER","keysort","$1","$_");
  413. }
  414. next;
  415. }
  416. if (/^username (\S+)(\s.*)? password ((\d) \S+|\S+)/) {
  417. if ($filter_pwds == 2) {
  418. ProcessHistory("USER","keysort","$1","!username $1$2 password <removed>\n");
  419. } elsif ($filter_pwds == 1 && $4 ne "5"){
  420. ProcessHistory("USER","keysort","$1","!username $1$2 password <removed>\n");
  421. } else {
  422. ProcessHistory("USER","keysort","$1","$_");
  423. }
  424. next;
  425. }
  426. if (/^(\s*)password / && $filter_pwds >= 1) {
  427. ProcessHistory("LINE-PASS","","","!$1password <removed>\n");
  428. next;
  429. }
  430. if (/^\s*neighbor (\S*) password / && $filter_pwds >= 1) {
  431. ProcessHistory("","","","! neighbor $1 password <removed>\n");
  432. next;
  433. }
  434. if (/^(ip ftp password) / && $filter_pwds >= 1) {
  435. ProcessHistory("","","","!$1 <removed>\n"); next;
  436. }
  437. if (/^(ftp-server username.*password) / && $filter_pwds >= 1) {
  438. ProcessHistory("","","","!$1 <removed>\n"); next;
  439. }
  440. if (/^( ip ospf authentication-key) / && $filter_pwds >= 1) {
  441. ProcessHistory("","","","!$1 <removed>\n"); next;
  442. }
  443. # this is reversable, despite 'md5' in the cmd
  444. if (/^( ip ospf message-digest-key \d+ md5) / && $filter_pwds >= 1) {
  445. ProcessHistory("","","","!$1 <removed>\n"); next;
  446. }
  447. # sort route-maps
  448. if (/^route-map (\S+)/) {
  449. my($key) = $1;
  450. my($routemap) = $_;
  451. while (<INPUT>) {
  452. tr/\015//d;
  453. last if (/^$prompt/ || ! /^(route-map |[ !])/);
  454. if (/^route-map (\S+)/) {
  455. ProcessHistory("ROUTEMAP","keysort","$key","$routemap");
  456. $key = $1;
  457. $routemap = $_;
  458. } else {
  459. $routemap .= $_;
  460. }
  461. }
  462. ProcessHistory("ROUTEMAP","keysort","$key","$routemap");
  463. }
  464. # filter out any RCS/CVS tags to avoid confusing local CVS storage
  465. s/\$(Revision|Id):/ $1:/;
  466. # order access-lists
  467. /^access-list\s+(\d\d?)\s+(\S+)\s+(\S+)/ &&
  468. ProcessHistory("ACL $1 $2","$aclsort","$3","$_") && next;
  469. # order extended access-lists
  470. /^access-list\s+(\d\d\d)\s+(\S+)\s+ip\s+host\s+(\S+)/ &&
  471. ProcessHistory("EACL $1 $2","$aclsort","$3","$_") && next;
  472. /^access-list\s+(\d\d\d)\s+(\S+)\s+ip\s+(\d\S+)/ &&
  473. ProcessHistory("EACL $1 $2","$aclsort","$3","$_") && next;
  474. /^access-list\s+(\d\d\d)\s+(\S+)\s+ip\s+any/ &&
  475. ProcessHistory("EACL $1 $2","$aclsort","0.0.0.0","$_") && next;
  476. # order arp lists
  477. /^arp\s+(\d+\.\d+\.\d+\.\d+)\s+/ &&
  478. ProcessHistory("ARP","$aclsort","$1","$_") && next;
  479. /^ip prefix-list\s+(\S+)\s+seq\s+(\d+)\s+(permit|deny)\s+(\d\S+)(\/.*)$/ &&
  480. ProcessHistory("PACL $1 $3","$aclsort","$4","ip prefix-list $1 $3 $4$5\n")
  481. && next;
  482. # order logging statements
  483. /^logging (\d+\.\d+\.\d+\.\d+)/ &&
  484. ProcessHistory("LOGGING","ipsort","$1","$_") && next;
  485. # order/prune snmp-server host statements
  486. # we only prune lines of the form
  487. # snmp-server host a.b.c.d <community>
  488. if (/^snmp-server host (\d+\.\d+\.\d+\.\d+) /) {
  489. if ($filter_commstr) {
  490. my($ip) = $1;
  491. my($line) = "snmp-server host $ip";
  492. my(@tokens) = split(' ', $');
  493. my($token);
  494. while ($token = shift(@tokens)) {
  495. if ($token eq 'version') {
  496. $line .= " " . join(' ', ($token, shift(@tokens)));
  497. } elsif ($token =~ /^(informs?|traps?|(no)?auth)$/) {
  498. $line .= " " . $token;
  499. } else {
  500. $line = "!$line " . join(' ', ("<removed>", join(' ',@tokens)));
  501. last;
  502. }
  503. }
  504. ProcessHistory("SNMPSERVERHOST","ipsort","$ip","$line\n");
  505. } else {
  506. ProcessHistory("SNMPSERVERHOST","ipsort","$1","$_");
  507. }
  508. next;
  509. }
  510. if (/^(snmp-server community) (\S+)/) {
  511. if ($filter_commstr) {
  512. ProcessHistory("SNMPSERVERCOMM","keysort","$_","!$1 <removed>$'") && next;
  513. } else {
  514. ProcessHistory("SNMPSERVERCOMM","keysort","$_","$_") && next;
  515. }
  516. }
  517. # order/prune tacacs/radius server statements
  518. if (/^(tacacs-server|radius-server) key / && $filter_pwds >= 1) {
  519. ProcessHistory("","","","!$1 key <removed>\n"); next;
  520. }
  521. # order clns host statements
  522. /^clns host \S+ (\S+)/ &&
  523. ProcessHistory("CLNS","keysort","$1","$_") && next;
  524. # delete ntp auth password - this md5 is a reversable too
  525. if (/^(ntp authentication-key \d+ md5) / && $filter_pwds >= 1) {
  526. ProcessHistory("","","","!$1 <removed>\n"); next;
  527. }
  528. # order ntp peers/servers
  529. if (/^ntp (server|peer) (\d+)\.(\d+)\.(\d+)\.(\d+)/) {
  530. $sortkey = sprintf("$1 %03d%03d%03d%03d",$2,$3,$4,$5);
  531. ProcessHistory("NTP","keysort",$sortkey,"$_");
  532. next;
  533. }
  534. # order ip host line statements
  535. /^ip host line(\d+)/ &&
  536. ProcessHistory("IPHOST","numsort","$1","$_") && next;
  537. # order atm map-list statements
  538. /^\s+ip\s+(\d+\.\d+\.\d+\.\d+)\s+atm-vc/ &&
  539. ProcessHistory("ATM map-list","ipsort","$1","$_") && next;
  540. # catch anything that wasnt matched above.
  541. ProcessHistory("","","","$_");
  542. # end of config. the ": " game is for the PIX
  543. if (/^end *$/) {
  544. $found_end = 1;
  545. return(0);
  546. }
  547. }
  548. return(0);
  549. }
  550. # dummy function
  551. sub DoNothing {print STDOUT;}
  552. # Main
  553. @commandtable = (
  554. {'show version' => 'ShowVersion'},
  555. {'show bootvar' => 'ShowBoot'},
  556. {'dir flash:' => 'DirSlotN'},
  557. {'dir slot0:' => 'DirSlotN'},
  558. {'show chassis' => 'ShowChassis'},
  559. {'show system' => 'ShowChassis'},
  560. {'show inventory' => 'ShowInventory'},
  561. {'show vlan' => 'ShowVLAN'},
  562. {'show running' => 'WriteTerm'}
  563. );
  564. # Use an array to preserve the order of the commands and a hash for mapping
  565. # commands to the subroutine and track commands that have been completed.
  566. @commands = map(keys(%$_), @commandtable);
  567. %commands = map(%$_, @commandtable);
  568. $cisco_cmds=join(";",@commands);
  569. $cmds_regexp = join("|", map quotemeta($_), @commands);
  570. if (length($host) == 0) {
  571. if ($file) {
  572. print(STDERR "Too few arguments: file name required\n");
  573. exit(1);
  574. } else {
  575. print(STDERR "Too few arguments: host name required\n");
  576. exit(1);
  577. }
  578. }
  579. open(OUTPUT,">$host.new") || die "Can't open $host.new for writing: $!\n";
  580. select(OUTPUT);
  581. # make OUTPUT unbuffered if debugging
  582. if ($debug) { $| = 1; }
  583. if ($file) {
  584. print STDERR "opening file $host\n" if ($debug);
  585. print STDOUT "opening file $host\n" if ($log);
  586. open(INPUT,"<$host") || die "open failed for $host: $!\n";
  587. } else {
  588. print STDERR "executing clogin -t $timeo -c\"$cisco_cmds\" $host\n" if ($debug);
  589. print STDOUT "executing clogin -t $timeo -c\"$cisco_cmds\" $host\n" if ($log);
  590. if (defined($ENV{NOPIPE}) && $ENV{NOPIPE} =~ /^YES/i) {
  591. system "clogin -t $timeo -c \"$cisco_cmds\" $host </dev/null > $host.raw 2>&1" || die "clogin failed for $host: $!\n";
  592. open(INPUT, "< $host.raw") || die "clogin failed for $host: $!\n";
  593. } else {
  594. open(INPUT,"clogin -t $timeo -c \"$cisco_cmds\" $host </dev/null |") || die "clogin failed for $host: $!\n";
  595. }
  596. }
  597. # determine ACL sorting mode
  598. if ($ENV{"ACLSORT"} =~ /no/i) {
  599. $aclsort = "";
  600. }
  601. # determine community string filtering mode
  602. if (defined($ENV{"NOCOMMSTR"}) &&
  603. ($ENV{"NOCOMMSTR"} =~ /yes/i || $ENV{"NOCOMMSTR"} =~ /^$/)) {
  604. $filter_commstr = 1;
  605. } else {
  606. $filter_commstr = 0;
  607. }
  608. # determine password filtering mode
  609. if ($ENV{"FILTER_PWDS"} =~ /no/i) {
  610. $filter_pwds = 0;
  611. } elsif ($ENV{"FILTER_PWDS"} =~ /all/i) {
  612. $filter_pwds = 2;
  613. } else {
  614. $filter_pwds = 1;
  615. }
  616. ProcessHistory("","","","!RANCID-CONTENT-TYPE: force10\n!\n");
  617. ProcessHistory("COMMENTS","keysort","B0","!\n");
  618. ProcessHistory("COMMENTS","keysort","F0","!\n");
  619. ProcessHistory("COMMENTS","keysort","G0","!\n");
  620. TOP: while(<INPUT>) {
  621. tr/\015//d;
  622. if (/\#\s?exit$/) {
  623. $clean_run=1;
  624. last;
  625. }
  626. if (/^Error:/) {
  627. print STDOUT ("$host clogin error: $_");
  628. print STDERR ("$host clogin error: $_") if ($debug);
  629. $clean_run=0;
  630. last;
  631. }
  632. while (/#\s*($cmds_regexp)\s*$/) {
  633. $cmd = $1;
  634. if (!defined($prompt)) {
  635. $prompt = ($_ =~ /^([^#]+#)/)[0];
  636. $prompt =~ s/([][}{)(\\])/\\$1/g;
  637. print STDERR ("PROMPT MATCH: $prompt\n") if ($debug);
  638. }
  639. print STDERR ("HIT COMMAND:$_") if ($debug);
  640. if (! defined($commands{$cmd})) {
  641. print STDERR "$host: found unexpected command - \"$cmd\"\n";
  642. $clean_run = 0;
  643. last TOP;
  644. }
  645. $rval = &{$commands{$cmd}};
  646. delete($commands{$cmd});
  647. if ($rval == -1) {
  648. $clean_run = 0;
  649. last TOP;
  650. }
  651. }
  652. }
  653. print STDOUT "Done $logincmd: $_\n" if ($log);
  654. # Flush History
  655. ProcessHistory("","","","");
  656. # Cleanup
  657. close(INPUT);
  658. close(OUTPUT);
  659. if (defined($ENV{NOPIPE}) && $ENV{NOPIPE} =~ /^YES/i) {
  660. unlink("$host.raw") if (! $debug);
  661. }
  662. # check for completeness
  663. if (scalar(%commands) || !$clean_run || !$found_end) {
  664. if (scalar(%commands)) {
  665. printf(STDOUT "$host: missed cmd(s): %s\n", join(',', keys(%commands)));
  666. printf(STDERR "$host: missed cmd(s): %s\n", join(',', keys(%commands))) if ($debug);
  667. }
  668. if (!$clean_run || !$found_end) {
  669. print STDOUT "$host: End of run not found\n";
  670. print STDERR "$host: End of run not found\n" if ($debug);
  671. system("/usr/bin/tail -1 $host.new");
  672. }
  673. unlink "$host.new" if (! $debug);
  674. }