This repository has been archived by the owner on Sep 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.py
68 lines (53 loc) · 1.81 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import json
import platform
import secrets
from datetime import datetime
from io import StringIO
import textfsm
from flask import Flask, render_template, request
from flask_socketio import SocketIO, emit
BIND, HOST, PORT = "127.0.0.1", "127.0.0.1", 5001
app = Flask(__name__)
app.config["SECRET_KEY"] = secrets.token_bytes(16)
socketio = SocketIO(app)
def parse_textfsm(data, template):
"""Given input data and TextFSM template, return tuple of parsing result
and created regexes."""
fsm = textfsm.TextFSM(StringIO(template.strip()))
regexes = "".join(
f"{rule.line_num:3} {state:15} {rule.regex}\n"
for state, rules in fsm.states.items()
for rule in rules
)
records = fsm.ParseText(data.rstrip())
return [dict(zip(fsm.header, record)) for record in records], regexes
@app.route("/")
def index():
return render_template(
"index.html",
port=PORT,
host=HOST,
versions=f"TextFSM {textfsm.__version__} Python {platform.python_version()}",
)
@socketio.on("connect")
def test_connect():
print("Client connected from {}".format(request.environ["REMOTE_ADDR"]))
@socketio.on("disconnect")
def test_disconnect():
print("Client disconnected from {}".format(request.environ["REMOTE_ADDR"]))
@socketio.on("parse")
def test_parse(message):
error = parsed = regexes = None
timestamp = datetime.now().strftime("%T")
try:
parsed, regexes = parse_textfsm(message["data"], message["template"])
parsed = json.dumps(parsed, indent=2)
except (KeyError, textfsm.Error) as e:
error = str(e)
emit(
"parse_response",
{"error": error, "text": parsed, "regex": regexes, "ts": timestamp},
)
if __name__ == "__main__":
print(f"Starting on {BIND}:{PORT}")
socketio.run(app, host=BIND, port=PORT)