Debian Codegraph @

docs/labs/lab02-inventory-operations/get_running_configs_by_time.py

raw
1 from cvprac.cvp_client import CvpClient
2 import ssl
3 ssl._create_default_https_context = ssl._create_unverified_context
4 import requests.packages.urllib3
5 requests.packages.urllib3.disable_warnings()
6
7 clnt = CvpClient()
8 clnt.connect(nodes=['cvp1'], username="username",password="password")
9
10 ts = "2021-11-19T15:04:05.0Z" # rfc3339 time
11 uri = "/api/v3/services/compliancecheck.Compliance/GetConfig"
12
13 # Fetch the inventory
14 inventory = clnt.api.get_inventory()
15
16 # Iterate through all devices and get the running-config at the specified time for each device
17 for device in inventory:
18 sn = device['serialNumber']
19 data = {"request":{
20 "device_id": sn,
21 "timestamp": ts,
22 "type":"RUNNING_CONFIG"
23 }
24 }
25 try:
26 resultRunningConfig = clnt.post(uri, data=data)
27 for idx in resultRunningConfig:
28 if 'config' in idx:
29 result = idx['config']
30 break
31 with open(device['hostname']+'.cfg','w') as f:
32 f.write(result)
33 except Exception as e:
34 print("Not able to get configuration for device {} - exception {}".format(device['fqdn'], e))