Advertisement
Python253

is_admin

Mar 18th, 2024
761
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.42 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Filename: is_admin.py
  4. # Version: 1.00
  5. # Author: Jeoi Reqi
  6.  
  7. """
  8. ADMINISTRATOR VERIFICATION TOOL:
  9.  
  10. This script checks if the current user has Elevated Administrator Permissions.
  11.  
  12. Elevated Administrator Permissions are required for certain operations that may affect system settings or files.
  13. These permissions grant the user the highest level of control over the system.
  14.  
  15. Requirements:
  16. - Python 3
  17. - Python Interpreter (With Admin Terminal)
  18. - Windows OS: (Windows 7 - 11)
  19.  
  20. Usage:
  21. 1. Run the script using any Python interpreter as admin or standard.
  22. 2. The script will determine whether it has administrative privileges.
  23. 3. If the script is run with administrative privileges, it will print a message confirming it.
  24. 4. If not, it will print a message indicating the lack of administrative privileges.
  25.  
  26. Note:
  27. - This script utilizes two methods to check for administrative privileges.
  28.  It first attempts to check the user's UID on Unix-like systems and then falls back to
  29.  ctypes.windll.shell32.IsUserAnAdmin() on Windows systems.
  30. - It is important to run the script from a terminal with elevated privileges (e.g., "Run as Administrator" on Windows) to accurately detect administrative privileges.
  31. - This script should be run on Windows systems due to its reliance on ctypes and shell32.
  32. - Compatibility with other operating systems such as Unix-like systems (Linux, macOS) is possible, but may require additional packages and dependencies not listed in this script.
  33.  
  34. """
  35.  
  36. import ctypes
  37. import os
  38. import sys
  39.  
  40. # Function to check if the script is running with administrative privileges
  41. def is_admin():
  42.     """
  43.    Check if the script is running with administrative privileges.
  44.  
  45.    Returns:
  46.        bool: True if the script is running with administrative privileges, False otherwise.
  47.    """
  48.     try:
  49.         # Check if the process is running with elevated privileges on Unix-like systems
  50.         return os.getuid() == 0
  51.     except AttributeError:
  52.         try:
  53.             # Attempt to check administrative privileges on Windows systems using ctypes
  54.             return ctypes.windll.shell32.IsUserAnAdmin() != 0
  55.         except Exception:
  56.             return False
  57.  
  58. if __name__ == "__main__":
  59.     if is_admin():
  60.         print("The script is being run with administrative privileges.")
  61.     else:
  62.         print("The script is not being run with administrative privileges.")
  63.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement