Collecting and sorting Cisco RME configuration files

Create a batch file to run configuration job:

echo Starting Configuration Collection
C:\Perl\bin\perl.exe G:\SCRIPT\collect.pl
C:\Perl\bin\perl.exe G:\SCRIPT\uniqueconfigs.pl
C:\Perl\bin\perl.exe G:\SCRIPT\sortconfigs.pl
C:\Perl\bin\perl.exe G:\SCRIPT\renameconfigs.pl
PAUSE

Remember to adjust path appropriately!

G:\SCRIPT\collect.pl contents:

use File::Copy;
use File::Path;

rmtree "G:\\CONFIGS";
system "mkdir G:\\CONFIGS\\RAW";
$rootpath = "C:\\Program Files\\CSCOpx\\files\\rme\\dcma\\devfiles";
opendir(DIRHANDLE, $rootpath) die "Cannot opendir: $!";
foreach $name (readdir(DIRHANDLE)) {
if ($name != "." $name != "..") {
#print "Dir: $name\n";
opendir(DIRHANDLE1, $rootpath."\\".$name);
foreach $name1 (readdir(DIRHANDLE1)) {
opendir(DIRHANDLE2, $rootpath."\\".$name."\\".$name1."\\PRIMARY\\RUNNING");
foreach $name2 (readdir(DIRHANDLE2)) {
if ($name2 != "." $name2 != "..") {
opendir(DIRHANDLE3, $rootpath."\\".$name."\\".$name1."\\PRIMARY\\RUNNING\\".$name2);
foreach $name3 (readdir(DIRHANDLE3)) {
if ($name3 != "." $name3 != "..") {
if( $name3 =~ /cfg/) {
print $name3."\n";
open(WRFILE, '>G:\\CONFIGS\\RAW\\'.$name3);
open(ROFILE, $rootpath."\\".$name."\\".$name1."\\PRIMARY\\RUNNING\\".$name2."\\".$name3);
while ($line = <ROFILE> ) {
print WRFILE $line;
}
close(ROFILE);
close(WRFILE);
}
}
}
closedir(DIRHANDLE3);
}
}
closedir(DIRHANDLE2);
}
closedir(DIRHANDLE1);
}
}
closedir(DIRHANDLE);

$root2 = "G:\\CONFIGS\\RAW";
open(WRFILE, '>G:\\CONFIGS\\RAW\\configlist.txt');
opendir(DIR2, $root2) die "Cannot opendir: $!";
foreach $n2 (readdir(DIR2)) {
if ($n2 != "." $n2 != "..") {
print $n2."\n";
print WRFILE $n2."\n";
}
}

closedir(DIR2);


Go to top


G:\SCRIPT\uniqueconfigs.pl contents:

use strict;
my @conf1 = ();
my @conf2 = ();
my $count = 0;
my $countun = 0;
open(JFILE, '>G:\\CONFIGS\\RAW\\uniquelist.txt');
open(CFILE, 'G:\\CONFIGS\\RAW\\configlist.txt');
while (my $line1 = <CFILE>) {
chomp($line1);
if($line1 =~ m/(\d+)-(\d{14}).cfg/) {
push(@conf1,$1);
push(@conf1,$2);
$count++;
}
}

@conf2 = @conf1;
my $conf1sz = @conf1;
my $i = 0;
my $j = 0;
for ($i = 0; $i < $conf1sz; $i++) {
for ($j = 0; $j < $conf1sz; $j++) {
if($conf1[$i] == "AAAA") {
#do nothing
} elsif(($conf1[$i] == $conf2[$j]) && ($i != $j) && ($conf1[$i+1] < $conf2[$j+1])) {
$conf1[$i] = "AAAA";
$conf1[$i+1] = "BBBB";
$j = $j + 1;
} else {
$j = $j + 1;
}
}
$i = $i + 1;
}

for ($i = 0; $i < $conf1sz; $i++) {
if($conf1[$i] != "AAAA") {
print JFILE $conf1[$i]."-".$conf1[$i+1].".cfg\n";
print $conf1[$i]."-".$conf1[$i+1].".cfg\n";
$countun++;
}
$i = $i + 1;
}

print "\n";

close(CFILE);
close(JFILE);
print "Total Config Count: ".$count."\n";
print "Total Unique Configs: ".$countun."\n";

Go to top


G:\SCRIPT\sortconfigs.pl contents:

use File::Copy;
use File::Path;
rmtree "G:\\CONFIGS\\PIX";
rmtree "G:\\CONFIGS\\ASA";
rmtree "G:\\CONFIGS\\FWSM";
rmtree "G:\\CONFIGS\\IOS";
system "mkdir G:\\CONFIGS\\PIX";
system "mkdir G:\\CONFIGS\\ASA";
system "mkdir G:\\CONFIGS\\FWSM";
system "mkdir G:\\CONFIGS\\IOS";
$rootpath = "G:\\CONFIGS\\RAW";
$count = 0;
open(UNFILE, 'G:\\CONFIGS\\RAW\\uniquelist.txt');
while (my $name = <UNFILE>) {
chomp($name);
open(CFILE, 'G:\\CONFIGS\\RAW\\'.$name);
while ($line = <CFILE>) {
if($line =~ /ASA Version/) {
copy('G:\\CONFIGS\\RAW\\'.$name , "G:\\CONFIGS\\ASA\\".$name);
break;
} elsif ($line =~ /PIX Version/) {
copy('G:\\CONFIGS\\RAW\\'.$name, 'G:\\CONFIGS\\PIX\\'.$name);
break;
} elsif ($line =~ /FWSM Version/) {
copy('G:\\CONFIGS\\RAW\\'.$name, 'G:\\CONFIGS\\FWSM\\'.$name);
break;
} elsif ($line =~ /version 12.[0-5]/) {
copy('G:\\CONFIGS\\RAW\\'.$name, 'G:\\CONFIGS\\IOS\\'.$name);
break;
} else {
#do nothing
}
}
close(CFILE);
$count++;
}
close(UNFILE);
print "Total Config Count: ".$count."\n";

Go to top


G:\SCRIPT\sortconfigs.pl contents

This script renames configuration files to their corresponding hostnames, since default Cisco RME format is deviceid-date.cfg

We rename files in IOS folder, but you can easely modify script to use it against ASA, PIX or FWSM folders.

use File::Copy;
use File::Path;
$rootios = "G:\\CONFIGS\\IOS";
$rootpix = "G:\\CONFIGS\\PIX";
$rootasa = "G:\\CONFIGS\\ASA";
$rootfswm = "G:\\CONFIGS\\FWSM";
opendir(ROOTIOS, $rootios);
foreach $iosconf (readdir(ROOTIOS)) {
if ($iosconf != "." $iosconf != "..") {
open(ROFILE, $rootios."\\".$iosconf);
while ($line = <ROFILE>) {
chomp($line);
if($line =~ m/hostname (.*)/) {
print $1." - ";
print $rootios."\\".$iosconf."\n";
copy($rootios."\\".$iosconf, $rootios."\\".$1);
}
}
}
}
closedir(ROOTIOS);
opendir(ROOTIOS, $rootios);
foreach $iosconf (readdir(ROOTIOS)) {
chomp($iosconf);
if ($iosconf != "." $iosconf != ".." ) {
if($iosconf =~ m/(.*.cfg)/) {
unlink $rootios."\\".$1;
}
}
}
closedir(ROOTIOS);

Moving your apps to Amazon or Miscrosoft Clouds?

We can help you analyze your existing infrastructure, identify the cost savings we can achieve by migrating to a cloud provider. We can then execute end-to-end migration plan of your infrastructure and bringing down your TCO.

Cloud Computing

Ready for IPv6 Migration?

The Internet is running out of the equivalent of phone numbers - familiar problem, non-trivial solution.

The world has to move to IPv6, with its 128-bit addresses. But that's easier said than done.

IPv6 Migration

Are you fluent in "Linux"?

Learn Linux from a leading expert and quickly master you Linux skills.

Learn how to simplify your workflow and increase your productivity using tips and techniques of the pros.

Ideal training for Corporate IT Beginners and Advanced IT Admins alike.

Corporate Linux Training

Who's Online

We have 4 guests and no members online