def find_device_by_ip(): ip = request.forms.get('ip') device = None target_cluster = None cluster_id = None inventory_url = '' device360_url = '' for cluster in clusters: # if the device has been cached, return it if ip in cluster.api.keys(): device = cluster.api[ip] target_cluster = cluster cluster_id = get_cluster_id(cluster) break # otherwise, try finding it in the current cluster else: try: # an exception will be thrown if it's not in the cluster result = cluster.api[STUB_DEVICE].get_device_by_ip(ip) # otherwise, it does exist in the physical cluster; cache it in the cluster and return it device = NetworkDevice(cluster, result['managementIpAddress']) target_cluster = cluster cluster_id = get_cluster_id(cluster) break except DnacApiError: # device not found; try the next cluster continue # if the device was found, get the associated site info in case the user wants to cross-launch into inventory if bool(device): device_by_ip = device.get_device_by_ip(ip) details = device.get_device_detail_by_name(device_by_ip['hostname']) location = details['location'] if location in target_cluster.api.keys(): site = target_cluster.api[location] else: site = Site(target_cluster, location) inventory_url = '%s%s%s%s' % (HTTPS, cluster_id, INVENTORY_URI, site.id) device360_url = '%s%s%s%s' % (HTTPS, cluster_id, DEVICE360_URI, device.devices['id']) return template('results', target=ip, cluster=cluster_id, device=device, inventory_url=inventory_url, device360_url=device360_url, method='GET')
def run(self): MODULE = 'networkdevice_example.py' try: self.logger.info('%s: setting up Cisco DNA Center and its API...' % MODULE) dnac = Dnac() ndapi = NetworkDevice(dnac, 'devices') self.logger.info('%s: getting all devices...' % MODULE) # The handle 'ndapi' could be used here, but the point of this example is # demonstrate how to get it directly from the Dnac.api{} using the API's name. devices = dnac.api['devices'].get_all_devices() self.logger.info('%s: found the following devices:' % MODULE) for device in devices: self.logger.info('hostname: %s\tserial: %s\tIP: %s' % (device['hostname'], device['serialNumber'], device['managementIpAddress'])) return (True, devices) except Exception as err: return (False, MODULE + str(err))
print(' str(bauth) = ' + str(d.bauth)) print(' bauth.creds = ' + d.bauth.creds) print(' bauth.hdrs = ' + str(d.bauth.hdrs)) print(' xauth = ' + str(type(d.xauth))) print(' str(xauth) = ' + str(d.xauth)) print(' xauth.token = ' + d.xauth.token) print(' xauth.hdrs = ' + str(d.xauth.hdrs)) print(' api = ' + str(d.api)) print(' url = ' + d.url) print(' hdrs = ' + str(type(d.hdrs))) print(' hdrs = ' + str(d.hdrs)) print() print('Adding network-device API...') nd = NetworkDevice(d, 'network-device') d.api[nd.name] = nd print(' is in api = ' + str(nd.name in d.api)) print(' api["network-device"] = ' + str(d.api[nd.name])) print() print('Using network-device API...') print() resp = d.api[nd.name].get_device_by_name('DC1-A3850.cisco.com') print(' response = ') pp.pprint(resp) print()
from dnac import Dnac from dnac.networkdevice import NetworkDevice from dnac.template import Template, TARGET_BY_ID # connect to DNAC dnac = Dnac() # get the switch's UUID switch = NetworkDevice(dnac, 'leaf2.abc.inc') # load the template template = Template(dnac, 'Interface Description') # set the template's target template.target_id = switch.get_id_by_device_name(switch.name) template.target_type = TARGET_BY_ID # input the template parameters template.versioned_template_params['interface'] = '<switch_interface>' template.versioned_template_params['description'] = '<interface_description>' # push the template template.deploy_sync(3) print( 'Done pushing the template. Check your assigned switchport\'s configuration.' )
@archiver.route('/create_new_device_archive', method='POST') def create_new_device_archive(): host = request.forms.get('host') host_id = device_api.get_device_by_name(host)['id'] config_archive.add_new_archive(host_id) return template('create_new_device_archive', dnac=dnac, host=host) if __name__ == '__main__': dnac = Dnac(name='', version='1.3.0.3', ip='10.1.41.218', port='443', user='******', passwd='P@$$w0rd', content_type='application/json') timestamp = TimeStamp() device_api = NetworkDevice(dnac, 'deviceapi') if bool(dnac.name): settings = ConfigArchiveSettings(dnac, dnac.name) config_archive = ConfigArchive(dnac, dnac.name) config_archive.load_all_archives() elif bool(dnac.ip): settings = ConfigArchiveSettings(dnac, dnac.name) config_archive = ConfigArchive(dnac, dnac.name) config_archive.load_all_archives() else: print('Could not connect to DNA Center. Set the FQDN or IP in dnac_config.') exit(1) run(archiver, host='localhost', port=8080, reloader=True, debug=True)
from dnac import Dnac from dnac.networkdevice import NetworkDevice MODULE = 'networkdevice_example.py' print('%s: setting up Cisco DNA Center and its API...' % MODULE) dnac = Dnac() ndapi = NetworkDevice(dnac, 'devices') print('%s: getting all devices...' % MODULE) # The handle 'ndapi' could be used here, but the point of this example is # demonstrate how to get it directly from the Dnac.api{} using the API's name. devices = dnac.api['devices'].get_all_devices() print('%s: found the following devices:' % MODULE) for device in devices: print('hostname: %s\tserial: %s\tIP: %s' % (device['hostname'], device['serialNumber'], device['managementIpAddress'])) print()
if __name__ == '__main__': """ usage: device_finder <clusters_file>: finds a network device among multiple DNA Center clusters. """ # read the json encoded config file passed from the CLI clusters_file = open(sys.argv[1], mode='r') clusters_from_config_file = json.load(clusters_file) clusters_file.close() # create all the Dnac objects from the clusters that were listed in the config file clusters = [] for cluster in clusters_from_config_file: # create a Dnac object dnac = Dnac(version=cluster['version'], name=cluster['name'], ip=cluster['ip'], port=cluster['port'], user=cluster['user'], passwd=cluster['passwd'], content_type=cluster['content_type']) # create a stub site for adding new sites stub_site = Site(dnac, STUB_SITE) stub_site.timeout = 60 # my lab's DNAC server is responding slowly; others may not need this # create a stub network device for finding devices stub_device = NetworkDevice(dnac, STUB_DEVICE) # add the new Dnac instance to the global clusters list clusters.append(dnac) run(finder, host='localhost', port=8088, reloader=True, debug=True)