cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Bookmark
|
Subscribe
|
794
Views
0
Helpful
0
Replies

NCS540 Python scrip to backup config, run commit replace then load

Hello, I am trying to make a python script that copy running config to a file for backup, then do commit replace to whipe the configuration. After that it gonna load a new config that are already uploaded and then do a new commit replace.

I am doing this because of a migration that have a new configuration.

I have tried do this with this script:

In this script I'm missing the commit replace to reset the configuration, I will add that when I figure out how to run commands that are under configure terminal.

 

import os
import sys
sys.path.append("/pkg/bin")
from ztp_helper import ZtpHelpers
from pprint import pprint

ztp_obj = ZtpHelpers()

# Check if file exists before copying running-config to test.cfg
cmd1 = {"exec_cmd": "dir harddisk:", "prompt_response": ""}
response1 = ztp_obj.xrcmd(cmd1)
if "test.cfg" in response1["output"]:
    print("File already exists, skipping copy.")
else:
    cmd2 = {"exec_cmd": "copy running-config harddisk:test.cfg", "prompt_response": "\n"}
    response2 = ztp_obj.xrcmd(cmd2)
    pprint(response2)

# Load configuration from test.cfg and commit replace
cmd3 = {"exec_cmd": "configure terminal", "prompt_response": ""}
cmd4 = {"exec_cmd": "load harddisk:test.cfg", "prompt_response": "\n"}
cmd5 = {"exec_cmd": "commit replace force", "prompt_response": "yes"}

response3 = ztp_obj.xrcmd(cmd3)
pprint(response3)

response4 = ztp_obj.xrcmd(cmd4)
pprint(response4)

response5 = ztp_obj.xrcmd(cmd5)
pprint(response5)

 

It does not work, I cannot run a command that are under configure terminal.
Anyone now how to do this?

0 Replies 0