1
0
Fork 0
mirror of https://github.com/sileht/bird-lg.git synced 2024-11-25 16:24:43 +01:00

Create link for whois on as number

This commit is contained in:
Mehdi Abaakouk 2012-01-16 14:13:49 +01:00 committed by root
parent 677e3a2f73
commit 41aca54fba
2 changed files with 59 additions and 29 deletions

80
lg.py
View file

@ -1,8 +1,8 @@
#!/usr/bin/python #!/usr/bin/python
import sys import sys, os, subprocess, re
from dns import resolver,reversename from dns import resolver,reversename
from flask import Flask, render_template, jsonify from flask import Flask, render_template, jsonify, redirect
app = Flask(__name__) app = Flask(__name__)
import socket import socket
@ -33,17 +33,47 @@ def check_ipv6(n):
except socket.error: except socket.error:
return False 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'<a href="/whois/\1">\1</a>',text)
else:
return text
#@app.errorhandler(404)
#def notfound(error):
# return redirect("/")
@app.route("/") @app.route("/")
def hello(): def hello():
return render_template('index.html') return render_template('index.html')
@app.route("/<host>/<proto>/prefix/") @app.route("/whois/<asnum>")
@app.route("/<host>/<proto>/prefix/<prefix>") def whois(asnum):
@app.route("/<host>/<proto>/prefix/<prefix>/<mask>") output = "<h3> Whois as" + asnum + "</h3><pre>"
@app.route("/<host>/<proto>/prefix<all>/") try:
@app.route("/<host>/<proto>/prefix<all>/<prefix>") asnum = int(asnum)
@app.route("/<host>/<proto>/prefix<all>/<prefix>/<mask>") except:
def prefix(host, proto, prefix="", mask="", all=False): 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 += "</pre>"
return render_template('index.html', output=output, typ="whois", asnum=asnum)
@app.route("/prefix_detail/<host>/<proto>/")
@app.route("/prefix_detail/<host>/<proto>/<prefix>")
@app.route("/prefix_detail/<host>/<proto>/<prefix>/<mask>")
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/<host>/<proto>/")
@app.route("/prefix/<host>/<proto>/<prefix>")
@app.route("/prefix/<host>/<proto>/<prefix>/<mask>")
def show_route_for_prefix(host, proto, prefix="", mask="", all=False):
qprefix = prefix qprefix = prefix
@ -73,13 +103,13 @@ def prefix(host, proto, prefix="", mask="", all=False):
allowed = False allowed = False
output = '<h3>' + host + ' (' + proto + ') show route for ' + prefix + (prefix != qprefix and " (%s)"%qprefix or "") + (mask and '/' + mask or '' ) + (all and " all" or "") + '</h3>' output = '<h3>' + host + ' (' + proto + ') show route for ' + prefix + (prefix != qprefix and " (%s)"%qprefix or "") + (mask and '/' + mask or '' ) + (all and " all" or "") + '</h3>'
if allowed: if allowed:
if mask: qprefix = qprefix +"/"+mask if mask: qprefix = qprefix +"/"+mask
if mask: prefix = prefix +"/"+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: if ok:
string = "\n".join([ s.replace("1007-"," ") for s in string.split("\n") if not s.startswith("0000") ]) output += '<pre>' + cleanup_output(string) + '</pre>'
output +='<pre>' + string + '</pre>'
else: else:
output += string output += string
else: 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) return render_template('index.html', output=output, typ="prefix" + (all and "_detail" or ""), host=host+"/"+proto, prefix=prefix)
@app.route("/<host>/<proto>/detail/") @app.route("/detail/<host>/<proto>/")
@app.route("/<host>/<proto>/detail/<name>") @app.route("/detail/<host>/<proto>/<name>")
def detail(host, proto, name=""): def detail(host, proto, name=""):
output = '<h3>' + host + ' (' + proto + ') show protocols all ' + name + '</h3>' output = '<h3>' + host + ' (' + proto + ') show protocols all ' + name + '</h3>'
if name: if not name:
output += "name missing"
else:
ok, string = get_cmd_result(host , proto, "show protocols all " + name) ok, string = get_cmd_result(host , proto, "show protocols all " + name)
if ok: if ok:
string = "\n".join([ s.strip() for s in string.split("\n") if s.startswith(" ")]) output += '<pre>'
output +='<pre>' + string + '</pre>' output += "\n".join([ add_link(s.strip()) for s in string.split("\n") if s.startswith(" ")])
output += '</pre>'
else: else:
output += string output += string
else:
output += "name missing"
return render_template('index.html', output=output, typ="detail", host=host+"/"+proto, name=name) return render_template('index.html', output=output, typ="detail", host=host+"/"+proto, name=name)
@app.route("/<host>/") @app.route("/summary/<host>")
@app.route("/<host>/<proto>/") @app.route("/summary/<host>/<proto>")
@app.route("/<host>/<proto>/summary")
def summary(host, proto="ipv4"): def summary(host, proto="ipv4"):
output = '<h3>' + host + ' (' + proto + ') show protocols</h3>' output = '<h3>' + host + ' (' + proto + ') show protocols</h3>'
@ -124,7 +154,7 @@ def summary(host, proto="ipv4"):
name = d[0] name = d[0]
typ = d[1] typ = d[1]
if typ == "BGP": if typ == "BGP":
output += '<tr><td><a href="/%s/%s/detail/%s">%s</a><td><td>%s</td></tr>'%(host,proto,name,name,infos.replace(name,"").strip()) output += '<tr><td><a href="/detail/%s/%s/%s">%s</a><td><td>%s</td></tr>'%(host,proto,name,name,infos.replace(name,"").strip())
output += '</table></pre>' output += '</table></pre>'
else: else:
output += string output += string
@ -176,6 +206,6 @@ def get_cmd_result(host, proto, cmd):
sock.close() sock.close()
return (ret, string) return (ret, string)
app.debug = True
if __name__ == "__main__": if __name__ == "__main__":
#app.debug = True app.run("0.0.0.0")
app.run()

View file

@ -45,16 +45,16 @@
switch ($("#typ").val()) switch ($("#typ").val())
{ {
case "summary": case "summary":
document.location = "/" + $("#host").val() + "/summary"; document.location = "/summary/" + $("#host").val() ;
break; break;
case "detail": case "detail":
document.location = "/" + $("#host").val() + "/detail/" + $("#name").val() ; document.location = "/detail/" + $("#host").val() + "/" + $("#name").val() ;
break; break;
case "prefix": case "prefix":
document.location = "/" + $("#host").val() + "/prefix/" + $("#prefix").val() ; document.location = "/prefix/" + $("#host").val() + "/" + $("#prefix").val() ;
break; break;
case "prefix_detail": case "prefix_detail":
document.location = "/" + $("#host").val() + "/prefix_detail/" + $("#prefix").val() ; document.location = "/prefix_detail/" + $("#host").val() + "/" + $("#prefix").val() ;
break; break;
} }
}); });