Tuesday, July 19, 2011

How to backup Cisco configurations without a TFTP

"expect" is a nice linux program that can be used to automate interactive applications. There fore you can used it to execute cisco commands through a linux machine.
Best way to backup the cisco config is using "show run" command but the problem was how to save running config into a separate machine. You can use "expect" program to do that.

First you need to install "expect" program. In ubuntu/debian its easy

apt-get install expect

Then change the "router" and "pass" variables of the following script and execute on a crontab.



#!/usr/bin/expect

# ------------------- config --------------------- #
set router 192.158.1.1
set pass password
set timeout 1000
set filesave [exec date +%m-%d-%Y]

#------------------------------------------------- #

spawn telnet $router
expect "Password:"
send "$pass\n"
expect ">"
send "en\n"
expect "Password:"
send "$pass\n"


log_file $router--$filesave.cfg
send "term len 0\n"
send "show running-config\n"
expect "end\r"
send "\n"
send "exit\n"
log_file






This will save the file in IP--date.cfg format

4 comments:

  1. Thanks for the info...future article with ssh ?

    ReplyDelete
  2. log_file $device-$filesave.cfg
    send "show running-config\r"
    expect -ex "--More--" {send " ";exp_continue}
    log_file
    interact

    I have the following code for looping between the pages, but it finally ends with the # prompt and hangs. I have to stop it using Ctrl Z. Any solution to loop. Is it possible to include in a "while" loop please suggest.

    ReplyDelete
  3. Send 'terminal length 0' to the device after the inital login. That stops the '--more' prompts when displaying the configuration.

    ReplyDelete
  4. Muchas gracias por tu ayuda. Nos ha ahorrado un gran trabajo en la empresa!. Gracias, Dani.

    ReplyDelete