diff --git a/lg.py b/lg.py index 0c8a30d..0b0eca2 100755 --- a/lg.py +++ b/lg.py @@ -1,8 +1,8 @@ #!/usr/bin/python -import sys +import sys, os, subprocess, re from dns import resolver,reversename -from flask import Flask, render_template, jsonify +from flask import Flask, render_template, jsonify, redirect app = Flask(__name__) import socket @@ -33,17 +33,47 @@ def check_ipv6(n): except socket.error: return False + +def cleanup_output(text): + return "\n".join([ add_link(re.sub(r'^[0-9]*-', r' ', line)) for line in text.split("\n") if not line.startswith("0000") ]) + +def add_link(text): + if text.strip().startswith("BGP.as_path:") or \ + text.strip().startswith("Neighbor AS:") : + return re.sub(r'([0-9]*)',r'\1',text) + else: + return text + +#@app.errorhandler(404) +#def notfound(error): +# return redirect("/") + @app.route("/") def hello(): return render_template('index.html') -@app.route("///prefix/") -@app.route("///prefix/") -@app.route("///prefix//") -@app.route("///prefix/") -@app.route("///prefix/") -@app.route("///prefix//") -def prefix(host, proto, prefix="", mask="", all=False): +@app.route("/whois/") +def whois(asnum): + output = "

Whois as" + asnum + "

"
+	try:
+		asnum = int(asnum)
+	except:
+		output += "Failed to parse as%s"%asnum
+	else:
+		output += subprocess.Popen(['whois', 'as%d'%asnum], stdout=subprocess.PIPE).communicate()[0].decode('utf-8', 'ignore')
+	output += "
" + return render_template('index.html', output=output, typ="whois", asnum=asnum) + +@app.route("/prefix_detail///") +@app.route("/prefix_detail///") +@app.route("/prefix_detail////") +def show_route_for_prefix_detail(host, proto, prefix="", mask=""): + return show_route_for_prefix(host, proto=proto, prefix=prefix, mask=mask, all = True) + +@app.route("/prefix///") +@app.route("/prefix///") +@app.route("/prefix////") +def show_route_for_prefix(host, proto, prefix="", mask="", all=False): qprefix = prefix @@ -73,13 +103,13 @@ def prefix(host, proto, prefix="", mask="", all=False): allowed = False output = '

' + host + ' (' + proto + ') show route for ' + prefix + (prefix != qprefix and " (%s)"%qprefix or "") + (mask and '/' + mask or '' ) + (all and " all" or "") + '

' + if allowed: if mask: qprefix = qprefix +"/"+mask if mask: prefix = prefix +"/"+mask - ok, string = get_cmd_result(host , proto, "show route for " + qprefix + (all and "all" or "")) + ok, string = get_cmd_result(host , proto, "show route for " + qprefix + (all and " all" or "")) if ok: - string = "\n".join([ s.replace("1007-"," ") for s in string.split("\n") if not s.startswith("0000") ]) - output +='
' + string + '
' + output += '
' + cleanup_output(string) + '
' else: output += string else: @@ -92,26 +122,26 @@ def prefix(host, proto, prefix="", mask="", all=False): return render_template('index.html', output=output, typ="prefix" + (all and "_detail" or ""), host=host+"/"+proto, prefix=prefix) -@app.route("///detail/") -@app.route("///detail/") +@app.route("/detail///") +@app.route("/detail///") def detail(host, proto, name=""): output = '

' + host + ' (' + proto + ') show protocols all ' + name + '

' - if name: + if not name: + output += "name missing" + else: ok, string = get_cmd_result(host , proto, "show protocols all " + name) if ok: - string = "\n".join([ s.strip() for s in string.split("\n") if s.startswith(" ")]) - output +='
' + string + '
' + output += '
'
+			output += "\n".join([ add_link(s.strip()) for s in string.split("\n") if s.startswith(" ")])
+			output += '
' else: output += string - else: - output += "name missing" return render_template('index.html', output=output, typ="detail", host=host+"/"+proto, name=name) -@app.route("//") -@app.route("///") -@app.route("///summary") +@app.route("/summary/") +@app.route("/summary//") def summary(host, proto="ipv4"): output = '

' + host + ' (' + proto + ') show protocols

' @@ -124,7 +154,7 @@ def summary(host, proto="ipv4"): name = d[0] typ = d[1] if typ == "BGP": - output += '%s%s'%(host,proto,name,name,infos.replace(name,"").strip()) + output += '%s%s'%(host,proto,name,name,infos.replace(name,"").strip()) output += '' else: output += string @@ -176,6 +206,6 @@ def get_cmd_result(host, proto, cmd): sock.close() return (ret, string) +app.debug = True if __name__ == "__main__": - #app.debug = True - app.run() + app.run("0.0.0.0") diff --git a/templates/layout.html b/templates/layout.html index 90c18c5..6a421fb 100644 --- a/templates/layout.html +++ b/templates/layout.html @@ -45,16 +45,16 @@ switch ($("#typ").val()) { case "summary": - document.location = "/" + $("#host").val() + "/summary"; + document.location = "/summary/" + $("#host").val() ; break; case "detail": - document.location = "/" + $("#host").val() + "/detail/" + $("#name").val() ; + document.location = "/detail/" + $("#host").val() + "/" + $("#name").val() ; break; case "prefix": - document.location = "/" + $("#host").val() + "/prefix/" + $("#prefix").val() ; + document.location = "/prefix/" + $("#host").val() + "/" + $("#prefix").val() ; break; case "prefix_detail": - document.location = "/" + $("#host").val() + "/prefix_detail/" + $("#prefix").val() ; + document.location = "/prefix_detail/" + $("#host").val() + "/" + $("#prefix").val() ; break; } });