def get_arptable(ver): """ Get ARP Tables from NetDB """ error = version_chk(ver, ['v1.0', 'v2']) if error: return error router = None hours = 1 if 'router' in request.args: router = request.args['router'] router = router.replace('*', '%') else: return jsonify(errors.json_error('InputError', 'router= is required')) if 'hours' in request.args: hours = int(request.args['hours']) try: return jsonify(upgrade_api(nglib.netdb.ip.arp(router=router, hours=hours), ver)) except ResultError as e: return jsonify(errors.json_error(e.expression, e.message))
def get_mactable(ver): """ Get the MAC Table from NetDB """ error = version_chk(ver, ['v1.0', 'v2']) if error: return error switch = None port = '%' hours = 1 if 'switch' in request.args: switch = request.args['switch'] switch = switch.replace('*', '%') else: return jsonify(errors.json_error('InputError', 'switch= is required')) if 'port' in request.args: port = request.args['port'] port = port.replace('*', '%') print("port", port) if 'hours' in request.args: hours = int(request.args['hours']) try: return jsonify(upgrade_api(nglib.netdb.switch.mac(switch=switch, port=port, hours=hours), ver)) except ResultError as e: return jsonify(errors.json_error(e.expression, e.message))
def get_mactable(ver): """ Get the MAC Table from NetDB """ error = version_chk(ver, ['v1.0', 'v2']) if error: return error switch = None port = '%' hours = 1 if 'switch' in request.args: switch = request.args['switch'] switch = switch.replace('*', '%') else: return jsonify(errors.json_error('InputError', 'switch= is required')) if 'port' in request.args: port = request.args['port'] port = port.replace('*', '%') print("port", port) if 'hours' in request.args: hours = int(request.args['hours']) try: return jsonify( upgrade_api( nglib.netdb.switch.mac(switch=switch, port=port, hours=hours), ver)) except ResultError as e: return jsonify(errors.json_error(e.expression, e.message))
def get_devs(ver): """ Get Device Reports Notes: Truncates by default for speed """ error = version_chk(ver) if error: return error search = '.*' group = '.*' trunc = True if 'search' in request.args: search = request.args['search'] if 'group' in request.args: group = request.args['group'] if 'full' in request.args: trunc = False try: return jsonify(upgrade_api(nglib.report.get_dev_report(dev=search, group=group, trunc=trunc), ver)) except ResultError as e: return jsonify(errors.json_error(e.expression, e.message))
def get_arptable(ver): """ Get ARP Tables from NetDB """ error = version_chk(ver, ['v1.0', 'v2']) if error: return error router = None hours = 1 if 'router' in request.args: router = request.args['router'] router = router.replace('*', '%') else: return jsonify(errors.json_error('InputError', 'router= is required')) if 'hours' in request.args: hours = int(request.args['hours']) try: return jsonify( upgrade_api(nglib.netdb.ip.arp(router=router, hours=hours), ver)) except ResultError as e: return jsonify(errors.json_error(e.expression, e.message))
def get_device_neighbors(ver, device): """ Get specific device neighbors """ error = version_chk(ver) if error: return error try: return jsonify(upgrade_api(nglib.query.dev.get_neighbors(device), ver)) except ResultError as e: return jsonify(errors.json_error(e.expression, e.message))
def get_nets(ver): """ /nets API method Options: none - return a list of all networks ip - find the cidr for an IP cidr - filter networks by CIDR filter - filter networks by NetGrph filter (vrf:[role]) """ error = version_chk(ver) if error: return error cidr = None nFilter = 'all' if 'ip' in request.args: try: return jsonify(upgrade_api(nglib.query.net.get_net(request.args['ip'], rtype="NGTREE"), ver)) except ResultError as e: return jsonify(errors.json_error(e.expression, e.message)) elif 'cidr' in request.args: cidr = request.args['cidr'] cidr = cidr.replace('-', '/') try: return jsonify(upgrade_api(nglib.query.net.get_networks_on_cidr(cidr, rtype="NGTREE"), ver)) except ResultError as e: return jsonify(errors.json_error(e.expression, e.message)) except ValueError as e: print(dir(e)) return jsonify(errors.json_error('ValueError', str(e), code=400)) else: if 'filter' in request.args: nFilter = request.args['filter'] try: return jsonify(upgrade_api(nglib.query.net.get_networks_on_filter(nFilter=nFilter, rtype="NGTREE"), ver)) except ResultError as e: return jsonify(errors.json_error(e.expression, e.message))
def get_device(device, ver): """ Get specific device reports """ error = version_chk(ver) if error: return error try: res = nglib.query.dev.get_device(device, rtype="NGTREE") return jsonify(upgrade_api(nglib.query.dev.get_device(device, rtype="NGTREE"), ver)) except ResultError as e: return jsonify(errors.json_error(e.expression, e.message))
def get_vlan(vlan, ver): 'Get individual vlan' allSwitches = True error = version_chk(ver) if error: return error if 'allSwitches' in request.args and request.args['allSwitches'] == 'False': allSwitches = False try: return jsonify(upgrade_api(nglib.query.vlan.get_vlan(vlan, allSwitches=allSwitches, \ rtype="NGTREE"), ver)) except ResultError as e: return jsonify(errors.json_error(e.expression, e.message))
def get_device_ints(ver, device): """ Get specific device interfaces """ error = version_chk(ver) if error: return error if not nglib.netdb: return jsonify(errors.json_error('NetDB Error', \ 'NetDB Must be enabled to run this query')) try: return jsonify(upgrade_api(nglib.netdb.switch.get_switch(device), ver)) except ResultError as e: return jsonify(errors.json_error(e.expression, e.message))
def get_mac_count(ver, switch): """ Get the MAC Table count from NetDB """ error = version_chk(ver, ['v1.0', 'v2']) if error: return error hours = 1 switch = switch.replace('*', '%') if 'hours' in request.args: hours = int(request.args['hours']) try: return jsonify(upgrade_api(nglib.netdb.switch.count(switch=switch, hours=hours), ver)) except ResultError as e: return jsonify(errors.json_error(e.expression, e.message))
def get_switched_path(ver): """ Switched Path """ error = version_chk(ver, ['v1.0', 'v1.1', 'v2']) if error: return error onepath = False depth = '20' if 'onepath' in request.args: if request.args['onepath'] == "True": onepath = True if 'depth' in request.args: depth = request.args['depth'] try: return jsonify(upgrade_api(nglib.query.path.get_switched_path(request.args['src'], \ request.args['dst'], {"onepath": onepath, "depth": depth}), ver)) except ResultError as e: return jsonify(errors.json_error(e.expression, e.message))
def get_mac_count(ver, switch): """ Get the MAC Table count from NetDB """ error = version_chk(ver, ['v1.0', 'v2']) if error: return error hours = 1 switch = switch.replace('*', '%') if 'hours' in request.args: hours = int(request.args['hours']) try: return jsonify( upgrade_api(nglib.netdb.switch.count(switch=switch, hours=hours), ver)) except ResultError as e: return jsonify(errors.json_error(e.expression, e.message))
def get_vlans(ver): 'L2 VLANs' error = version_chk(ver, ['v1.0', 'v1.1', 'v2']) if error: return error allSwitches = True vrange = '1-4096' group = '.*' if 'allSwitches' in request.args and request.args['allSwitches'] == 'False': allSwitches = False if 'vrange' in request.args: vrange = request.args['vrange'] if 'group' in request.args: group = request.args['group'] try: return jsonify(upgrade_api(nglib.report.get_vlan_report(vrange=vrange, group=group, \ rtype="NGTREE"), ver)) except ResultError as e: return jsonify(errors.json_error(e.expression, e.message))
def get_routed_path(ver): """ Routed Paths, accepts vrf """ onepath = False depth = '20' vrf = 'default' error = version_chk(ver, ['v1.0', 'v1.1', 'v2']) if error: return error if 'onepath' in request.args: if request.args['onepath'] == "True": onepath = True if 'depth' in request.args: depth = request.args['depth'] if 'vrf' in request.args: vrf = request.args['vrf'] try: return jsonify(upgrade_api(nglib.query.path.get_routed_path(request.args['src'], \ request.args['dst'], {"onepath": onepath, "depth": depth, \ "VRF": vrf}), ver)) except ResultError as e: return jsonify(errors.json_error(e.expression, e.message))