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. -> 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

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”  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 :


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

    Script to take backup of devices, Please enter your credential
    User name admin
    Traceback (most recent call last):
    File “”, 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


    Thank you in advance

    • Thanks for your feedback Chris.

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

      Admin team

    • Hi Chris,

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

      net_connect.send_command_expect(‘login’, expect_string=’username:’)
      net_connect.send_command_expect(‘localuser’, expect_string=’password:’)

      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?


Leave a comment :