Steven Smith revised this gist . Go to revision
1 file changed, 1 insertion, 1 deletion
lookingglass.py
| @@ -45,7 +45,7 @@ def process(): | |||
| 45 | 45 | elif service == "mtr": | |
| 46 | 46 | resp = make_response(check_output(["mtr", "-c", "1", "--report-wide", "-m", "60", "-b", quote(host)])) | |
| 47 | 47 | elif service == "bgp": | |
| 48 | - | data = post("http://noc.i3d.net/looking-glass/execute.php", data={"routers": "ausyd1-rt001i", "query": "bgp", "parameter": host, "dontlook": ""}).json()["result"][8:-6].strip().replace('</kdb></p><pre class="pre-scrollable">', "\n\n") | |
| 48 | + | data = post("http://noc.i3d.net/looking-glass/execute.php", data={"routers": "uslax1-rt001i", "query": "bgp", "parameter": host, "dontlook": ""}).json()["result"][8:-6].strip().replace('</kdb></p><pre class="pre-scrollable">', "\n\n") | |
| 49 | 49 | resp = make_response(data) | |
| 50 | 50 | else: | |
| 51 | 51 | return "oi" | |
Steven Smith revised this gist . Go to revision
1 file changed, 11 insertions, 4 deletions
lookingglass.py
| @@ -3,15 +3,19 @@ from flask import Flask,request,make_response | |||
| 3 | 3 | from shlex import quote | |
| 4 | 4 | from subprocess import check_output | |
| 5 | 5 | import socket | |
| 6 | + | from requests import post | |
| 6 | 7 | ||
| 7 | 8 | app = Flask(__name__) | |
| 8 | 9 | ||
| 9 | 10 | @app.route('/') | |
| 10 | 11 | def index(): | |
| 11 | 12 | return """<form method="post" action="process"> | |
| 12 | - | <input type="text" name="host"><br> | |
| 13 | - | <input type="radio name="service" id="ping"><label for="ping">Ping</label><br> | |
| 14 | - | <input type="radio name="service" id="mtr"><label for="mtr">mtr</label><br> | |
| 13 | + | <input type="text" name="host"> | |
| 14 | + | <select name="service"> | |
| 15 | + | <option value="ping">Ping</option> | |
| 16 | + | <option value="mtr">MTR</option> | |
| 17 | + | <option value="bgp">bgp</option> | |
| 18 | + | </select> | |
| 15 | 19 | <input type="submit"></form> | |
| 16 | 20 | """ | |
| 17 | 21 | ||
| @@ -40,6 +44,9 @@ def process(): | |||
| 40 | 44 | resp = make_response(check_output(["ping", "-i", "0.2", "-q", "-c", "4", quote(host)])) | |
| 41 | 45 | elif service == "mtr": | |
| 42 | 46 | resp = make_response(check_output(["mtr", "-c", "1", "--report-wide", "-m", "60", "-b", quote(host)])) | |
| 47 | + | elif service == "bgp": | |
| 48 | + | data = post("http://noc.i3d.net/looking-glass/execute.php", data={"routers": "ausyd1-rt001i", "query": "bgp", "parameter": host, "dontlook": ""}).json()["result"][8:-6].strip().replace('</kdb></p><pre class="pre-scrollable">', "\n\n") | |
| 49 | + | resp = make_response(data) | |
| 43 | 50 | else: | |
| 44 | 51 | return "oi" | |
| 45 | 52 | print(" Completed request {},{} from {} {}".format(host,service, request.environ["REMOTE_ADDR"], request.headers.get("User-Agent"))) | |
| @@ -47,4 +54,4 @@ def process(): | |||
| 47 | 54 | return resp | |
| 48 | 55 | ||
| 49 | 56 | if __name__ == "__main__": | |
| 50 | - | app.run(port=5001) | |
| 57 | + | app.run() | |
Steven Smith revised this gist . Go to revision
1 file changed, 2 insertions, 2 deletions
lookingglass.py
| @@ -37,9 +37,9 @@ def process(): | |||
| 37 | 37 | pass # host is ipv4 | |
| 38 | 38 | print("Processing request {},{} from {} {}".format(host, service, request.environ["REMOTE_ADDR"], request.headers.get("User-Agent"))) | |
| 39 | 39 | if service == "ping": | |
| 40 | - | resp = make_response(check_output(["ping", "-i", "0.2", "-q", "-c", "4", host])) | |
| 40 | + | resp = make_response(check_output(["ping", "-i", "0.2", "-q", "-c", "4", quote(host)])) | |
| 41 | 41 | elif service == "mtr": | |
| 42 | - | resp = make_response(check_output(["mtr", "-c", "1", "--report-wide", "-m", "60", "-b", host])) | |
| 42 | + | resp = make_response(check_output(["mtr", "-c", "1", "--report-wide", "-m", "60", "-b", quote(host)])) | |
| 43 | 43 | else: | |
| 44 | 44 | return "oi" | |
| 45 | 45 | print(" Completed request {},{} from {} {}".format(host,service, request.environ["REMOTE_ADDR"], request.headers.get("User-Agent"))) | |
Steven Smith revised this gist . Go to revision
1 file changed, 2 insertions, 3 deletions
lookingglass.py
| @@ -17,7 +17,7 @@ def index(): | |||
| 17 | 17 | ||
| 18 | 18 | @app.route("/process", methods=["POST"]) | |
| 19 | 19 | def process(): | |
| 20 | - | host = request.form.get("host", "8.8.8.8") | |
| 20 | + | host,service = request.form.get("host", "8.8.8.8"),request.form.get("service","ping") | |
| 21 | 21 | try: | |
| 22 | 22 | # Is it an ip? | |
| 23 | 23 | _ = socket.inet_aton(host) | |
| @@ -35,8 +35,7 @@ def process(): | |||
| 35 | 35 | pass # host is ipv6 | |
| 36 | 36 | else: | |
| 37 | 37 | pass # host is ipv4 | |
| 38 | - | host,service = quote(request.form.get("host", "8.8.8.8")),request.form.get("service","ping") | |
| 39 | - | print("Processing request {},{} from {} {}".format(host,service, request.environ["REMOTE_ADDR"], request.headers.get("User-Agent"))) | |
| 38 | + | print("Processing request {},{} from {} {}".format(host, service, request.environ["REMOTE_ADDR"], request.headers.get("User-Agent"))) | |
| 40 | 39 | if service == "ping": | |
| 41 | 40 | resp = make_response(check_output(["ping", "-i", "0.2", "-q", "-c", "4", host])) | |
| 42 | 41 | elif service == "mtr": | |
Steven Smith revised this gist . Go to revision
1 file changed, 51 insertions
lookingglass.py(file created)
| @@ -0,0 +1,51 @@ | |||
| 1 | + | #!/usr/bin/env python3 | |
| 2 | + | from flask import Flask,request,make_response | |
| 3 | + | from shlex import quote | |
| 4 | + | from subprocess import check_output | |
| 5 | + | import socket | |
| 6 | + | ||
| 7 | + | app = Flask(__name__) | |
| 8 | + | ||
| 9 | + | @app.route('/') | |
| 10 | + | def index(): | |
| 11 | + | return """<form method="post" action="process"> | |
| 12 | + | <input type="text" name="host"><br> | |
| 13 | + | <input type="radio name="service" id="ping"><label for="ping">Ping</label><br> | |
| 14 | + | <input type="radio name="service" id="mtr"><label for="mtr">mtr</label><br> | |
| 15 | + | <input type="submit"></form> | |
| 16 | + | """ | |
| 17 | + | ||
| 18 | + | @app.route("/process", methods=["POST"]) | |
| 19 | + | def process(): | |
| 20 | + | host = request.form.get("host", "8.8.8.8") | |
| 21 | + | try: | |
| 22 | + | # Is it an ip? | |
| 23 | + | _ = socket.inet_aton(host) | |
| 24 | + | except OSError: | |
| 25 | + | # Well it's not ipv4, | |
| 26 | + | try: | |
| 27 | + | _ = socket.inet_pton(socket.AF_INET6, host) | |
| 28 | + | except OSError: | |
| 29 | + | # Not ipv6 either. We'll try resolving dns | |
| 30 | + | try: | |
| 31 | + | host = socket.gethostbyname(host) | |
| 32 | + | except socket.gaierror: | |
| 33 | + | return "We only support ipv4/ipv6/valid domains\n" | |
| 34 | + | else: | |
| 35 | + | pass # host is ipv6 | |
| 36 | + | else: | |
| 37 | + | pass # host is ipv4 | |
| 38 | + | host,service = quote(request.form.get("host", "8.8.8.8")),request.form.get("service","ping") | |
| 39 | + | print("Processing request {},{} from {} {}".format(host,service, request.environ["REMOTE_ADDR"], request.headers.get("User-Agent"))) | |
| 40 | + | if service == "ping": | |
| 41 | + | resp = make_response(check_output(["ping", "-i", "0.2", "-q", "-c", "4", host])) | |
| 42 | + | elif service == "mtr": | |
| 43 | + | resp = make_response(check_output(["mtr", "-c", "1", "--report-wide", "-m", "60", "-b", host])) | |
| 44 | + | else: | |
| 45 | + | return "oi" | |
| 46 | + | print(" Completed request {},{} from {} {}".format(host,service, request.environ["REMOTE_ADDR"], request.headers.get("User-Agent"))) | |
| 47 | + | resp.headers["Content-Type"] = "text/plain" | |
| 48 | + | return resp | |
| 49 | + | ||
| 50 | + | if __name__ == "__main__": | |
| 51 | + | app.run(port=5001) | |