Network Automation using Python – Part VI – Automatic backup of multiple switches

Python Backup Script

 

Continuing our Networking Automation using Python blog series, here is the Part 6.

Here we are explaining a simple script to take the backup of multiple Cisco switches/routers quickly. You can schedule the script using crone or job scheduler so it will automatically take daily backup without your intervention. The script will take the output of ‘sh run’ and save to the file. The file name would be  device IP address + today’s date .

Please read part 1 and part 2 to get started with python and to run your first program. Please read part 4 for detailed steps on how to take an SSH session of a switch.

The script have two files

  1. ipfile.txt -> this file store all the device IP address
  2. autobackup.py -> This is the python script

How to run :

Step 1. Download the autobackup and iplist to the same folder

Step 2. Change the autobackup.txt to autobackup-cisco.py

Step 3. Open iplist.txt and save with all your device IP address which need to be backed up.

——————- advertisements ——————-

———————————————————-

Step 4. Open command prompt “CMD” and navigate to the folder where you have saved script and ‘iplist.txt’

Step 5. Run script by typing “python autobackup-cisco.py”  on command prompt

Step 6. You can see the backup of device on same folder with the filename device IP address+ date

——————- advertisements ——————-

———————————————————-

Sample screenshot below.

Hope this post helped you.

You can read more posts on Network automation using Python here. Please use the comments section for any queries/suggestions .

Reference :

https://www.tutorialspoint.com/python

https://www.python.org/

16 comments

  • i tried to run the command but get this error message

    D:\py>python autobackup.py
    Script to take backup of devices, Please enter your credential
    User name admin
    Traceback (most recent call last):
    File “autobackup.py”, line 23, in
    device[‘username’]=input(“User name “)
    File “”, line 1, in
    NameError: name ‘admin’ is not defined

    • A little late to be adding this but I had the same issue. I found that if I used double quotes when entering my name that the script would run.

  • Yes, It worked when username is specified with double quotes. Thanks.

  • Hello, much thanks

    It works super fine for Routers and switches, i wish to use this for firewall, unfortunately on firewall on login we use ssh and after login in going to global configuration we use command ASA>login, instead of en, kindly assist on how we can integrate this on script

    ssh.
    username:john.doe
    Password:XXXXXX
    ASA>
    ASA>
    ASA>login
    username:localuser
    password:Password212

    Thank you in advance
    Chris

    • Thanks for your feedback Chris.

      We will consider this in one of our future (soon) posts.

      Regards,
      Admin team

    • Hi Chris,

      You could use following commands instead of net_connect.enable()

      time.sleep(1)
      net_connect.send_command_expect(‘login’, expect_string=’username:’)
      time.sleep(1)
      net_connect.send_command_expect(‘localuser’, expect_string=’password:’)
      time.sleep(1)
      net_connect.send_command(‘Password212’)

      This will send command and will read until it is detecting the given ‘expect_string’ back from the device

  • worked for me fine thanks for the script #sharingiscaring

  • but i need configuration backup of the device

  • but most of the devices in our environment it’s failing it says access to device deviceaddress failed back up did not taken may i know the reason why?

    • please provide error
      Please ensure you have reach ability to to device from the script running machine and you have given device ip address on iplist.txt file

  • and i want to know weather the script is using ssh or telnet to connect to devices?

  • Hi,
    Do you have any similar script for Dell Force10 switches?

    Regards,
    Ali

Leave a comment :