Skip to content

Instantly share code, notes, and snippets.

@blha303
Last active August 28, 2018 15:14
Show Gist options
  • Save blha303/b2b53caacde0c6a64a2bcac50772e9d7 to your computer and use it in GitHub Desktop.
Save blha303/b2b53caacde0c6a64a2bcac50772e9d7 to your computer and use it in GitHub Desktop.
A Looking Glass implementation in Python3 and Flask. Vet code for security concerns before deploying, this isn't tested for vulnerabilities in any way
#!/usr/bin/env python3
from flask import Flask,request,make_response
from shlex import quote
from subprocess import check_output
import socket
from requests import post
app = Flask(__name__)
@app.route('/')
def index():
return """<form method="post" action="process">
<input type="text" name="host">
<select name="service">
<option value="ping">Ping</option>
<option value="mtr">MTR</option>
<option value="bgp">bgp</option>
</select>
<input type="submit"></form>
"""
@app.route("/process", methods=["POST"])
def process():
host,service = request.form.get("host", "8.8.8.8"),request.form.get("service","ping")
try:
# Is it an ip?
_ = socket.inet_aton(host)
except OSError:
# Well it's not ipv4,
try:
_ = socket.inet_pton(socket.AF_INET6, host)
except OSError:
# Not ipv6 either. We'll try resolving dns
try:
host = socket.gethostbyname(host)
except socket.gaierror:
return "We only support ipv4/ipv6/valid domains\n"
else:
pass # host is ipv6
else:
pass # host is ipv4
print("Processing request {},{} from {} {}".format(host, service, request.environ["REMOTE_ADDR"], request.headers.get("User-Agent")))
if service == "ping":
resp = make_response(check_output(["ping", "-i", "0.2", "-q", "-c", "4", quote(host)]))
elif service == "mtr":
resp = make_response(check_output(["mtr", "-c", "1", "--report-wide", "-m", "60", "-b", quote(host)]))
elif service == "bgp":
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")
resp = make_response(data)
else:
return "oi"
print(" Completed request {},{} from {} {}".format(host,service, request.environ["REMOTE_ADDR"], request.headers.get("User-Agent")))
resp.headers["Content-Type"] = "text/plain"
return resp
if __name__ == "__main__":
app.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment