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?


  • Kindly suggest me how to make script using SSH and whole config backup save in Hostname not in IP address.
    Please reply me very soon

    • Saving the output to a file with hostname of device instead of IP

      You have to get the hostname of device using the command ‘sh run | in hostname’ first . This will give out put like ‘hostname MYROUTR’. Then you have to extract the router name from the output using regex feature available on python.

      Following are the solution, modify the script as per below

      add ‘import re’ to the beginning of script.
      Then replace the line ‘filename=device[‘ip’]+’-‘+today+”.txt” ‘ with following commands

      dev_name=net_connect.send_command(‘show run | in hostname’) // getting the hostname
      hname=re.searc(r’hostname\s+(\w+)’,dev_name) //extracting the hostname from the putput
      filename=hostname+today+”.txt” // generating file name

      Complete modified script below

      from netmiko import ConnectHandler
      import getpass
      import sys
      import time
      import re

      ##getting system date

      ##initialising device
      device = {
      ‘device_type’: ‘cisco_ios’,
      ‘ip’: ‘’,
      ‘username’: ‘username’,
      ‘password’: ‘password’,
      ##opening IP file
      print (“Script to take backup of devices, Please enter your credential”)
      device[‘username’]=input(“User name “)
      print(“Enter enable password: “)

      ##taking backup
      for line in ipfile:
      print(“\n\nConnecting Device “,line)
      net_connect = ConnectHandler(**device)
      print (“Reading the running config “)
      output = net_connect.send_command(‘show run’)
      dev_name=net_connect.send_command(‘show run | in hostname’)
      print(“Writing Configuration to file”)
      print (“Configuration saved to file”,filename)
      print (“Access to “+device[‘ip’]+” failed,backup did not taken”)

      print (“\nAll device backup completed”)

  • can’t we run this script through pycharm?

    As i tried, i unable to run.

    Could you help please?

  • i unable to save the configuration with hostname and getting error messag, but with original script its working fine. Could you helpon this please

  • I have run the script example from admin, Naisamuddin PK and Juliet using Raspberry Pi 2 (Debian (version 2)).
    I could SSH to my switch through prove that is working but running the script I kept getting the result below

    pi@raspberrypi:~/Desktop/PyNet $ python3
    Script to take backup of devices, Please enter your credential
    User name:admin
    Enter enable password:

    Connecting Device

    Access to failed,backup did not taken

    Connecting Device

    Access to failed,backup did not taken

    Connecting Device

    Access to failed,backup did not taken

    All device backup completed

    this happen using the three example posted here

    please can you advice what else to do. Is it best run on window only ?

  • Sidda Srinivasarao

    I want to take backup of different vendor switches[cisco, HP, arista] on single script. How is it possible. On paramuko we can do but the problem is output not it in correct format (some ansi letters on that output). Is there any other solution?

Leave a comment :