update script.py to be able to go back in time

main
vitrinekast 8 months ago
parent fae00e7109
commit 83f101f661

@ -3,11 +3,13 @@ import subprocess
from datetime import datetime, timedelta from datetime import datetime, timedelta
import os import os
import sys import sys
import re
if len(sys.argv) > 1: if len(sys.argv) > 1:
time_ago_arg = int(sys.argv[1]) time_ago_arg = int(sys.argv[1])
print(f"Going back in time by {time_ago_arg} days") print(f"Going back in time by {time_ago_arg} days")
else: else:
print(f"Creating a book for today")
time_ago_arg = False time_ago_arg = False
#create the log directory #create the log directory
@ -79,14 +81,23 @@ print("Commands: journalctl, this sometimes takes longer");
journal_today = run_command(["sudo", "journalctl","_COMM=useradd", "_COMM=usermod", "_COMM=userdel","_COMM=groupremove", "_COMM=groupadd","-r", "--utc"] + get_journalctl_on()).splitlines() journal_today = run_command(["sudo", "journalctl","_COMM=useradd", "_COMM=usermod", "_COMM=userdel","_COMM=groupremove", "_COMM=groupadd","-r", "--utc"] + get_journalctl_on()).splitlines()
# TODO this is not working with the time ago. name should be grepped from user_created_today # TODO this is not working with the time ago. name should be grepped from user_created_today
last_user_added = subprocess.run(["sudo", "journalctl","_COMM=useradd","-r","-n", "1" , "--output-fields=MESSAGE"], capture_output=True) last_user_added = subprocess.run(["sudo", "journalctl","_COMM=useradd","-r","-n", "1" , "--output-fields=MESSAGE"], capture_output=True)
last_user_added_name = run_command(['grep', '-Po', "(?<=name)\W*\K[^ ]*"], based=last_user_added.stdout) last_user_added_name = run_command(['grep', '-Po', "(?<=name)\W*\K[^ ]*"], based=last_user_added.stdout)
users_created_today = run_command(["sudo", "journalctl","_COMM=useradd","-r"] + get_journalctl_on()).splitlines()
journal_today = subprocess.run(["sudo", "journalctl","_COMM=systemd-logind","_COMM=useradd", "_COMM=usermod", "_COMM=userdel","_COMM=groupremove", "_COMM=groupadd","-r"] + get_journalctl_on(), check=True, capture_output=True) journal_today = subprocess.run(["sudo", "journalctl","_COMM=systemd-logind","_COMM=useradd", "_COMM=usermod", "_COMM=userdel","_COMM=groupremove", "_COMM=groupadd","-r"] + get_journalctl_on(), check=True, capture_output=True)
def get_last_user(log):
if log:
last = log[-1]
result = re.findall(r"name=([^,]*)", users_created_today[-1])
return result[0]
else:
return False
try: try:
groups_created = subprocess.run(['grep','groupadd'], capture_output=True, input=journal_today.stdout).stdout.decode('UTF-8').strip().splitlines() groups_created = subprocess.run(['grep','groupadd'], capture_output=True, input=journal_today.stdout).stdout.decode('UTF-8').strip().splitlines()
groups_removed = subprocess.run(['grep','groupremove'], capture_output=True, input=journal_today.stdout).stdout.decode('UTF-8').strip().splitlines() groups_removed = subprocess.run(['grep','groupremove'], capture_output=True, input=journal_today.stdout).stdout.decode('UTF-8').strip().splitlines()
@ -95,6 +106,8 @@ try:
user_deleted = subprocess.run(['grep','userdel'], capture_output=True, input=journal_today.stdout).stdout.decode('UTF-8').strip().splitlines() user_deleted = subprocess.run(['grep','userdel'], capture_output=True, input=journal_today.stdout).stdout.decode('UTF-8').strip().splitlines()
logins_today = subprocess.run(['grep','New session'], capture_output=True, input=journal_today.stdout).stdout.decode('UTF-8').strip().splitlines() logins_today = subprocess.run(['grep','New session'], capture_output=True, input=journal_today.stdout).stdout.decode('UTF-8').strip().splitlines()
last_user_name = get_last_user(users_created_today)
except subprocess.CalledProcessError as e: except subprocess.CalledProcessError as e:
raise RuntimeError("command '{}' return with error (code {}): {}".format(e.cmd, e.returncode, e.output)) raise RuntimeError("command '{}' return with error (code {}): {}".format(e.cmd, e.returncode, e.output))
@ -130,7 +143,8 @@ output = template.render(
users_created_today = users_created_today, users_created_today = users_created_today,
user_modified = user_modified, user_modified = user_modified,
user_deleted = user_deleted, user_deleted = user_deleted,
logins_today = logins_today logins_today = logins_today,
last_user_name = last_user_name if not last_user_name else False
) )

Loading…
Cancel
Save