I gave an example earlier on grabbing configs from routers and switches with ssh and expect, here is the complete method for grabbing cisco asa configs. This script works a little different from the router and switch back up in that it does not use a device file for the ip addresses to back up but rather takes an argument(since I didn’t have as many asa’s to back up as routers and switches). So you would call this one like this:
perl asabackup.pl 192.168.1.5
It would connect to, get the config, clean it up a little, and save it to a directory named from the ip with a filename of ipaddress-date.txt so that you could have a backup for everyday every hour etc.. This uses the command more system:running-config since that gives more information than just a show run does on the asa’s. This is a scaled down version since I have incorporated the encrypted password file on my server, if you would like to see that version just post a request.
#!/usr/bin/perl -w
use Expect;
use strict;
use POSIX;
#Change these to your settings
my $username = "username";
my $password = "password";
my $backupdir = "/usr/local/configs/";
my $host = $ARGV[0];
my $filename;
my $timeout = 10;
my @logfile;
$filename = strftime("$host-%m-%d-%Y.%H:%M.txt", localtime);
my $filepath = "$backupdir$host/$filename";
mkdir "$backupdir$host";
&getconfig;
@ARGV = ("$filepath");
$^I = ".bak";
while (<>) {
s/\w+#\s.*\r//||s/^Logoff//||s/^Connection to.*//||s/\r//;
print;
}
unlink "$filepath.bak";
sub getconfig {
my $command = Expect->spawn("ssh $username\@$host");
$command->expect($timeout, -re => "password:") or die("Failed to get password prompt");
print $command "$password\r";
sleep 1;
print $command "enable\r";
$command->expect($timeout, -re => "Password:") or die("Did not get a password prompt\n");
print $command "$password\r";
print $command "terminal pager 0\r";
$command->log_file("$filepath");
print $command "more system:running-config\r";
my $redo = 1;
while($redo)
{ $command->clear_accum();
$command->expect(1,
[ qr/More/ => sub { my $comand = shift; print $command "\r"; exp_continue; } ],
[ qr/#/ => sub { my $exp = shift; $redo = 0; exp_continue; } ],
);
}
print $command "exit\r";
$command->soft_close();
$command->log_file(undef);
}