| 1 |
# SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
# Print lastlog2 database as JSON; used in ./login.js |
| 3 |
import json |
| 4 |
import os |
| 5 |
import sqlite3 |
| 6 |
import sys |
| 7 |
|
| 8 |
DB = "/var/lib/lastlog/lastlog2.db" |
| 9 |
|
| 10 |
if not os.path.exists(DB): |
| 11 |
sys.exit(f"{DB} does not exist") |
| 12 |
|
| 13 |
con = sqlite3.connect(DB) |
| 14 |
cur = con.cursor() |
| 15 |
query = "SELECT Name, Time, TTY, RemoteHost FROM Lastlog2" |
| 16 |
if len(sys.argv) == 2: |
| 17 |
res = cur.execute(query + " WHERE Name = ?", [sys.argv[1]]) |
| 18 |
else: |
| 19 |
res = cur.execute(query) |
| 20 |
users = {} |
| 21 |
for [name, time, tty, host] in res: |
| 22 |
users[name] = {"time": time, "tty": tty, "host": host} |
| 23 |
print(json.dumps(users)) |