We help IT Professionals succeed at work.

Script to back up Cisco devices running config.

4,369 Views
Last Modified: 2008-01-09
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.
Comment
Watch Question

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;
                }
CERTIFIED EXPERT
Most Valuable Expert 2015
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
jesper

cd /usr/ports/net-mgmt/rancid

make install clean


That was what i needed!

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.