Last active
February 10, 2021 10:55
-
-
Save RDCH106/fdd419ef7dd803932b16056aab1d2300 to your computer and use it in GitHub Desktop.
Cross-platform script to check admin rights in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ctypes, os | |
from sys import exit | |
def is_admin(): | |
is_admin = False | |
is_win = False | |
try: | |
is_admin = os.getuid() == 0 | |
except AttributeError: | |
is_admin = ctypes.windll.shell32.IsUserAnAdmin() != 0 | |
is_win = True | |
print ("Admin privileges: {}".format(is_admin)) | |
return is_admin, is_win | |
if __name__ == "__main__": | |
is_admin, is_win = is_admin() | |
# Converting boolean to integer | |
ret = int(is_admin == False) | |
if is_win: | |
exit(ret) | |
else: | |
exit(ret * -1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
pwd | |
cd `dirname $0` | |
SCRIPTDIR=`pwd` | |
pwd | |
python is_admin.py | |
if [ $? -eq 0 ] | |
then | |
# [your_executable] | |
else | |
echo Exit Code is $? | |
echo. | |
echo Admin privileges required | |
echo Run it again as Administrator | |
echo. | |
read -rsp $'Press any key to continue...\n' -n 1 key | |
exit $? | |
fi | |
read -rsp $'Press any key to continue...\n' -n 1 key |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@echo off | |
cd | |
cd /D "%~dp0" | |
cd | |
python is_admin.py | |
if errorlevel 1 ( | |
echo Exit Code is %errorlevel% | |
echo. | |
echo Admin privileges required | |
echo Run it again as Administrator | |
echo. | |
pause | |
exit /b %errorlevel% | |
) | |
#REM [your_executable].exe | |
pause |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Explicación detallada:
https://mascandobits.es/programacion/comprobar-permisos-de-administrador-en-una-ejecucion-estilo-%f0%9f%90%8d-python/