Gesammelte und von mir selbst geschriebene Skripte
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Skripte/python/show-proc-data.py

69 lines
2.2 KiB

#!/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)