How & where to best retrieve sudo password via a native GUI on a macOS Python-based app - (while maintaining an interactive output stream (stdout))

给你一囗甜甜゛ 提交于 2019-12-06 10:40:00

My question thus becomes: How can I make sure to use macOS native GUI to ask for the sudo password, thus preventing having to store it in a variable, while still maintaining the possibility to catch the stdout from the privileged script in an interactive / real-time stream.

I have found a solution myself, using a named pipe (os.mkfifo()).

That way, you can have 2 python scripts communicate with each other while 1 of them is launched with privileged rights via osascript (meaning: you get a native GUI window that asks for the users sudo password).

Working solution:

mainscript.py

import os
from pathlib import Path
import shlex
import subprocess
import sys
from threading import Thread
import time

class LaunchDeletionProcess(Thread):

    def __init__(self):

        Thread.__init__(self)

    def run(self):

        launch_command = r"""/usr/bin/osascript -e 'do shell script "/usr/local/bin/python3.6 -u /path/to/priviliged_script.py" with prompt "Sart Deletion Process " with administrator privileges'"""
        split_command = shlex.split(launch_command)

        print("Thread 1 started")
        testprogram = subprocess.Popen(split_command)
        testprogram.wait()
        print("Thread1 Finished")

class ReadStatus(Thread):

    def __init__(self):

        Thread.__init__(self)

    def run(self):

        while not os.path.exists(os.path.expanduser("~/p1")):
            time.sleep(0.1)

        print("Thread 2 started")

        self.wfPath = os.path.expanduser("~/p1")

        rp = open(self.wfPath, 'r')
        response = rp.read()

        self.try_pipe(response)

    def try_pipe(self, response):
        rp = open(self.wfPath, 'r')
        response = rp.read()
        print("Receiving response: ", response)
        rp.close()
        if response == str(self.nr_of_steps-1):
            print("Got to end")
            os.remove(os.path.expanduser("~/p1"))
        else:
            time.sleep(1)
            self.try_pipe(response)

if __name__ == "__main__":

    thread1 = LaunchDeletionProcess()
    thread2 = ReadStatus()
    thread1.start()
    thread2.start()

priviliged_script.py

import os
import time
import random

wfPath = os.path.expanduser("~/p1")

try:

    os.mkfifo(wfPath)

except OSError:

    print("error")
    pass

result = 10

nr = 0 

while nr < result:

    random_nr = random.random()

    wp = open(wfPath, 'w')
    print("writing new number: ", random_nr)
    wp.write("Number: " + str(random_nr))       
    wp.close()

    time.sleep(1)
    nr += 1

wp = open(wfPath, 'w')
wp.write("end")     
wp.close()
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!