How to execute os methods. * How to root?

Is it possible to request root pw without storing a script in my memory and run some os commands. * how root?

My script

  • scans some folders and files to see if it can complete the task
  • makes some changes to / etc / ...
  • creates a folder and files that should belong to the user who ran the script

(1) can be run as a regular user. I can do (2) using a sudoing script, but then the folder and files in (3) will be root.

The problem is that I use a lot of os.makedirs, os.symlink, etc., which prevents me from making it workable as a regular user.

Tanks 2 all for offers

The solution so far:

# do all in sudo
os.chown(folder, int(os.getenv('SUDO_UID')), int(os.getenv('SUDO_GID')))

thanks to the gnibbler hint.

+3
5

gnibbler os.chown. , , sudo. SUDO_*:

os.chown, (some_path, int(os.getenv('SUDO_UID')), int(os.getenv('SUDO_GID')))

3 , , .

+1

, (2) script, script2.py, script sudo script2.py popen?

, (2) root.

+2

yourscript.py:

run_part_1()
subprocess.call(['sudo', sys.executable, 'part2.py'])
run_part_3()

part2.py:

run_part_2()
+2
+1

script root (3) os.chmod os.chown.

script root sudo, script sudo, .

, /. -, UID/ . os.chown, , os.chmod, . chown/chmod os, : http://docs.python.org/library/os.html

: . , .

+1
source

Source: https://habr.com/ru/post/1721275/


All Articles