In order to avoid the user having to explicitly prefix a script with sudo
or su --command
, I wrote the following:
import sys
import os
if os.getuid():
root = "/usr/bin/sudo"
if not os.path.exists("/usr/bin/sudo"):
root = "/bin/su --command"
command = "{} {}".format(root, sys.argv[0])
command = command.split()
retcode = subprocess.call(command)
if retcode:
print("something wrong happened")
else:
action_that_needs_admin_rights()
It feels like a hack to me, so am looking forward to better approaches.