HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Becoming root from Python

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
pythonfrombecomingroot

Problem

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.

Solution

The last time I checked (which admittedly has been a while) all major Linux distributions except Ubuntu have a default setup in which sudo is installed, but not configured to be able to start arbitrary applications. So on those your script will fail.

Apart from that I think it's a bad idea to use split like this. This will break if the python file (or the path to it if it was invoked with a path) contains spaces. I'd do it like this instead:

if sudo:
    root = ["/usr/bin/sudo"]
else:
    root = ["/bin/su", "--command"]
command = root + [ sys.argv[0] ]


A further problem is that you're requiring the script to be marked as executable. I think it'd be a better idea to use sys.executable to get the python interpreter and invoke that.

Code Snippets

if sudo:
    root = ["/usr/bin/sudo"]
else:
    root = ["/bin/su", "--command"]
command = root + [ sys.argv[0] ]

Context

StackExchange Code Review Q#2203, answer score: 5

Revisions (0)

No revisions yet.