diff options
Diffstat (limited to 'docs/labs/lab02-inventory-operations')
7 files changed, 246 insertions, 0 deletions
diff --git a/docs/labs/lab02-inventory-operations/compliance_check.py b/docs/labs/lab02-inventory-operations/compliance_check.py new file mode 100644 index 0000000..306b407 --- /dev/null +++ b/docs/labs/lab02-inventory-operations/compliance_check.py @@ -0,0 +1,57 @@ +# Copyright (c) 2021 Arista Networks, Inc. +# Use of this source code is governed by the Apache License 2.0 +# that can be found in the COPYING file. + +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() + +### Compliance Code description +compliance = {"0000":"Configuration is in sync", + "0001": "Config is out of sync", + "0002": "Image is out of sync", + "0003": "Config & image out of sync", + "0004": "Config, Image and Device time are in sync", + "0005": "Device is not reachable", + "0006": "The current EOS version on this device is not supported by CVP. Upgrade the device to manage.", + "0007": "Extensions are out of sync", + "0008": "Config, Image and Extensions are out of sync", + "0009": "Config and Extensions are out of sync", + "0010": "Image and Extensions are out of sync", + "0011": "Unauthorized User", + "0012": "Config, Image, Extension and Device time are out of sync", + "0013": "Config, Image and Device time are out of sync", + "0014": "Config, Extensions and Device time are out of sync", + "0015": "Image, Extensions and Device time are out of sync", + "0016": "Config and Device time are out of sync", + "0017": "Image and Device time are out of sync", + "0018": "Extensions and Device time are out of sync", + "0019": "Device time is out of sync" +} + +# Create connection to CloudVision using Service account token +with open("token.tok") as f: + token = f.read().strip('\n') + +clnt = CvpClient() +clnt.connect(nodes=['cvp1'], username='',password='',api_token=token) + +def check_devices_under_container(client, container): + ''' container is the container ID ''' + + nodeId = container['key'] + nodeName = container['name'] + api = '/ztp/getAllNetElementList.do?' + queryParams = "nodeId={}&queryParam=&nodeName={}&startIndex=0&endIndex=0&contextQueryParam=&ignoreAdd=false&useCache=true".format(nodeId, nodeName) + return client.get(api + queryParams) + + +container = clnt.api.get_container_by_name('TP_LEAFS') + +devices = (check_devices_under_container(clnt,container)) + +for device in devices['netElementList']: + code = device['complianceCode'] + print(device['fqdn'], ' ', code,' ', compliance[code]) diff --git a/docs/labs/lab02-inventory-operations/get_running_configs.py b/docs/labs/lab02-inventory-operations/get_running_configs.py new file mode 100644 index 0000000..5478f2e --- /dev/null +++ b/docs/labs/lab02-inventory-operations/get_running_configs.py @@ -0,0 +1,31 @@ +# Copyright (c) 2021 Arista Networks, Inc. +# Use of this source code is governed by the Apache License 2.0 +# that can be found in the COPYING file. + +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() + +# Create connection to CloudVision +clnt = CvpClient() +clnt.connect(nodes=['cvp1'], username="username",password="password") + +# Get the full inventory +inventory = clnt.api.get_inventory() + +# Create a list of MAC addresses +device_macs = [] +for i in inventory: + device_macs.append(i['systemMacAddress']) + +# Create a dictionary with MAC to running-config mapping +running_configs = {} +for i in device_macs: + running_configs[i] = clnt.api.get_device_configuration(i) + +# Write the running-configs of each device using the hostname as the filename +for i in inventory: + with open(i['fqdn']+'.cfg', 'w') as f: + f.write(running_configs[i['systemMacAddress']])
\ No newline at end of file diff --git a/docs/labs/lab02-inventory-operations/get_running_configs_by_time.py b/docs/labs/lab02-inventory-operations/get_running_configs_by_time.py new file mode 100644 index 0000000..7bbc294 --- /dev/null +++ b/docs/labs/lab02-inventory-operations/get_running_configs_by_time.py @@ -0,0 +1,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)) diff --git a/docs/labs/lab02-inventory-operations/remove_all_devices_legacy.py b/docs/labs/lab02-inventory-operations/remove_all_devices_legacy.py new file mode 100644 index 0000000..f8ca8cb --- /dev/null +++ b/docs/labs/lab02-inventory-operations/remove_all_devices_legacy.py @@ -0,0 +1,30 @@ +# Copyright (c) 2021 Arista Networks, Inc. +# Use of this source code is governed by the Apache License 2.0 +# that can be found in the COPYING file. + +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() + +# Create connection to CloudVision +clnt = CvpClient() +clnt.connect(nodes=['cvp1'], username="username",password="password") + +inventory = clnt.api.get_inventory() + +devices = [] +for netelement in inventory: + devices.append(netelement['systemMacAddress']) + +# Remove devices from provisioning +# This is a legacy API call that removes the devices from Network Provisioning +# in CVP versions older than 2021.3.0, however it does not remove them from +# the Device Inventory as that requires the streaming agent (TerminAttr) to be shutdown, +# which this API does not support. +# To fully decommission a device the device_decommissioning() API can be used, which is +# supported from 2021.3.0+. +# Note that using the delete_devices() function post CVP 2021.3.0 the device will be +# automatically added back to the Undefined container. +clnt.api.delete_devices(devices) diff --git a/docs/labs/lab02-inventory-operations/remove_and_decommission_device.py b/docs/labs/lab02-inventory-operations/remove_and_decommission_device.py new file mode 100644 index 0000000..16e783a --- /dev/null +++ b/docs/labs/lab02-inventory-operations/remove_and_decommission_device.py @@ -0,0 +1,32 @@ +# Copyright (c) 2022 Arista Networks, Inc. +# Use of this source code is governed by the Apache License 2.0 +# that can be found in the COPYING file. + +from cvprac.cvp_client import CvpClient +import ssl +import uuid +import time +ssl._create_default_https_context = ssl._create_unverified_context +import requests.packages.urllib3 +requests.packages.urllib3.disable_warnings() + +# Create connection to CloudVision +clnt = CvpClient() +clnt.connect(nodes=['cvp1'], username="username", password="password") + +device_id = input("Serial number of the device to be decommissioned: ") +request_id = str(uuid.uuid4()) +clnt.api.device_decommissioning(device_id, request_id) + +# This API call will fully decommission the device, ie remove it from both +# Network Provisioning and Device Inventory (telemetry). It send an eAPI request +# to EOS to shutdown the TerminAttr daemon, waits for streaming to stop and then removes +# the device from provisioning and finally decommissions it. This operation can take a few minutes. +# Supported from CVP 2021.3.0+ and CVaaS. +decomm_status = "DECOMMISSIONING_STATUS_SUCCESS" +decomm_result = "" +while decomm_result != decomm_status: + decomm_result = clnt.api.device_decommissioning_status_get_one(request_id)['value']['status'] + time.sleep(10) + +print(decomm_result) diff --git a/docs/labs/lab02-inventory-operations/remove_devices_from_container_legacy.py b/docs/labs/lab02-inventory-operations/remove_devices_from_container_legacy.py new file mode 100644 index 0000000..93e0e19 --- /dev/null +++ b/docs/labs/lab02-inventory-operations/remove_devices_from_container_legacy.py @@ -0,0 +1,32 @@ +# Copyright (c) 2021 Arista Networks, Inc. +# Use of this source code is governed by the Apache License 2.0 +# that can be found in the COPYING file. + +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() + +# Create connection to CloudVision +clnt = CvpClient() +clnt.connect(nodes=['cvp1'], username="username",password="password") + +# Get devices in a specific container +inventory = clnt.api.get_devices_in_container("Undefined") + +# Create device list +devices = [] +for netelement in inventory: + devices.append(netelement['systemMacAddress']) + +# Remove devices from provisioning +# This is a legacy API call that removes the devices from Network Provisioning +# in CVP versions older than 2021.3.0, however it does not remove them from +# the Device Inventory as that requires the streaming agent (TerminAttr) to be shutdown, +# which this API does not support. +# To fully decommission a device the device_decommissioning() API can be used, which is +# supported from 2021.3.0+. +# Note that using the delete_devices() function post CVP 2021.3.0 the device will be +# automatically added back to the Undefined container. +clnt.api.delete_devices(devices) diff --git a/docs/labs/lab02-inventory-operations/remove_devices_legacy.py b/docs/labs/lab02-inventory-operations/remove_devices_legacy.py new file mode 100644 index 0000000..1f274f0 --- /dev/null +++ b/docs/labs/lab02-inventory-operations/remove_devices_legacy.py @@ -0,0 +1,30 @@ +# Copyright (c) 2021 Arista Networks, Inc. +# Use of this source code is governed by the Apache License 2.0 +# that can be found in the COPYING file. + +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() + +# Create connection to CloudVision using Service account token +with open("token.tok") as f: + token = f.read().strip('\n') + +clnt = CvpClient() +clnt.connect(nodes=['cvp1'], username='', password='', api_token=token) + +devices = ["50:08:00:a7:ca:c3","50:08:00:b1:5b:0b","50:08:00:60:c6:76", + "50:08:00:25:9d:36","50:08:00:8b:ee:b1","50:08:00:8c:22:49"] + +# Remove devices from provisioning +# This is a legacy API call that removes the devices from Network Provisioning +# in CVP versions older than 2021.3.0, however it does not remove them from +# the Device Inventory as that requires the streaming agent (TerminAttr) to be shutdown, +# which this API does not support. +# To fully decommission a device the device_decommissioning() API can be used, which is +# supported from 2021.3.0+. +# Note that using the delete_devices() function post CVP 2021.3.0 the device will be +# automatically added back to the Undefined container. +clnt.api.delete_devices(devices) |