1
0
Fork 0
mirror of https://github.com/sileht/bird-lg.git synced 2024-11-25 08:14:42 +01:00

Cleanup and handle all errors codes

This commit is contained in:
Mehdi ABAAKOUK 2011-12-16 12:52:56 +01:00
parent 94b4b48c28
commit ff0b8c1259

36
lg.py
View file

@ -56,24 +56,12 @@ def prefix(host, proto, prefix, mask=""):
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>'
# security check
#ok, string = get_cmd_result(host , proto, "show protocols")
ok = True
if ok:
#protocols = [ s.split()[0] for s in string.split("\n") if s.startswith(" ") ]a
protocols = [ name ]
if name in protocols :
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(" ")]) string = "\n".join([ s.strip() for s in string.split("\n") if s.startswith(" ")])
output +='<pre>' + string + '</pre>' output +='<pre>' + string + '</pre>'
else: else:
output += string output += string
else:
output += name + ' don\'t exist'
else:
output += string
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>/<proto>/summary") @app.route("/<host>/<proto>/summary")
@ -83,26 +71,13 @@ def summary(host, proto):
ok, string = get_cmd_result(host , proto, "show protocols") ok, string = get_cmd_result(host , proto, "show protocols")
if ok: if ok:
output += '<pre><table>' output += '<pre><table>'
protocols_info = [ s for s in string.split("\n") if s.startswith(" ") ] for infos in string.split("\n"):
for infos in protocols_info: if not infos.startswith(" "): continue
d = infos.split() d = infos.split()
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,"")) output += '<tr><td><a href="/%s/%s/detail/%s">%s</a><td><td>%s</td></tr>'%(host,proto,name,name,infos.replace(name,"").strip())
continue
try:
name, typ, status, up, date, hour, info = infos
except:
try:
name, typ, status, up, date, hour = infos
info = ""
except:
name, typ, status, up, hour = infos
info = ""
date = ""
if typ == "BGP":
output += '<tr><td><a href="/%s/%s/detail/%s">%s</a><td><td>%s</td><td>%s</td><td>%s</td><td>%s %s</td><td>%s</td></tr>'%(host,proto,name,name,typ,status,up,date,hour,info)
output += '</table></pre>' output += '</table></pre>'
else: else:
output += string output += string
@ -136,15 +111,14 @@ def get_cmd_result(host, proto, cmd):
data = sock.recv(bufsize) data = sock.recv(bufsize)
string = data string = data
app.logger.info("read %s (%d)", data, len(data)) app.logger.info("read %s (%d)", data, len(data))
code = string.strip()[len(string.strip())-4:]
code = string.split("\n")[-2][0:4] code = string.split("\n")[-2][0:4]
while not code in ["0000", "9001"]: while not code[0] in ["0", "9", "8"]:
data = sock.recv(bufsize) data = sock.recv(bufsize)
string = string + data string = string + data
app.logger.info("read %s (%d)", data, len(data)) app.logger.info("read %s (%d)", data, len(data))
code = string.strip()[len(string.strip())-4:] code = string.strip()[len(string.strip())-4:]
if code == "9001": if code[0] in [ "9", "8" ]:
ret = False ret = False
app.logger.info("return %s",string) app.logger.info("return %s",string)