summaryrefslogtreecommitdiffstats
path: root/docs/labs/lab02-inventory-operations/get_running_configs_by_time.py
blob: 7bbc294470e536184d79c34d96028fc008d0cf40 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
from cvprac.cvp_client import CvpClient
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
import requests.packages.urllib3
requests.packages.urllib3.disable_warnings()

clnt = CvpClient()
clnt.connect(nodes=['cvp1'], username="username",password="password")

ts = "2021-11-19T15:04:05.0Z" # rfc3339 time
uri = "/api/v3/services/compliancecheck.Compliance/GetConfig"

# Fetch the inventory
inventory = clnt.api.get_inventory()

# Iterate through all devices and get the running-config at the specified time for each device
for device in inventory:
    sn = device['serialNumber']
    data = {"request":{
        "device_id": sn,
        "timestamp": ts,
        "type":"RUNNING_CONFIG"
        }
    }
    try:
        resultRunningConfig = clnt.post(uri, data=data)
        for idx in resultRunningConfig:
            if 'config' in idx:
                result = idx['config']
                break
        with open(device['hostname']+'.cfg','w') as f:
            f.write(result)
    except Exception as e:
        print("Not able to get configuration for device {} - exception {}".format(device['fqdn'], e))