Advertisement
Guest User

confconv.pl

a guest
Nov 16th, 2011
378
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 11.48 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. ### INCLUDES ###
  4.  
  5. ### MAIN CODE BLOCK ###
  6.  
  7. ### REPLACEMENT DEFINITIONS ###
  8. my @r1, @r2, @r3, @r4, @r5, @r6, @bb1, @bb2, @bb3, @sw1, @sw2, @sw3, @sw4;
  9. my @r1match, @r2match, @r3match, @r4match, @r5match, @r6match, @bb1match, @bb2match, @bb3match, @sw1match, @sw2match, @sw3match, @sw4match, @globalmatch;
  10. my @r1set, @r2set, @r3set, @r4set, @r5set, @r6set, @bb1set, @bb2set, @bb3set, @sw1set, @sw2set, @sw3set, @sw4set, @globalset;
  11.  
  12. #Global
  13. push (@globalmatch, "clock rate .*");
  14. push (@globalset, "clock rate 8000000\r");
  15.  
  16. push (@globalmatch, "clockrate.*");
  17. push (@globalset, "clock rate 8000000\r");
  18.  
  19. push (@globalmatch, "frame-relay intf-type.*");
  20. push (@globalset, "\!\r");
  21.  
  22. push (@globalmatch, "alias exec en.*");
  23. push (@globalset, "\!\r");
  24.  
  25. push (@globalmatch, "enable secret.*");
  26. push (@globalset, "enable secret cisco\r");
  27.  
  28. push (@globalmatch, "exec-timeout.*");
  29. push (@globalset, "\!\r");
  30.  
  31. #push (@globalmatch, "switchport host");
  32. #push (@globalset, "switchport mode access");
  33.  
  34. #R1
  35. push (@r1match, "hostname.*");
  36. push (@r1set, "hostname R1\r");
  37.  
  38. push (@r1match, "Serial0\/0\/0");
  39. push (@r1set, "s0/0");
  40.  
  41. push (@r1match, "Serial0\/1\/0");
  42. push (@r1set, "s0/1");
  43.  
  44. #R2
  45. push (@r2match, "hostname.*");
  46. push (@r2set, "hostname R2\r");
  47.  
  48. push (@r2match, "Serial0\/0\/0");
  49. push (@r2set, "s0/0");
  50.  
  51. push (@r2match, "Serial0\/1\/0");
  52. push (@r2set, "s0/1");
  53.  
  54. #R3
  55. push (@r3match, "hostname.*");
  56. push (@r3set, "hostname R3\r");
  57.  
  58. #R4
  59. push (@r4match, "hostname.*");
  60. push (@r4set, "hostname R4\r");
  61.  
  62. push (@r4match, "Serial0\/0\/0");
  63. push (@r4set, "s0/0");
  64.  
  65. push (@r4match, "Serial0\/1\/0");
  66. push (@r4set, "s0/1");
  67.  
  68. #R5
  69. push (@r5match, "hostname.*");
  70. push (@r5set, "hostname R5\r");
  71.  
  72. push (@r5match, "Serial0\/0\/0");
  73. push (@r5set, "s0/0");
  74.  
  75. push (@r5match, "Serial0\/1\/0");
  76. push (@r5set, "s0/1");
  77.  
  78. #R6
  79. push (@r6match, "hostname.*");
  80. push (@r6set, "hostname R6\r");
  81.  
  82. push (@r6match, "Serial0\/0\/0");
  83. push (@r6set, "s0/0");
  84.  
  85. #BB1
  86. push (@bb1match, "hostname.*");
  87. push (@bb1set, "hostname BB1\r");
  88.  
  89. push (@bb1match, "frame-relay switching");
  90. push (@bb1set, "!\r");
  91.  
  92. push (@bb1match, "interface Serial6");
  93. push (@bb1set, "interface s0/0");
  94.  
  95. push (@bb1match, "interface Serial9");
  96. push (@bb1set, "interface s0/1");
  97.  
  98. push (@bb1match, "frame-relay route .*");
  99. push (@bb1set, "!\r");
  100.  
  101. push (@bb1match, "description.*");
  102. push (@bb1set, "!\r");
  103.  
  104. #BB2
  105. push (@bb2match, "hostname.*");
  106. push (@bb2set, "hostname BB2\r");
  107.  
  108. push (@bb2match, "Ethernet0");
  109. push (@bb2set, "f0/0");
  110.  
  111. push (@bb2match, "description.*");
  112. push (@bb2set, "!\r");
  113.  
  114. #BB3
  115. push (@bb3match, "hostname.*");
  116. push (@bb3set, "hostname BB3\r");
  117.  
  118. push (@bb3match, "Serial0");
  119. push (@bb3set, "s0/0");
  120.  
  121. push (@bb3match, "Ethernet0");
  122. push (@bb3set, "f0/0");
  123.  
  124. push (@bb3match, "description.*");
  125. push (@bb3set, "!\r");
  126.  
  127. #SW1
  128. push (@sw1match, "hostname.*");
  129. push (@sw1set, "hostname SW1\r");
  130.  
  131. #SW2
  132. push (@sw2match, "hostname.*");
  133. push (@sw2set, "hostname SW2\r");
  134.  
  135. #SW3
  136. push (@sw3match, "hostname.*");
  137. push (@sw3set, "hostname SW3\r");
  138.  
  139. #SW4
  140. push (@sw4match, "hostname.*");
  141. push (@sw4set, "hostname SW4\r");
  142.  
  143. ### Open original data files
  144. open (INPUT, "r1.txt") || die ("Could not open r1.txt!\r\r");
  145. @r1 = <INPUT>;
  146.  
  147. open (INPUT, "r2.txt") || die ("Could not open r2.txt!\r\r");
  148. @r2 = <INPUT>;
  149.  
  150. open (INPUT, "r3.txt") || die ("Could not open r3.txt!\r\r");
  151. @r3 = <INPUT>;
  152.  
  153. open (INPUT, "r4.txt") || die ("Could not open r4.txt!\r\r");
  154. @r4 = <INPUT>;
  155.  
  156. open (INPUT, "r5.txt") || die ("Could not open r5.txt!\r\r");
  157. @r5 = <INPUT>;
  158.  
  159. open (INPUT, "r6.txt") || die ("Could not open r6.txt!\r\r");
  160. @r6 = <INPUT>;
  161.  
  162. open (INPUT, "bb1.txt") || die ("Could not open bb1.txt!\r\r");
  163. @bb1 = <INPUT>;
  164.  
  165. open (INPUT, "bb2.txt") || die ("Could not open bb2.txt!\r\r");
  166. @bb2 = <INPUT>;
  167.  
  168. open (INPUT, "bb3.txt") || die ("Could not open bb3.txt!\r\r");
  169. @bb3 = <INPUT>;
  170.  
  171. open (INPUT, "sw1.txt") || die ("Could not open sw1.txt!\r\r");
  172. @sw1 = <INPUT>;
  173.  
  174. open (INPUT, "sw2.txt") || die ("Could not open sw2.txt!\r\r");
  175. @sw2 = <INPUT>;
  176.  
  177. open (INPUT, "sw3.txt") || die ("Could not open sw3.txt!\r\r");
  178. @sw3 = <INPUT>;
  179.  
  180. open (INPUT, "sw4.txt") || die ("Could not open sw4.txt!\r\r");
  181. @sw4 = <INPUT>;
  182.  
  183. #R1
  184. my @newFile;
  185. foreach $line (@r1) {
  186.     chomp ($line);
  187.     for ($n = 0; $n < @r1match; $n++) {
  188.         $line =~ s/$r1match[$n]/$r1set[$n]/;
  189.     }
  190.     for ($n = 0; $n < @globalmatch; $n++) {
  191.         $line =~ s/$globalmatch[$n]/$globalset[$n]/;
  192.     }
  193.     push (@newFile, $line);
  194. }
  195. @r1 = @newFile;
  196.  
  197. #R2
  198. my @newFile;
  199. foreach $line (@r2) {
  200.     chomp ($line);
  201.     for ($n = 0; $n < @r2match; $n++) {
  202.         $line =~ s/$r2match[$n]/$r2set[$n]/;
  203.     }
  204.     for ($n = 0; $n < @globalmatch; $n++) {
  205.         $line =~ s/$globalmatch[$n]/$globalset[$n]/;
  206.     }
  207.     push (@newFile, $line);
  208. }
  209. @r2 = @newFile;
  210.  
  211. #R3
  212. my @newFile;
  213. foreach $line (@r3) {
  214.     chomp ($line);
  215.     for ($n = 0; $n < @r3match; $n++) {
  216.         $line =~ s/$r3match[$n]/$r3set[$n]/;
  217.     }
  218.     for ($n = 0; $n < @globalmatch; $n++) {
  219.         $line =~ s/$globalmatch[$n]/$globalset[$n]/;
  220.     }
  221.     push (@newFile, $line);
  222. }
  223. @r3 = @newFile;
  224.  
  225. #R4
  226. my @newFile;
  227. foreach $line (@r4) {
  228.     chomp ($line);
  229.     for ($n = 0; $n < @r4match; $n++) {
  230.         $line =~ s/$r4match[$n]/$r4set[$n]/;
  231.     }
  232.     for ($n = 0; $n < @globalmatch; $n++) {
  233.         $line =~ s/$globalmatch[$n]/$globalset[$n]/;
  234.     }
  235.     push (@newFile, $line);
  236. }
  237. @r4 = @newFile;
  238.  
  239. #R5
  240. my @newFile;
  241. foreach $line (@r5) {
  242.     chomp ($line);
  243.     for ($n = 0; $n < @r5match; $n++) {
  244.         $line =~ s/$r5match[$n]/$r5set[$n]/;
  245.     }
  246.     for ($n = 0; $n < @globalmatch; $n++) {
  247.         $line =~ s/$globalmatch[$n]/$globalset[$n]/;
  248.     }
  249.     push (@newFile, $line);
  250. }
  251. @r5 = @newFile;
  252.  
  253. #R6
  254. my @newFile;
  255. foreach $line (@r6) {
  256.     chomp ($line);
  257.     for ($n = 0; $n < @r6match; $n++) {
  258.         $line =~ s/$r6match[$n]/$r6set[$n]/;
  259.     }
  260.     for ($n = 0; $n < @globalmatch; $n++) {
  261.         $line =~ s/$globalmatch[$n]/$globalset[$n]/;
  262.     }
  263.     push (@newFile, $line);
  264. }
  265. @r6 = @newFile;
  266.  
  267. #BB1
  268. my @newFile;
  269. $omit = 0;
  270. foreach $line (@bb1) {
  271.     chomp ($line);
  272.     if ($line =~ /interface Serial0.*/) { $omit = 1;}
  273.     if ($line =~ /interface\ Serial6.*/) { $omit = 0;}
  274.     if ($line =~ /interface\ Serial7.*/) { $omit = 1;}
  275.     if ($line =~ /interface\ Serial9.*/) { $omit = 0;}
  276.     for ($n = 0; $n < @bb1match; $n++) {
  277.         $line =~ s/$bb1match[$n]/$bb1set[$n]/;
  278.     }
  279.     for ($n = 0; $n < @globalmatch; $n++) {
  280.         $line =~ s/$globalmatch[$n]/$globalset[$n]/;
  281.     }
  282.     if ($omit ne 1) { push (@newFile, $line); }
  283. }
  284. @bb1 = @newFile;
  285.  
  286. #BB2
  287. my @newFile;
  288. foreach $line (@bb2) {
  289.     chomp ($line);
  290.     for ($n = 0; $n < @bb2match; $n++) {
  291.         $line =~ s/$bb2match[$n]/$bb2set[$n]/;
  292.     }
  293.     for ($n = 0; $n < @globalmatch; $n++) {
  294.         $line =~ s/$globalmatch[$n]/$globalset[$n]/;
  295.     }
  296.     push (@newFile, $line);
  297. }
  298. @bb2 = @newFile;
  299.  
  300. #BB3
  301. my @newFile;
  302. foreach $line (@bb3) {
  303.     chomp ($line);
  304.     for ($n = 0; $n < @bb3match; $n++) {
  305.         $line =~ s/$bb3match[$n]/$bb3set[$n]/;
  306.     }
  307.     for ($n = 0; $n < @globalmatch; $n++) {
  308.         $line =~ s/$globalmatch[$n]/$globalset[$n]/;
  309.     }
  310.     push (@newFile, $line);
  311. }
  312. @bb3 = @newFile;
  313.  
  314. #SW1
  315. my @newFile;
  316. foreach $line (@sw1) {
  317.     chomp ($line);
  318.     for ($n = 0; $n < @sw1match; $n++) {
  319.         $line =~ s/$sw1match[$n]/$sw1set[$n]/;
  320.     }
  321.     for ($n = 0; $n < @globalmatch; $n++) {
  322.         $line =~ s/$globalmatch[$n]/$globalset[$n]/;
  323.     }
  324.     push (@newFile, $line);
  325. }
  326. @sw1 = @newFile;
  327.  
  328. #SW2
  329. my @newFile;
  330. foreach $line (@sw2) {
  331.     chomp ($line);
  332.     for ($n = 0; $n < @sw2match; $n++) {
  333.         $line =~ s/$sw2match[$n]/$sw2set[$n]/m;
  334.     }
  335.     for ($n = 0; $n < @globalmatch; $n++) {
  336.         $line =~ s/$globalmatch[$n]/$globalset[$n]/m;
  337.     }
  338.     push (@newFile, $line);
  339. }
  340. @sw2 = @newFile;
  341.  
  342. #SW3
  343. my @newFile;
  344. foreach $line (@sw3) {
  345.     chomp ($line);
  346.     for ($n = 0; $n < @sw3match; $n++) {
  347.         $line =~ s/$sw3match[$n]/$sw3set[$n]/m;
  348.     }
  349.     for ($n = 0; $n < @globalmatch; $n++) {
  350.         $line =~ s/$globalmatch[$n]/$globalset[$n]/m;
  351.     }
  352.     push (@newFile, $line);
  353. }
  354. @sw3 = @newFile;
  355.  
  356. #SW4
  357. my @newFile;
  358. foreach $line (@sw4) {
  359.     chomp ($line);
  360.     for ($n = 0; $n < @sw4match; $n++) {
  361.         $line =~ s/$sw4match[$n]/$sw4set[$n]/m;
  362.     }
  363.     for ($n = 0; $n < @globalmatch; $n++) {
  364.         $line =~ s/$globalmatch[$n]/$globalset[$n]/m;
  365.     }
  366.     push (@newFile, $line);
  367. }
  368. @sw4 = @newFile;
  369.  
  370.  
  371. # ADD SOME GLOBAL COMMANDS
  372. #$routerGlobal = "conf t\rint ra f0/0 , f0/1\rspeed 100\rduplex full\rend\r\r\r";
  373. #$switchGlobal = "conf t\rint ra f1/1 - 15\rspeed 100\rduplex full\rline con 0\rexec-timeout 0\rend\r\r\r";
  374.  
  375. push (@r1, $routerGlobal);
  376. push (@r2, $routerGlobal);
  377. push (@r3, $routerGlobal);
  378. push (@r4, $routerGlobal);
  379. push (@r5, $routerGlobal);
  380. push (@r6, $routerGlobal);
  381. push (@bb1, $routerGlobal);
  382. push (@bb2, $routerGlobal);
  383. push (@bb3, $routerGlobal);
  384. push (@sw1, $switchGlobal);
  385. push (@sw2, $switchGlobal);
  386. push (@sw3, $switchGlobal);
  387. push (@sw4, $switchGlobal);
  388.  
  389. # WRITE FILES
  390. open (OUTFILE, ">R1-converted.cfg") || die ("Could not open file for writing!\r\r");
  391. print OUTFILE "\r\r\r\ren\rconfig t\r";
  392. print OUTFILE @r1;
  393.  
  394. open (OUTFILE, ">R2-converted.cfg") || die ("Could not open file for writing!\r\r");
  395. print OUTFILE "\r\r\r\ren\rconfig t\r";
  396. print OUTFILE @r2;
  397.  
  398. open (OUTFILE, ">R3-converted.cfg") || die ("Could not open file for writing!\r\r");
  399. print OUTFILE "\r\r\r\ren\rconfig t\r";
  400. print OUTFILE @r3;
  401.  
  402. open (OUTFILE, ">R4-converted.cfg") || die ("Could not open file for writing!\r\r");
  403. print OUTFILE "\r\r\r\ren\rconfig t\r";
  404. print OUTFILE @r4;
  405.  
  406. open (OUTFILE, ">R5-converted.cfg") || die ("Could not open file for writing!\r\r");
  407. print OUTFILE "\r\r\r\ren\rconfig t\r";
  408. print OUTFILE @r5;
  409.  
  410. open (OUTFILE, ">R6-converted.cfg") || die ("Could not open file for writing!\r\r");
  411. print OUTFILE "\r\r\r\ren\rconfig t\r";
  412. print OUTFILE @r6;
  413.  
  414. open (OUTFILE, ">BB1-converted.cfg") || die ("Could not open file for writing!\r\r");
  415. print OUTFILE "\r\r\r\ren\rconfig t\r";
  416. print OUTFILE @bb1;
  417.  
  418. open (OUTFILE, ">BB2-converted.cfg") || die ("Could not open file for writing!\r\r");
  419. print OUTFILE "\r\r\r\ren\rconfig t\r";
  420. print OUTFILE @bb2;
  421.  
  422. open (OUTFILE, ">BB3-converted.cfg") || die ("Could not open file for writing!\r\r");
  423. print OUTFILE "\r\r\r\ren\rconfig t\r";
  424. print OUTFILE @bb3;
  425.  
  426. open (OUTFILE, ">SW1-converted.cfg") || die ("Could not open file for writing!\r\r");
  427. print OUTFILE "\r\r\r\ren\rconfig t\r";
  428. print OUTFILE @sw1;
  429.  
  430. open (OUTFILE, ">SW2-converted.cfg") || die ("Could not open file for writing!\r\r");
  431. print OUTFILE "\r\r\r\ren\rconfig t\r";
  432. print OUTFILE @sw2;
  433.  
  434. open (OUTFILE, ">SW3-converted.cfg") || die ("Could not open file for writing!\r\r");
  435. print OUTFILE "\r\r\r\ren\rconfig t\r";
  436. print OUTFILE @sw3;
  437.  
  438. open (OUTFILE, ">SW4-converted.cfg") || die ("Could not open file for writing!\r\r");
  439. print OUTFILE "\r\r\r\ren\rconfig t\r";
  440. print OUTFILE @sw4;
  441.  
  442. system ('gedit R1-converted.cfg &');
  443. select(undef, undef, undef, 0.3);
  444. system ('gedit R2-converted.cfg &');
  445. select(undef, undef, undef, 0.1);
  446. system ('gedit R3-converted.cfg &');
  447. select(undef, undef, undef, 0.1);
  448. system ('gedit R4-converted.cfg &');
  449. select(undef, undef, undef, 0.1);
  450. system ('gedit R5-converted.cfg &');
  451. select(undef, undef, undef, 0.1);
  452. system ('gedit R6-converted.cfg &');
  453. select(undef, undef, undef, 0.1);
  454. system ('gedit SW1-converted.cfg &');
  455. select(undef, undef, undef, 0.1);
  456. system ('gedit SW2-converted.cfg &');
  457. select(undef, undef, undef, 0.1);
  458. system ('gedit SW3-converted.cfg &');
  459. select(undef, undef, undef, 0.1);
  460. system ('gedit SW4-converted.cfg &');
  461. select(undef, undef, undef, 0.1);
  462. system ('gedit BB1-converted.cfg &');
  463. select(undef, undef, undef, 0.1);
  464. system ('gedit BB2-converted.cfg &');
  465. select(undef, undef, undef, 0.1);
  466. system ('gedit BB3-converted.cfg &');
  467.  
  468.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement