123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- #!/usr/bin/env python3
- from telnetlib import Telnet
- import toml
- from flask import Flask, jsonify, render_template_string
- app = Flask(__name__)
- def get_users():
- with open("secret.toml") as infile:
- cfg = toml.load(infile)
- login = ("login %s %s\n" % (cfg["user"], cfg["pass"])).encode("utf-8")
- with Telnet(cfg["host"], cfg["port"], 5) as tn:
- print("connection")
- print(tn.read_until(b"\n").decode("ascii"))
- print(tn.read_until(b"\n").decode("ascii"))
- print("----")
- tn.write(login)
- print("after login")
- print(tn.read_until(b"\n").decode("ascii"))
- print("----")
- tn.write(b"use 1 -virtual\n")
- print("after use")
- print(tn.read_until(b"\n").decode("ascii"))
- print("----")
- tn.write(b"clientlist\n")
- response = tn.read_until(b"\n").decode("ascii")
- print("after clientlist")
- print(response)
- print(tn.read_until(b"\n").decode("ascii"))
- print("----")
- # TODO clean this nicely
- client_info = {info["client_nickname"]: info for info in [{k: v for k, v in [i.split("=", 1) for i in ent.split()]} for ent in response.split("|")] if "serveradmin" not in info["client_nickname"]}
- for k, v in client_info.items():
- tn.write(f"clientinfo clid={v['clid']}\n".encode("utf-8"))
- response = tn.read_until(b"\n").decode("ascii")
- print(f"after clientinfo for {k}")
- print(response)
- print(tn.read_until(b"\n").decode("ascii"))
- print("----")
- v["client_info"] = {k: v for k, v in [ent.split("=", 1) for ent in response.split() if "=" in ent]}
- tn.write(b"quit\n")
- print("after quit")
- print(tn.read_until(b"\n").decode("ascii"))
- import pprint
- pprint.pprint(client_info)
- return client_info
- @app.route("/")
- def get_status():
- return jsonify({"users": list(get_users().keys())})
- @app.route("/page")
- def get_status_page():
- info = get_users()
- users = list(info.keys())
- if len(users) == 0:
- text = "No one in teamspeak!"
- elif len(users) == 1:
- text = f"Only {users[0]}"
- else:
- text = "I see the following people: " + ", ".join(users)
- return render_template_string("""
- <!doctype html>
- <title>Teamspeak Server Status</title>
- <style>
- body {
- margin: 0px 0px 0px 0px;
- padding: 0px 0px 0px 0px;
- font-family: verdana, arial, helvetica, sans-serif;
- color: #ccc;
- background-color: #333;
- }
- h1 {
- font-size: 24px;
- line-height: 44px;
- font-weight: bold;
- margin-top: 0;
- margin-bottom: 0;
- }
- </style>
- {% if scrolling %}
- <marquee direction="up" style="height 600px" behavior="alternate">
- <marquee direction="right" style="width 600px" behavior="alternate">
- {% endif %}
- <div class="page">
- <h1>TeamSpeak Server Status</h1>
- {{ r }}
- </div>
- {% if scrolling %}
- </marquee>
- </marquee>
- {% endif %}
- """, scrolling=True, r=text)
- if __name__ == "__main__":
- print(get_users())
- # app.run("0.0.0.0", 5000, debug=True, threaded=True)
|