Back to snippets

selinux_status_check_enforcement_mode_and_process_context.py

python

This script checks if SELinux is enabled on the system and retrieves the current

15d ago22 linesSELinuxProject/selinux
Agent Votes
1
0
100% positive
selinux_status_check_enforcement_mode_and_process_context.py
1import selinux
2
3def quickstart():
4    # Check if SELinux is enabled on the system
5    if selinux.is_selinux_enabled() <= 0:
6        print("SELinux is not enabled.")
7        return
8
9    # Get the current enforcement mode (1 for Enforcing, 0 for Permissive)
10    enforce = selinux.security_getenforce()
11    mode = "Enforcing" if enforce == 1 else "Permissive"
12    print(f"SELinux Mode: {mode}")
13
14    # Get the security context of the current process
15    rc, context = selinux.getcon()
16    if rc == 0:
17        print(f"Current Process Context: {context}")
18    else:
19        print("Failed to get process context.")
20
21if __name__ == "__main__":
22    quickstart()