mirror of
https://github.com/sileht/bird-lg.git
synced 2024-11-22 06:54:43 +01:00
Initial Commit
This commit is contained in:
commit
6a68df0534
160
lg.py
Executable file
160
lg.py
Executable file
|
@ -0,0 +1,160 @@
|
||||||
|
#!/usr/bin/python
|
||||||
|
|
||||||
|
import sys
|
||||||
|
|
||||||
|
from flask import Flask, render_template, jsonify
|
||||||
|
app = Flask(__name__)
|
||||||
|
|
||||||
|
import socket
|
||||||
|
|
||||||
|
def check_mask(n):
|
||||||
|
if not n:
|
||||||
|
return True
|
||||||
|
try:
|
||||||
|
mask = int(n)
|
||||||
|
return ( mask > 1 and mask < 128)
|
||||||
|
except:
|
||||||
|
return False
|
||||||
|
|
||||||
|
def check_ipv4(n):
|
||||||
|
try:
|
||||||
|
socket.inet_pton(socket.AF_INET, n)
|
||||||
|
return True
|
||||||
|
except socket.error:
|
||||||
|
return False
|
||||||
|
|
||||||
|
def check_ipv6(n):
|
||||||
|
try:
|
||||||
|
socket.inet_pton(socket.AF_INET6, n)
|
||||||
|
return True
|
||||||
|
except socket.error:
|
||||||
|
return False
|
||||||
|
|
||||||
|
@app.route("/")
|
||||||
|
def hello():
|
||||||
|
return render_template('index.html')
|
||||||
|
|
||||||
|
@app.route("/<host>/<proto>/prefix/<prefix>")
|
||||||
|
@app.route("/<host>/<proto>/prefix/<prefix>/<mask>")
|
||||||
|
def prefix(host, proto, prefix, mask=""):
|
||||||
|
output = '<h3>' + host + '(' + proto + ') show route for ' + prefix + '/' + mask + '</h3>'
|
||||||
|
|
||||||
|
# security check
|
||||||
|
if ( (proto == "ipv6" and check_ipv6(prefix)) or (proto == "ipv4" and check_ipv4(prefix)) ) and check_mask(mask):
|
||||||
|
if mask: prefix = prefix +"/"+mask
|
||||||
|
ok, string = get_cmd_result(host , proto, "show route for " + prefix)
|
||||||
|
if ok:
|
||||||
|
string = "\n".join([ s.replace("1007-"," ") for s in string.split("\n") if not s.startswith("0000") ])
|
||||||
|
output +='<pre>' + string + '</pre>'
|
||||||
|
else:
|
||||||
|
output += string
|
||||||
|
else:
|
||||||
|
output += prefix + ' not allowed'
|
||||||
|
return render_template('index.html', output=output, typ="prefix", host=host+"/"+proto, prefix=prefix)
|
||||||
|
|
||||||
|
@app.route("/<host>/<proto>/detail/<name>")
|
||||||
|
def detail(host, proto, name):
|
||||||
|
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)
|
||||||
|
if ok:
|
||||||
|
string = "\n".join([ s.strip() for s in string.split("\n") if s.startswith(" ")])
|
||||||
|
output +='<pre>' + string + '</pre>'
|
||||||
|
else:
|
||||||
|
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)
|
||||||
|
|
||||||
|
@app.route("/<host>/<proto>/summary")
|
||||||
|
def summary(host, proto):
|
||||||
|
output = '<h3>' + host + '(' + proto + ') show protocols</h3>'
|
||||||
|
|
||||||
|
ok, string = get_cmd_result(host , proto, "show protocols")
|
||||||
|
if ok:
|
||||||
|
output += '<pre><table>'
|
||||||
|
protocols_info = [ s for s in string.split("\n") if s.startswith(" ") ]
|
||||||
|
for infos in protocols_info:
|
||||||
|
d = infos.split()
|
||||||
|
name = d[0]
|
||||||
|
typ = d[1]
|
||||||
|
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,""))
|
||||||
|
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>'
|
||||||
|
else:
|
||||||
|
output += string
|
||||||
|
return render_template('index.html', output=output, typ="summary", host=host+"/"+proto)
|
||||||
|
|
||||||
|
@app.route("/<host>/<proto>/status")
|
||||||
|
def status(host, proto):
|
||||||
|
string = get_cmd_result(host, proto, "show status")
|
||||||
|
output = '<pre>' + string + '</pre>'
|
||||||
|
return render_template('index.html', output=output, host=host+"/"+proto)
|
||||||
|
|
||||||
|
def get_cmd_result(host, proto, cmd):
|
||||||
|
|
||||||
|
ret = True
|
||||||
|
if proto == "ipv4":
|
||||||
|
port = 9994
|
||||||
|
else:
|
||||||
|
port = 9996
|
||||||
|
|
||||||
|
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
|
sock.settimeout(3.0)
|
||||||
|
# sock.setblocking(0)
|
||||||
|
try:
|
||||||
|
sock.connect((host, port))
|
||||||
|
app.logger.info("open socket on %s:%d", host, port)
|
||||||
|
|
||||||
|
sock.send(cmd + "\n")
|
||||||
|
app.logger.info("send %s socket on %s:%d", cmd, host, port)
|
||||||
|
|
||||||
|
bufsize = 4096
|
||||||
|
data = sock.recv(bufsize)
|
||||||
|
string = data
|
||||||
|
app.logger.info("read %s (%d)", data, len(data))
|
||||||
|
code = string.strip()[len(string.strip())-4:]
|
||||||
|
code = string.split("\n")[-2][0:4]
|
||||||
|
while not code in ["0000", "9001"]:
|
||||||
|
data = sock.recv(bufsize)
|
||||||
|
string = string + data
|
||||||
|
app.logger.info("read %s (%d)", data, len(data))
|
||||||
|
code = string.strip()[len(string.strip())-4:]
|
||||||
|
|
||||||
|
if code == "9001":
|
||||||
|
ret = False
|
||||||
|
|
||||||
|
app.logger.info("return %s",string)
|
||||||
|
except Exception as detail:
|
||||||
|
ret = False
|
||||||
|
string = "Failed connect to %s:%d (%s)"%(host, port, detail)
|
||||||
|
app.logger.error(string)
|
||||||
|
sock.close()
|
||||||
|
return (ret, string)
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
app.debug = True
|
||||||
|
app.run()
|
4
static/jquery.js
vendored
Normal file
4
static/jquery.js
vendored
Normal file
File diff suppressed because one or more lines are too long
0
static/style.css
Normal file
0
static/style.css
Normal file
4
templates/index.html
Normal file
4
templates/index.html
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
{% extends "layout.html" %}
|
||||||
|
{% block body %}
|
||||||
|
{{ output|safe }}
|
||||||
|
{% endblock %}
|
64
templates/layout.html
Normal file
64
templates/layout.html
Normal file
|
@ -0,0 +1,64 @@
|
||||||
|
<!doctype html>
|
||||||
|
<title>Tetaneutral.net looking glass</title>
|
||||||
|
<link rel=stylesheet type=text/css href="{{ url_for('static', filename='style.css') }}">
|
||||||
|
<script type="text/javascript" src="{{url_for('static', filename='jquery.js') }}"></script>
|
||||||
|
<script type="text/javascript">
|
||||||
|
$(function() {
|
||||||
|
$("#typ").change(function(){
|
||||||
|
$("#name").hide()
|
||||||
|
$("#prefix").hide()
|
||||||
|
switch ($("#typ").val())
|
||||||
|
{
|
||||||
|
case "summary":
|
||||||
|
break;
|
||||||
|
case "detail":
|
||||||
|
$("#name").show()
|
||||||
|
break;
|
||||||
|
case "prefix":
|
||||||
|
$("#prefix").show()
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
});
|
||||||
|
$("#submit").click( function(){
|
||||||
|
switch ($("#typ").val())
|
||||||
|
{
|
||||||
|
case "summary":
|
||||||
|
document.location = "/" + $("#host").val() + "/summary";
|
||||||
|
break;
|
||||||
|
case "detail":
|
||||||
|
document.location = "/" + $("#host").val() + "/detail/" + $("#name").val() ;
|
||||||
|
break;
|
||||||
|
case "prefix":
|
||||||
|
document.location = "/" + $("#host").val() + "/prefix/" + $("#prefix").val() ;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
$("#typ option[value={{typ}}]").attr("selected", "selected")
|
||||||
|
$("#typ").change()
|
||||||
|
$("#host option[value='{{host}}']").attr("selected", "selected")
|
||||||
|
$("#host").change()
|
||||||
|
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
<div id="page">
|
||||||
|
<h1>Tetaneutral.net Looking Glass</h1>
|
||||||
|
<form>
|
||||||
|
<select id="host">
|
||||||
|
<option value="gw/ipv4">gw (ipv4)</option>
|
||||||
|
<option value="gw/ipv6">gw (ipv6)</option>
|
||||||
|
<option value="h3/ipv4" selected="selected">h3 (ipv4)</option>
|
||||||
|
<option value="h3/ipv6">h3 (ipv6)</option>
|
||||||
|
</select>
|
||||||
|
<select id="typ">
|
||||||
|
<option value="summary">summary</option>
|
||||||
|
<option value="detail">detail</option>
|
||||||
|
<option value="prefix">prefix</option>
|
||||||
|
</select>
|
||||||
|
|
||||||
|
<input type="text" id="name" style="display:none" value="{{ name }}"></input>
|
||||||
|
<input type="text" id="prefix" style="display:none" value="{{ prefix }}"></input>
|
||||||
|
<input type="button" id="submit" value="submit"></input>
|
||||||
|
</form>
|
||||||
|
{% block body %}{% endblock %}
|
||||||
|
</div>
|
Loading…
Reference in a new issue