How to check that my python script is running under Administrator rights (sudo) under BSD-like OS? Need to display user-friendly warning in order it is executed without admin rights.
|
feedback
|
|
How about this? Check if uid == 0 : [kbrandt@kbrandt-admin: ~] python -c 'import os; print os.getuid()' 196677 [kbrandt@kbrandt-admin: ~] sudo python -c 'import os; print os.getuid()' 0 | |||||
feedback
|
|
In Unix-like OSes, the only "Administrator" is the root user with uid=0. (There may be more users with the same uid, and they all will have the same privileges.) So probably the best way is:
| |||
|
feedback
|
|
How about that one:
| |||
|
feedback
|
|
Don't be tempted to match a username against the string "root". Generally you will either have to provide less efficient callouts to obtain the textual representation of the UID or you will be relying on environment variables which may not be so trustworthy. | |||
|
feedback
|