")
def show_bgpmap(data):
data = json.loads(unquote(data))
graph = pydot.Dot('BGPMAP', graph_type='digraph')
nodes = {}
edges = {}
for host, asmaps in data.iteritems():
nodes[host] = pydot.Node(host, shape="box", style="filled", fillcolor="#F5A9A9")
graph.add_node(nodes[host])
for host, asmaps in data.iteritems():
first = True
for asmap in asmaps:
previous_as = host
for _as in asmap:
_as = get_as_name(_as)
if _as == previous_as:
continue
if not nodes.has_key(_as):
nodes[_as] = pydot.Node(_as, style="filled", fillcolor=(first and "#F5A9A9" or "white"))
graph.add_node(nodes[_as])
edge_tuple = (nodes[previous_as], nodes[_as])
if not edges.has_key(edge_tuple):
edge = pydot.Edge(*edge_tuple)
graph.add_edge(edge)
edges[edge_tuple] = edge
if edge.get_color() != "red" and first:
edge.set_color("red")
previous_as = _as
first = False
#return Response("" + graph.create_dot() + "
")
return Response(graph.create_png(), mimetype='image/png')
def show_route(request_type, hosts, proto):
expression = unquote(request.args.get('q', ''))
if not expression.strip(): abort(400)
set_session(request_type, hosts, proto, expression)
all = (request_type.endswith("detail") and " all" or "" )
if request_type.startswith("adv"):
command = "show route " + expression
elif request_type.startswith("where"):
command = "show route where net ~ [ " + expression + " ]" + all
else:
mask = ""
if len(expression.split("/")) > 1:
expression, mask = (expression.split("/"))
if not mask and proto == "ipv4" : mask = "32"
if not mask and proto == "ipv6" : mask = "128"
if not mask_is_valid(mask):
return error_page("mask %s is invalid" % mask)
if proto == "ipv6" and not ipv6_is_valid(expression):
try: expression = resolve(expression, "AAAA")
except: return error_page("%s is unresolvable or invalid for %s" % (expression, proto))
if proto == "ipv4" and not ipv4_is_valid(expression):
try: expression = resolve(expression, "A")
except: return error_page("%s is unresolvable or invalid for %s" % (expression, proto))
if mask: expression += "/" + mask
command = "show route for " + expression + all
detail = {}
error = []
bgpmap = {}
for host in hosts.split("+"):
ret, res = bird_command(host, proto, command)
res = res.split("\n")
if len(res) > 1 : #if ret:
detail[host] = add_links(res)
bgpmap[host] = extract_paths(res)
else:
error.append("%s: bird command failed with error, %s" % (host,"\n".join(res)))
return render_template('route.html', detail=detail, command=command, expression=expression, bgpmap=json.dumps(bgpmap), error="
".join(error) )
app.secret_key = app.config["SESSION_KEY"]
app.debug = True
if __name__ == "__main__":
app.run("0.0.0.0")