def nautobatfishbot(request): print(os.system('ls /source/nautobatfishbot/snapshots/lab/')) bf_session.host = "batfish" network_name = 'lab' print("set network name var") snapshot_path = '/source/nautobatfishbot/snapshots/lab/' print(snapshot_path) bf_set_network(network_name) bf_session.init_snapshot(snapshot_path, name=network_name, overwrite=True) load_questions() # pprint(bfq.parseWarning().answer().frame()) pprint(bfq.bgpSessionCompatibility().answer().frame()) print(bfq.ipOwners(duplicatesOnly=True).answer().frame()) all_output = '' port_tests = load_tests() results = test_port_flows(port_tests) for result in results: data = result.to_json() data = json.loads(data) all_output = all_output + pprint_reachability(result) print('\n\n\n\n\n\n') print(all_output) print('\n\n\n\n\n\n') pprint(HttpResponse("BUG")) # return HttpResponse(all_output) print('\n\n\n\n\n\n') # return HttpResponse("Working") return return_tests_data(requset, template_name='main/home.html', all_output)
def test_dataplane(isFailed, fromNode, checkMultipath=True): ips = bfq.ipOwners().answer().frame() loopbacks = ips[(ips['Interface'] == 'Loopback0') & (ips['Active'])] localIP = loopbacks[loopbacks['Node'] == fromNode]['IP'][0] leaves = set(loopbacks[loopbacks['Node'].str.contains('leaf')]['IP']) leaves.remove(localIP) spines = set(loopbacks[loopbacks['Node'].str.contains('spine')]['IP']) mpath = len(spines) logging.info("Progress: Analyzing traceroute from Leaf-3 to Leaf-1 and Leaf-2") # Set up the resulting data structure troute = dict() for leaf in leaves: troute[leaf] = dict() # Build headers for traceroute flows for leaf in leaves: troute[leaf]['header'] = header(srcIps=localIP,dstIps=leaf) # Ask questions about traceroute for leaf,data in troute.items(): troute[leaf]['trace'] = bfq.traceroute(startLocation=fromNode, headers=data['header']).answer() # Get first flow disposition for traces for leaf,data in troute.items(): troute[leaf]['result'] = data['trace'].get('answerElements')[0]['rows'][0]['Traces'][0]['disposition'] # Get traceroute paths to reach Leaf-1 and Leaf-2 for leaf,data in troute.items(): troute[leaf]['paths'] = data['trace'].get('answerElements')[0]['rows'][0]['Traces'] # Get traceroute hops to reach Leaf-1 and Leaf-2 for leaf,data in troute.items(): troute[leaf]['hops'] = data['trace'].get('answerElements')[0]['rows'][0]['Traces'][0].get('hops',[]) # Now let's check that the traceroute behaves as we expect for leaf, data in troute.items(): if data['result'] != 'ACCEPTED': logging.error("Traceroute to {} has failed: {}".format(leaf, data['result'])) isFailed = True else: logging.info("Traceroute Progress: {}".format(data['result'])) # Number of paths should be equal to the number of spines if len(data['paths']) != mpath: logging.error("Number of paths {} != {} number of spines".format(len(data['paths']), mpath)) logging.error(data['paths']) isFailed = True else: logging.info("Number of paths {} == {} number of spines".format(len(data['paths']), mpath)) # Traceroute has to traverse exactly two hops for path in data['paths']: if len(path.get('hops',[])) != 2: logging.error("Traceroute has not traversed exactly two hops") logging.error(path) isFailed = True else: logging.info("Traceroute traversed exactly two hops") return isFailed
def ping(self, source_ip, destination_ip): ip_owners = bfq.ipOwners().answer() traceroute = self._get_traceroute_status(source_ip, destination_ip, ip_owners) reverse_traceroute = self._get_traceroute_status( destination_ip, source_ip, ip_owners) if traceroute == True and reverse_traceroute == True: self._show_trace(source_ip, destination_ip, ip_owners) return 1 else: logger.warn('Ping {} -> {} failed'.format(source_ip, destination_ip)) return 0
print("Detect forwarding loops.") print("#"*100) print(bfq.detectLoops().answer().frame()) # List edges types print("#"*100) print("Lists neighbor relationships of the specified type (layer3, BGP, ospf, etc. in the form of edges).") print("#"*100) print(bfq.edges().answer().frame()) # File parse status print("#"*100) print("For each file in a snapshot, returns the host(s) that were produced by the file and the parse status: pass, fail, partially parsed.") print("#"*100) print(bfq.fileParseStatus().answer().frame()) # IP Interfaces print("#"*100) print("List IPs configured on interfaces") print("#"*100) ip_owners_ans = bfq.ipOwners().answer() print(ip_owners_ans.frame()) # Traceroute !! print("#"*100) print("Traceroute ! The killer feature :-p") print("#"*100) print((bfq.traceroute(startLocation='arista', headers={"dstIps": "ofLocation(n9k)"}).answer()).frame()) print((bfq.traceroute(startLocation='n9k', headers={"dstIps": "ofLocation(arista)"}).answer()).frame()) #print((bfq.traceroute(startLocation='arista', headers={"dstIps": "ofLocation(arista)"}).answer()).frame())
def razbor_ipowner(): vyvod = bfq.ipOwners().answer().frame() return vyvod
def test_answer_foreground(network): """Expect an answer that is valid JSON when run in foreground.""" bfq.ipOwners().answer()
def test_answer_background(network): """Expect a GUID when running in background, which can be fed to bf_get_work_status.""" work_item_id = bfq.ipOwners().answer(background=True) bf_get_work_status(work_item_id)
def analyse_network(report_dir): """ This function runs batfish questions and captures the query results into a spread sheet. :param report_dir: defines the directory in which the analysis report gets saved """ # Captures the status of the configurations that were Parsed. parse_status = bfq.fileParseStatus().answer().frame() # Batfish question to extract node properties print(Fore.YELLOW + " ==> GETTING NODE PROPERTIES") np = bfq.nodeProperties().answer().frame() # Batfish question to extract interface properties print(Fore.YELLOW + " ==> GETTING INTERFACE PROPERTIES") interface = bfq.interfaceProperties().answer().frame() # Batfish question to extract VLAN properties print(Fore.YELLOW + " ==> GETTING VLAN PROPERTIES") vlan_prop = bfq.switchedVlanProperties().answer().frame() # Batfish question to extract IP Owners print(Fore.YELLOW + " ==> GETTING IPOWNERS") ip_owners = bfq.ipOwners().answer().frame() # Batfish question to extract L3 edges print(Fore.YELLOW + " ==> GETTING L3 EDGES") l3edge = bfq.layer3Edges().answer().frame() # Batfish question to extract MPLAG properties print(Fore.YELLOW + " ==> GETTING MLAG PROPERTIES") mlag = bfq.mlagProperties().answer().frame() # Batfish question to extract OSPF configuration print(Fore.YELLOW + " ==> GETTING OSPF CONFIGURATION") ospf_config = bfq.ospfProcessConfiguration().answer().frame() # Batfish question to extract OSPF area configuration print(Fore.YELLOW + " ==> GETTING OSPF AREA CONFIGURATION") ospf_area_config = bfq.ospfAreaConfiguration().answer().frame() # Batfish question to extract OSPF interface configuration print(Fore.YELLOW + " ==> GETTING OSPF INTERFACE CONFIGURATION") ospf_interface = bfq.ospfInterfaceConfiguration().answer().frame() # Batfish question to extract OSPF Session compatability print(Fore.YELLOW + " ==> GETTING OSPF SESSION COMPATABILITY") ospf_session = bfq.ospfSessionCompatibility().answer().frame() # Batfish question to extract BGP configuration print(Fore.YELLOW + " ==> GETTING BGP CONFIGURATION") bgp_config = bfq.bgpProcessConfiguration().answer().frame() # Batfish question to extract BGP peer configuration print(Fore.YELLOW + " ==> GETTING BGP PEER CONFIGURATION") bgp_peer_config = bfq.bgpPeerConfiguration().answer().frame() # Batfish question to extract BGP session compatibility print(Fore.YELLOW + " ==> GETTING BGP SESSION COMPATIBILITY") bgp_session = bfq.bgpSessionStatus().answer().frame() # Batfish question to extract routing table print(Fore.YELLOW + " ==> GETTING ROUTE TABLE") routing = bfq.routes().answer().frame() # Batfish question to extract F5 VIP configuration print(Fore.YELLOW + " ==> GETTING F5 VIP CONFIGURATION") f5_vip = bfq.f5BigipVipConfiguration().answer().frame() # Batfish question to extract Named Structures print(Fore.YELLOW + " ==> GETTING NAMED STRUCTURES") named_structure = bfq.namedStructures().answer().frame() # Batfish question to extract Structure deginitions print(Fore.YELLOW + " ==> GETTING STRUCTURE DEFINITIONS") def_structure = bfq.definedStructures().answer().frame() # Batfish question to extract referenced structures print(Fore.YELLOW + " ==> GETTING REFERENCED STRUCTURES") ref_structure = bfq.referencedStructures().answer().frame() # Batfish question to extract undefined references print(Fore.YELLOW + " ==> GETTING UNDEFINED STRUCTURE REFERENCES") undefined_references = bfq.undefinedReferences().answer().frame() # Batfish question to extract used structures print(Fore.YELLOW + " ==> GETTING UNUSED STRUCTURES") unused_structure = bfq.unusedStructures().answer().frame() # Setting the path and file name were the analysis report will be saved analysis_report_file = report_dir + "/" + NETWORK_NAME + "_analysis_report.xlsx" print(Fore.YELLOW + " ==> GENERATING REPORT") # Writes previously computed configuration analysis into a excel file with pd.ExcelWriter(analysis_report_file) as f: parse_status.to_excel(f, sheet_name="parse_satus", engine="xlsxwriter") np.to_excel(f, sheet_name="node_properties", engine="xlsxwriter") interface.to_excel(f, sheet_name="interface_properties", engine="xlsxwriter") vlan_prop.to_excel(f, sheet_name="vlan_properties", engine="xlsxwriter") ip_owners.to_excel(f, sheet_name="IPOwners", engine="xlsxwriter") l3edge.to_excel(f, sheet_name="l3edges", engine="xlsxwriter") mlag.to_excel(f, sheet_name="mlag", engine="xlsxwriter") ospf_session.to_excel(f, sheet_name="ospf_session", engine="xlsxwriter") ospf_config.to_excel(f, sheet_name="ospf_config", engine="xlsxwriter") ospf_area_config.to_excel(f, sheet_name="ospf_area_config", engine="xlsxwriter") ospf_interface.to_excel(f, sheet_name="ospf_interface", engine="xlsxwriter") bgp_config.to_excel(f, sheet_name="bgp_config", engine="xlsxwriter") bgp_peer_config.to_excel(f, sheet_name="bgp_peer_config", engine="xlsxwriter") bgp_session.to_excel(f, sheet_name="bgp_session", engine="xlsxwriter") routing.to_excel(f, sheet_name="routing_table", engine="xlsxwriter") f5_vip.to_excel(f, sheet_name="f5_vip", engine="xlsxwriter") named_structure.to_excel(f, sheet_name="named_structure", engine="xlsxwriter") def_structure.to_excel(f, sheet_name="defined_structures", engine="xlsxwriter") ref_structure.to_excel(f, sheet_name="referrenced_structures", engine="xlsxwriter") undefined_references.to_excel(f, sheet_name="undefined_references", engine="xlsxwriter") unused_structure.to_excel(f, sheet_name="unused_structure", engine="xlsxwriter")
def get_interfaces(self): result = bfq.ipOwners().answer().frame() return result