| 1 |
# Copyright (c) 2022 Arista Networks, Inc. |
| 2 |
# Use of this source code is governed by the Apache License 2.0 |
| 3 |
# that can be found in the COPYING file. |
| 4 |
|
| 5 |
from cvprac.cvp_client import CvpClient |
| 6 |
import ssl |
| 7 |
ssl._create_default_https_context = ssl._create_unverified_context |
| 8 |
import requests.packages.urllib3 |
| 9 |
requests.packages.urllib3.disable_warnings() |
| 10 |
|
| 11 |
# Create connection to CloudVision |
| 12 |
clnt = CvpClient() |
| 13 |
clnt.connect(nodes=['cvp1'], username="username",password="password") |
| 14 |
|
| 15 |
def main(): |
| 16 |
|
| 17 |
print('Retrieving configlets ...') |
| 18 |
|
| 19 |
inventory = clnt.api.get_inventory() |
| 20 |
data = clnt.api.get_configlets_and_mappers()['data'] |
| 21 |
print(data) |
| 22 |
|
| 23 |
print('Number of configlets:', len(data['configlets'])) |
| 24 |
|
| 25 |
searchAgain = True |
| 26 |
while searchAgain: |
| 27 |
try: |
| 28 |
search = input( "\nEnter Config Line: " ) |
| 29 |
print(f"\n\n\'{search}\' has been found in following configlets:\n\n") |
| 30 |
print(f"{'Hostname':<30}{'Serial number':<50}{'MAC address':<30}{'Configlets':<40}") |
| 31 |
print("=" * 150) |
| 32 |
for i in inventory: |
| 33 |
device = i['hostname'] |
| 34 |
device_sn = i['serialNumber'] |
| 35 |
device_mac = i['systemMacAddress'] |
| 36 |
configlet_list = [] |
| 37 |
for c in data['configlets']: |
| 38 |
for g in data['generatedConfigletMappers']: |
| 39 |
if device_mac == g['netElementId'] and c['key'] == g['configletBuilderId'] and search in c['config']: |
| 40 |
configlet_list.append(c['name']) |
| 41 |
for k in data['configletMappers']: |
| 42 |
if device_mac == k['objectId'] and c['key'] == k['configletId'] and search in c['config']: |
| 43 |
configlet_list.append(c['name']) |
| 44 |
configlet_list_final = ",".join(configlet_list) |
| 45 |
if len(configlet_list) > 0: |
| 46 |
print(f"{device:<30}{device_sn:<50}{device_mac:<30}{configlet_list_final:<30}") |
| 47 |
|
| 48 |
except KeyboardInterrupt: |
| 49 |
print('\nExiting... \n') |
| 50 |
return |
| 51 |
|
| 52 |
if __name__ == '__main__': |
| 53 |
main() |
| 54 |
|