From 6e5755ecaa4b3ce516325df40f290f4f5677c55d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hauke=20Z=C3=BChl?= Date: Thu, 5 Jun 2025 07:46:07 +0200 Subject: [PATCH] Auslesen von Prozessinfos --- python/show-proc-data.py | 69 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 python/show-proc-data.py diff --git a/python/show-proc-data.py b/python/show-proc-data.py new file mode 100644 index 0000000..a3ae000 --- /dev/null +++ b/python/show-proc-data.py @@ -0,0 +1,69 @@ +#!/usr/bin/env python + +import os +import sys +import struct + +def read_proc_data(pid): + '''Liest Daten einer Prozess-ID aus /proc aus und zeigt diese an''' + + proc_dir = '/proc/{0}'.format(pid) + + if not os.path.exists(proc_dir): + return "Prozess mit PID {0} existiert nicht.".format(pid) + + # Lese die Daten aus dem /proc-Verzeichnis + for filename in os.listdir(proc_dir): + file_path = os.path.join(proc_dir, filename) + + # Ueberspringe Dateien, die keine Informationen enthalten + if filename in [".", "..", "task", "mem", "cwd", "exe", "pagemap"]: + continue + + try: + if filename in ("environ", "cmdline"): + with open(file_path, "rb") as file: + environ_data = file.read() + print("{0}:".format(filename)) + for line in environ_data.decode("utf-8", errors="replace").split("\x00"): + if line: + print(line) + + print() + elif filename == "auxv": + with open(file_path, "rb") as file: + auxv_data = file.read() + print("{0}:".format(filename)) + i = 0 + while i < len(auxv_data): + entry_type, entry_value = struct.unpack("qq", auxv_data[i:i+16]) + print("Type: {0}, Value: {1}".format(entry_type, entry_value)) + i += 16 + print() + else: + with open(file_path, "r") as file: + print("{0}:".format(filename)) + for line in file: + line = line.rstrip("\n") + if line: + print(line) + print() + + except (IOError, UnicodeDecodeError): + pass + + return None + +if __name__ == "__main__": + if len(sys.argv) != 2: + print("Bitte geben Sie die Prozess-ID (PID) als Argument an.") + sys.exit(1) + + try: + pid = int(sys.argv[1]) + except ValueError: + print("Ungueltige Prozess-ID (PID).") + sys.exit(1) + + read_proc_data(pid) +