def troubleshoot(deviceip): token = get_auth_token() print("Reachability test for device:%s" % deviceip) url = create_url(path="reachability-info/ip-address/%s" % deviceip) print("Getting %s" % url) headers = {"x-auth-token": token["token"]} try: response = requests.get(url, headers=headers, verify=False) except requests.exceptions.RequestException as cerror: print("Error processing request", cerror) sys.exit(1) print(json.dumps(response.json(), indent=2)) dev = response.json()["response"] print("Status: {status}".format(status=dev["reachabilityStatus"])) if "reachabilityFailureReason" in dev: print(dev["reachabilityFailureReason"]) url = create_url(path="network-device/management-info") print("Getting %s" % url) headers = {"x-auth-token": token["token"]} try: response = requests.get(url, headers=headers, verify=False) except requests.exceptions.RequestException as cerror: print("Error processing request", cerror) sys.exit(1) print(json.dumps(response.json(), indent=2)) for cred in response.json()["response"]: if cred["managementIpAddress"] == deviceip: print(json.dumps(cred, indent=2))
def path_trace(): ''' :return: the result of path trace ''' url = create_url(path="flow-analysis") print url data = {"sourceIP" : "65.1.1.46", "destIP" : "212.1.10.20"} token = get_auth_token() headers = {'X-auth-token' : token['token'], 'Content-Type' : 'application/json'} try: response = requests.post(url, data=json.dumps(data), headers=headers, verify=False) except requests.exceptions.RequestException as cerror: print "Error processing request", cerror sys.exit(1) taskid = response.json()['response']['taskId'] print "Waiting for Task %s" % taskid task_result = wait_on_task(taskid, token) flow_id = task_result['progress'] url = create_url(path="flow-analysis/%s" % flow_id) try: response = requests.get(url, headers=headers, verify=False) except requests.exceptions.RequestException as cerror: print "Error processing request", cerror sys.exit(1) return response.json()
def path_trace(): ''' :return: the result of path trace ''' url = create_url(path="flow-analysis") print url data = {"sourceIP": "65.1.1.46", "destIP": "212.1.10.20"} token = get_auth_token() headers = { 'X-auth-token': token['token'], 'Content-Type': 'application/json' } try: response = requests.post(url, data=json.dumps(data), headers=headers, verify=False) except requests.exceptions.RequestException as cerror: print "Error processing request", cerror sys.exit(1) taskid = response.json()['response']['taskId'] print "Waiting for Task %s" % taskid task_result = wait_on_task(taskid, token) flow_id = task_result['progress'] url = create_url(path="flow-analysis/%s" % flow_id) try: response = requests.get(url, headers=headers, verify=False) except requests.exceptions.RequestException as cerror: print "Error processing request", cerror sys.exit(1) return response.json()
def troubleshoot(deviceip): token = get_auth_token() print('Reachability test for device:%s' % deviceip) url = create_url(path="reachability-info/ip-address/%s" % deviceip) print("Getting %s" % url) headers = {'x-auth-token': token['token']} try: response = requests.get(url, headers=headers, verify=False) except requests.exceptions.RequestException as cerror: print("Error processing request", cerror) sys.exit(1) print(json.dumps(response.json(), indent=2)) dev = response.json()['response'] print('Status: {status}'.format(status=dev['reachabilityStatus'])) if 'reachabilityFailureReason' in dev: print(dev['reachabilityFailureReason']) url = create_url(path="network-device/management-info") print("Getting %s" % url) headers = {'x-auth-token': token['token']} try: response = requests.get(url, headers=headers, verify=False) except requests.exceptions.RequestException as cerror: print("Error processing request", cerror) sys.exit(1) print(json.dumps(response.json(), indent=2)) for cred in response.json()['response']: if cred['managementIpAddress'] == deviceip: print(json.dumps(cred, indent=2))
def project_name_to_id(project_name): token = get_auth_token() url = create_url(path="pnp-project") headers= { 'x-auth-token': token['token'], 'content-type' : 'application/json'} # look for project by name. need to get the project id search_url = url + '?siteName=%s&offset=1&limit=10' %project_name print("GET: %s" % search_url) try: response = requests.get(search_url, headers=headers, verify=False) except requests.exceptions.RequestException as cerror: print("Error processing request", cerror) sys.exit(1) # check response.json() for values # no match # multi match # single match matches = response.json()['response'] if len(matches) <1: raise ValueError('No matches found for %s' % project_name) elif len(matches) > 1: raise ValueError("multiple matches found for %s" % project_name) else: project_id = matches[0]['id'] return project_id
def main(apic, user, password, policyid): token = get_auth_token(controller_ip=apic, username=user, password=password) url = create_url(path="policy/%s" % policyid, controller_ip=apic) print "GET %s" % url headers= { 'x-auth-token': token['token']} try: response = requests.get(url, headers=headers, verify=False) except requests.exceptions.RequestException as cerror: print "Error processing request", cerror sys.exit(1) policy = response.json()['response'] print "Deleting policy %s to %s" %(policy['policyName'], policy['state']) headers['Content-Type'] = 'application/json' try: response = requests.delete(url, headers=headers, verify=False) print json.dumps(response.json()['response']) taskid = response.json()['response']['taskId'] task = wait_on_task(taskid, token) print json.dumps(task, indent=2) except requests.exceptions.RequestException as cerror: print "Error processing request", cerror sys.exit(1)
def main(apic, user, password, policyid): token = get_auth_token(controller_ip=apic, username=user, password=password) url = create_url(path="policy", controller_ip=apic) print "GET %s" % url headers= { 'x-auth-token': token['token']} try: response = requests.get(url+"/%s" % policyid, headers=headers, verify=False) except requests.exceptions.RequestException as cerror: print "Error processing request", cerror sys.exit(1) policy = response.json()['response'] print json.dumps(policy, indent=2) if policy['state'] == "Active": policy['state'] = 'Inactive' else: policy['state'] = "Active" print "Setting policy %s to %s" %(policy['policyName'], policy['state']) headers['Content-Type'] = 'application/json' policylist = [policy] try: response = requests.put(url, headers=headers, data=json.dumps(policylist), verify=False) print json.dumps(response.json()['response']) taskid = response.json()['response']['taskId'] wait_on_task(taskid, token) except requests.exceptions.RequestException as cerror: print "Error processing request", cerror sys.exit(1)
def main(apic, user, password, policyscope): token = get_auth_token(controller_ip=apic, username=user, password=password) url = create_url(path="policy?policyScope=%s" % policyscope, controller_ip=apic) print "GET %s" % url headers = {'x-auth-token': token['token']} try: response = requests.get(url, headers=headers, verify=False) except requests.exceptions.RequestException as cerror: print "Error processing request", cerror sys.exit(1) for policy in response.json()['response']: print policy['policyName'] # look for the business relevant apps if '-BR' in policy['policyName']: brapps = policy['resource']['applications'] print "Business Relevant app count: %d" % len(brapps) # look for the current default apps elif '-D' in policy['policyName']: defapps = policy['resource']['applications'] print "Default app count: %d" % len(defapps) apps = brapps + defapps print "Total Default app count: %d" % len(apps) print
def main(apic, user, password, policyscope): token = get_auth_token(controller_ip=apic, username=user, password=password) url = create_url(path="policy?policyScope=%s" % policyscope, controller_ip=apic) print "GET %s" % url headers= { 'x-auth-token': token['token']} try: response = requests.get(url, headers=headers, verify=False) except requests.exceptions.RequestException as cerror: print "Error processing request", cerror sys.exit(1) for policy in response.json()['response']: print policy['policyName'] # look for the business relevant apps if '-BR' in policy['policyName']: brapps = policy['resource']['applications'] print "Business Relevant app count: %d" % len(brapps) # look for the current default apps elif '-D' in policy['policyName']: defapps = policy['resource']['applications'] print "Default app count: %d" % len(defapps) apps = brapps + defapps print "Total Default app count: %d" % len(apps) print
def delete_project(project_name): token = get_auth_token() url = create_url(path="pnp-project") headers = { 'x-auth-token': token['token'], 'content-type': 'application/json' } try: project_id = project_name_to_id(project_name) except ValueError as v: print("NO project: %s" % project_name) return {} delete_url = url + '/%s?deleteRule=1&deleteDevice=1' % project_id print("DELETE: %s" % delete_url) try: response = requests.delete(delete_url, headers=headers, verify=False) response.raise_for_status() except requests.exceptions.RequestException as cerror: print("Error processing request", cerror) sys.exit(1) taskid = response.json()['response']['taskId'] print("Waiting for Task %s" % taskid) task_result = wait_on_task(taskid, token) return task_result
def delete_project(project_name): token = get_auth_token() url = create_url(path="pnp-project") headers= { 'x-auth-token': token['token'], 'content-type' : 'application/json'} try: project_id = project_name_to_id(project_name) except ValueError as v: print("NO project: %s" % project_name) return {} delete_url = url + '/%s?deleteRule=1&deleteDevice=1' % project_id print("DELETE: %s" % delete_url) try: response = requests.delete(delete_url, headers=headers, verify=False) response.raise_for_status() except requests.exceptions.RequestException as cerror: print("Error processing request", cerror) sys.exit(1) taskid = response.json()['response']['taskId'] print("Waiting for Task %s" % taskid) task_result = wait_on_task(taskid, token) return task_result
def project_name_to_id(project_name): token = get_auth_token() url = create_url(path="pnp-project") headers = { 'x-auth-token': token['token'], 'content-type': 'application/json' } # look for project by name. need to get the project id search_url = url + '?siteName=%s&offset=1&limit=10' % project_name print("GET: %s" % search_url) try: response = requests.get(search_url, headers=headers, verify=False) except requests.exceptions.RequestException as cerror: print("Error processing request", cerror) sys.exit(1) # check response.json() for values # no match # multi match # single match matches = response.json()['response'] if len(matches) < 1: raise ValueError('No matches found for %s' % project_name) elif len(matches) > 1: raise ValueError("multiple matches found for %s" % project_name) else: project_id = matches[0]['id'] return project_id
def create_project_rule(project_name, serial, platform, host, config_file_id=None): token = get_auth_token() url = create_url(path="pnp-project") headers= { 'x-auth-token': token['token'], 'content-type' : 'application/json'} project_id = project_name_to_id(project_name) post_url = url + '/%s/device' % project_id print("POST URL %s" % post_url) body = [{ "serialNumber": serial, "platformId": platform, "hostName": host, "pkiEnabled": True }] if config_file_id is not None: body[0]['configId'] = config_file_id print(json.dumps(body, indent=2)) try: response = requests.post(post_url, headers=headers, data=json.dumps(body), verify=False) response.raise_for_status() except requests.exceptions.RequestException as cerror: print("Error processing request", cerror) if response.json(): print(json.dumps(response.json(), indent=2)) sys.exit(1) taskid = response.json()['response']['taskId'] print("Waiting for Task %s" % taskid) task_result = wait_on_task(taskid, token) return task_result
def main(apic, user, password, policyid): token = get_auth_token(controller_ip=apic, username=user, password=password) url = create_url(path="policy/%s" % policyid, controller_ip=apic) print "GET %s" % url headers = {'x-auth-token': token['token']} try: response = requests.get(url, headers=headers, verify=False) except requests.exceptions.RequestException as cerror: print "Error processing request", cerror sys.exit(1) policy = response.json()['response'] print "Deleting policy %s to %s" % (policy['policyName'], policy['state']) headers['Content-Type'] = 'application/json' try: response = requests.delete(url, headers=headers, verify=False) print json.dumps(response.json()['response']) taskid = response.json()['response']['taskId'] task = wait_on_task(taskid, token) print json.dumps(task, indent=2) except requests.exceptions.RequestException as cerror: print "Error processing request", cerror sys.exit(1)
def get_app_from_name(token, app, apic): url = create_url(path="application?name=%s" % app, controller_ip=apic) print "GET %s" % url headers= { 'x-auth-token': token['token']} try: response = requests.get(url, headers=headers, verify=False) response.raise_for_status() except requests.exceptions.RequestException as cerror: print "Error processing request", cerror return response.json()['response'][0]['id']
def get_interfaces(): url = create_url(path="interface") token = get_auth_token() headers = {'X-auth-token': token['token']} try: response = requests.get(url, headers=headers, verify=False) except requests.exceptions.RequestException as cerror: print("Error processing request", cerror) sys.exit(1) return response.json()
def get_app_from_name(token, app, apic): url = create_url(path="application?name=%s" % app, controller_ip=apic) print "GET %s" % url headers = {'x-auth-token': token['token']} try: response = requests.get(url, headers=headers, verify=False) response.raise_for_status() except requests.exceptions.RequestException as cerror: print "Error processing request", cerror return response.json()['response'][0]['id']
def list_network_devices(): url = create_url(path="network-device") print url token = get_auth_token() headers = {'X-auth-token': token['token']} try: response = requests.get(url, headers=headers, verify=False) except requests.exceptions.RequestException as cerror: print "Error processing request", cerror sys.exit(1) return response.json()
def get(path): url = create_url(path=path) print url token = get_auth_token() headers = {'X-auth-token' : token['token']} try: response = requests.get(url, headers=headers, verify=False) except requests.exceptions.RequestException as cerror: print "Error processing request", cerror sys.exit(1) return response.json()
def list_projects(): token = get_auth_token() url = create_url(path="pnp-project") print("Getting %s" % url) headers= { 'x-auth-token': token['token']} try: response = requests.get(url, headers=headers, verify=False) except requests.exceptions.RequestException as cerror: print("Error processing request", cerror) sys.exit(1) return response.json()
def list_files(namespace): token = get_auth_token() url = create_url(path="file/namespace/%s" % namespace) print("Getting %s" % url) headers = {'x-auth-token': token['token']} try: response = requests.get(url, headers=headers, verify=False) except requests.exceptions.RequestException as cerror: print("Error processing request", cerror) sys.exit(1) return response.json()
def list_project_detail(projectname): token = get_auth_token() project_id = project_name_to_id(projectname) url = create_url(path="pnp-project/%s/device" % project_id) print("Getting %s" % url) headers = {'x-auth-token': token['token']} try: response = requests.get(url, headers=headers, verify=False) except requests.exceptions.RequestException as cerror: print("Error processing request", cerror) sys.exit(1) return response.json()
def delete_file(namespace, fileid): token = get_auth_token() url = create_url(path="file/%s" % fileid) print("DELETE %s" % url) headers= { 'x-auth-token': token['token']} try: response = requests.delete(url, headers=headers, verify=False) except requests.exceptions.RequestException as cerror: print("Error processing request", cerror) sys.exit(1) return response.json()
def get_url(url): if FAKE: return fake[url] url = create_url(path=url) print(url) token = get_auth_token() headers = {'X-auth-token' : token['token']} try: response = requests.get(url, headers=headers, verify=False) except requests.exceptions.RequestException as cerror: print("Error processing request", cerror) sys.exit(1) return response.json()
def post_and_wait(url, data): if FAKE: return fake_post[url] token = get_auth_token() url = create_url(path=url) headers= { 'x-auth-token': token['token'], 'content-type' : 'application/json'} try: response = requests.post(url, headers=headers, data=json.dumps(data), verify=False) except requests.exceptions.RequestException as cerror: print ("Error processing request", cerror) sys.exit(1) taskid = response.json()['response']['taskId'] print ("Waiting for Task %s" % taskid) task_result = wait_on_task(taskid, token) return task_result
def create_project_rule(project_name, serial, platform, host, config_file_id=None): token = get_auth_token() url = create_url(path="pnp-project") headers = { 'x-auth-token': token['token'], 'content-type': 'application/json' } project_id = project_name_to_id(project_name) post_url = url + '/%s/device' % project_id print("POST URL %s" % post_url) body = [{ "serialNumber": serial, "platformId": platform, "hostName": host, "pkiEnabled": True }] if config_file_id is not None: body[0]['configId'] = config_file_id print(json.dumps(body, indent=2)) try: response = requests.post(post_url, headers=headers, data=json.dumps(body), verify=False) response.raise_for_status() except requests.exceptions.RequestException as cerror: print("Error processing request", cerror) if response.json(): print(json.dumps(response.json(), indent=2)) sys.exit(1) taskid = response.json()['response']['taskId'] print("Waiting for Task %s" % taskid) task_result = wait_on_task(taskid, token) return task_result
def create_project(project_name): token = get_auth_token() url = create_url(path="pnp-project") print ("POST URL %s" % url) print ("Creating project %s" % project_name) headers= { 'x-auth-token': token['token'], 'content-type' : 'application/json'} body = [{"siteName": project_name}] try: response = requests.post(url, headers=headers, data=json.dumps(body), verify=False) except requests.exceptions.RequestException as cerror: print ("Error processing request", cerror) sys.exit(1) taskid = response.json()['response']['taskId'] print ("Waiting for Task %s" % taskid) task_result = wait_on_task(taskid, token) return task_result
def main(apic, user, password): token = get_auth_token() url = create_url(path="policy/tag/association", controller_ip=apic) print "GET %s" % url headers = {'x-auth-token': token['token']} try: response = requests.get(url, headers=headers, verify=False) except requests.exceptions.RequestException as cerror: print "Error processing request", cerror sys.exit(1) for association in response.json()['response']: print "Tag:", association['policyTag'] print "Devices:", for network_device in association['networkDevices']: print ",", network_device['deviceName'], print print
def main(apic, user, password): token = get_auth_token() url = create_url(path="policy/tag/association", controller_ip=apic) print "GET %s" % url headers= { 'x-auth-token': token['token']} try: response = requests.get(url, headers=headers, verify=False) except requests.exceptions.RequestException as cerror: print "Error processing request", cerror sys.exit(1) for association in response.json()['response']: print "Tag:", association['policyTag'] print "Devices:", for network_device in association['networkDevices']: print ",", network_device['deviceName'], print print
def main(apic, user, password): token = get_auth_token() url = create_url(path="policy", controller_ip=apic) print "GET %s" % url headers= { 'x-auth-token': token['token']} try: response = requests.get(url, headers=headers, verify=False) except requests.exceptions.RequestException as cerror: print "Error processing request", cerror sys.exit(1) for policy in response.json()['response']: count =0 if 'applications' in policy['resource']: count = len(policy['resource']['applications']) elif 'categories' in policy['resource']: count = len(policy['resource']['categories']) print policy['policyName'], policy['state'], policy['policyScope'], policy['id'], "(", count ,")", \ json.dumps(policy['actionProperty'], indent=2), \ json.dumps(policy['actions'], indent=2)
def create_policy(token, apic, policyscope, appname, appid, relevance): url = create_url(path="policy", controller_ip=apic) policy = [{ "policyName" : "adam-test", "policyScope" : policyscope, "policyPriority": 4095, "actionProperty" : { "pathControlFlag": False, "pathPreferenceFlag": False}, "resource" : {"applications" : [ {"appName": appname, "id" : appid} ]}, "actionProperty": { "relevanceLevel": relevance }, "actions" : ["SET_PROPERTY"] }] headers= { 'x-auth-token': token['token'], 'content-type' : 'application/json'} print "POST", url, json.dumps(policy, indent=2) response = requests.post(url, headers=headers, data=json.dumps(policy), verify=False) response.raise_for_status() task_id = response.json()['response']['taskId'] task = wait_on_task(task_id, token) print json.dumps(task, indent=2)
def upload_file(namespace, filepath): token = get_auth_token() try: f = open(filepath, "r") files = {'fileUpload': f} except: print("Could not open file %s" % filepath) sys.exit(1) url = create_url(path="file/%s" % namespace) print("POST %s" % url) headers = {'x-auth-token': token['token']} try: response = requests.post(url, files=files, headers=headers, verify=False) except requests.exceptions.RequestException as cerror: print("Error processing request", cerror) sys.exit(1) return response.json()
def main(apic, user, password, policyid): token = get_auth_token(controller_ip=apic, username=user, password=password) url = create_url(path="policy", controller_ip=apic) print "GET %s" % url headers = {'x-auth-token': token['token']} try: response = requests.get(url + "/%s" % policyid, headers=headers, verify=False) except requests.exceptions.RequestException as cerror: print "Error processing request", cerror sys.exit(1) policy = response.json()['response'] print json.dumps(policy, indent=2) if policy['state'] == "Active": policy['state'] = 'Inactive' else: policy['state'] = "Active" print "Setting policy %s to %s" % (policy['policyName'], policy['state']) headers['Content-Type'] = 'application/json' policylist = [policy] try: response = requests.put(url, headers=headers, data=json.dumps(policylist), verify=False) print json.dumps(response.json()['response']) taskid = response.json()['response']['taskId'] wait_on_task(taskid, token) except requests.exceptions.RequestException as cerror: print "Error processing request", cerror sys.exit(1)
def create_policy(token, apic, policyscope, appname, appid, relevance): url = create_url(path="policy", controller_ip=apic) policy = [{ "policyName": "adam-test", "policyScope": policyscope, "policyPriority": 4095, "actionProperty": { "pathControlFlag": False, "pathPreferenceFlag": False }, "resource": { "applications": [{ "appName": appname, "id": appid }] }, "actionProperty": { "relevanceLevel": relevance }, "actions": ["SET_PROPERTY"] }] headers = { 'x-auth-token': token['token'], 'content-type': 'application/json' } print "POST", url, json.dumps(policy, indent=2) response = requests.post(url, headers=headers, data=json.dumps(policy), verify=False) response.raise_for_status() task_id = response.json()['response']['taskId'] task = wait_on_task(task_id, token) print json.dumps(task, indent=2)
def post_and_wait(url, data): if FAKE: return fake_post[url] token = get_auth_token() url = create_url(path=url) headers = { 'x-auth-token': token['token'], 'content-type': 'application/json' } try: response = requests.post(url, headers=headers, data=json.dumps(data), verify=False) except requests.exceptions.RequestException as cerror: print("Error processing request", cerror) sys.exit(1) taskid = response.json()['response']['taskId'] print("Waiting for Task %s" % taskid) task_result = wait_on_task(taskid, token) return task_result