Link to home
Start Free TrialLog in
Avatar of djohnson104
djohnson104Flag for United States of America

asked on

Script to back up Cisco devices running config.

I want to create a simple script that can be run daily against my Cisco ASA and Layer 3 switch to back up runnning congi. I want these backed up to a local tftp server. Maybe some thing that will run daily via cron or windows scheduled task.
Avatar of bdeterding
bdeterding

I can't claim to know much about the switch - but you configure a tftp server in ASA with "tftp-server inside <ip> <path"

Then you would need a perl/expect script along the following lines ...
NOTE 1: This will actually log on, perform a sh run, and write the results out to a local file. It could be modified to do a "copy tftp" instead of a sh run but I included this to handle your switch config as well.
NOTE 2: This isn't copy/paste perfect but it should get you most of the way there
NOTE 3: This is obviously a small piece of a larger program I have - you'd have to handle your own variable declaration, return code handling, etc.

sub mfw_backup
{
        $Expect::Debug = 0;
        $Expect::Exp_Internal = 0;
        $Expect::Log_Stdout = 1;

        my ( $status_textbuffer,$status_textview ) = @_;
        my $timeout=30;
        my $ret=0;

        $exp = new Expect;
        my $exp = Expect->spawn("ssh -p $port $user\@$ip") or warn "expect spawn failed\n";

        $exp->expect($timeout,  ["assword:", sub { $exp->send("$pw\n");}],
                                ["Connection refused", sub { print "\n\t-Connection Refused\n"; $ret=1; }],
                                ["ssh_exchange_identification", sub { print "\n\t-SSH Server Problem\n"; $ret=8; }],
                                ["Bad port", sub { print "\n\t-Bad port\n"; $ret=9; }],
                                ["No route to host", sub { print "\n\t-No Route to Host\n"; $ret=7; }],
                                ["Connection timed out", sub { print "\n\t-Connection Time Out\n"; $ret=1; }],
                                [timeout, sub { print "\n\t-Timed out! Looks like connection issue\n"; $ret=1; }]);
                                if ( $ret != "0" ) { $exp->soft_close(); if ( $? != 0 ) { $exp->hard_close(); } return $ret; }

                # Log on
                $exp->expect($timeout,  ["assword:", sub { print "\n\t-Auth failed (initial)\n"; $ret=2; }],
                                        ["> ", sub { $exp->send("en\n"); }],
                                        [timeout, sub { $ret=3; }]);
                                        if ( $ret != "0" ) { $exp->soft_close(); if ( $? != 0 ) { $exp->hard_close(); } return $ret; }

                $exp->expect($timeout,  ["assword:", sub { $exp->send("$epw\n"); }],
                                        [timeout, sub { $ret=3; }]);
                                        if ( $ret != "0" ) { $exp->soft_close(); if ( $? != 0 ) { $exp->hard_close(); } return $ret; }

                $exp->expect($timeout,  ["assword:", sub { $exp->send("exit\n"); print "\n\t-Auth failed (enable)\n"; $ret=2; }],
                                        ["# .*", sub { $exp->send("conf t\n"); print "\n\t-Login Successful!\n"; }],
                                        [timeout, sub { $ret=3; }]);
                                        if ( $ret != "0" ) { $exp->soft_close(); if ( $? != 0 ) { $exp->hard_close(); } return $ret; }

                $exp->expect($timeout,  ["# .*", sub { $exp->send("no pager\n"); }],
                                        [timeout, sub { $ret=3; }]);
                                        if ( $ret != "0" ) { $exp->soft_close(); if ( $? != 0 ) { $exp->hard_close(); } return $ret; }

                # Write the contents of 'sh ver' and 'sh run' to a file
                $exp->debug(0);
                $exp->log_file('.output');

                print "\n\t-Getting Config\n";
                $exp->expect($timeout,  ["# .*", sub { $exp->send("sh run\n"); }],
                                        [timeout, sub { $ret=3; }]);
                                        if ( $ret != "0" ) { $exp->soft_close(); if ( $? != 0 ) { $exp->hard_close(); } return $ret; }

                $exp->expect($timeout,  ["# .*", sub { $exp->send("exit\n"); }],
                                        [timeout, sub { $ret=3; }]);
                                        if ( $ret != "0" ) { $exp->soft_close(); if ( $? != 0 ) { $exp->hard_close(); } return $ret; }

                $exp->expect($timeout,  ["# .*", sub { $exp->send("exit\n"); }],
                                        [timeout, sub { $ret=3; }]);
                                        if ( $ret != "0" ) { $exp->soft_close(); if ( $? != 0 ) { $exp->hard_close(); } return $ret; }

                $exp->expect(undef);
                $exp->log_file(undef);
                $exp->soft_close();
                if ( $? != 0 ) { $exp->hard_close(); }

                if ( $ret == 0 )
                {
                        # Read the config and write it to the right place
                        open(infile,'<.output') or die(" .Could not open temp log file\n");
                        open(outfile,">$DIR/$custid-$host/$host.txt") or die (" .Could not write pix config in host's directory\n");

                        while (my $test = <infile>)
                        {
                                print outfile $test;

                        close(infile);
                        close(outfile);
                        unlink(".output");
                        return $ret;
                }
ASKER CERTIFIED SOLUTION
Avatar of Jan Bacher
Jan Bacher
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of djohnson104

ASKER

jesper

cd /usr/ports/net-mgmt/rancid

make install clean


That was what i needed!