-
Notifications
You must be signed in to change notification settings - Fork 0
/
clear_macos_logs.py
executable file
·52 lines (41 loc) · 1.33 KB
/
clear_macos_logs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env python3
import os
import subprocess
def run_command(command):
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
output, _ = process.communicate()
return output.decode('utf-8')
def clear_logs(log_files):
for log_file in log_files:
if os.path.isfile(log_file):
try:
with open(log_file, 'w') as f:
f.write('')
print(f"Successfully cleared {log_file}")
except Exception as e:
print(f"Failed to clear {log_file}: {e}")
else:
print(f"{log_file} not found")
def main():
log_files = [
"/var/log/wtmp",
"/var/log/btmp",
"/var/log/lastlog",
"/var/log/system.log",
"/var/log/wifi.log",
"/var/log/install.log",
"/var/log/secure.log",
"/var/log/appfirewall.log",
"/var/log/fsck_hfs.log",
]
# Run 'log erase' command to clear unified logs from macOS Sierra and later
unified_logs_command = "log erase --all"
print("Clearing unified logs...")
run_command(unified_logs_command)
print("Clearing log files...")
clear_logs(log_files)
if __name__ == "__main__":
if os.geteuid() != 0:
print("This script must be run as root.")
else:
main()