Aug-29-2019, 06:32 AM
Linux Script Sudo and "&"
Linux Script Sudo and "&"
|
Aug-29-2019, 08:29 AM
I think you want to look how /etc/soduers work.
You can allow all/single/list of commands for a user/group. You can define, that the user needs no password to execute a specified program as root. Another solution could be, that you run your program not as root, but the part, where the root access is needed, you can start the external program with sudo (e.g. with subprocess.Popen). This will make your application bit more safe. Maybe this is possible solution: import getpass import shlex from functools import wraps from subprocess import Popen, PIPE, DEVNULL class Sudo: def __init__(self, user='root', quiet=False): self.password = None self.user = user self.cmd = ['sudo', '-S', '-u', user] self.quiet = quiet def authorize(self): self.password = getpass.getpass(f'Password for {self.user}: ') def run(self, cmd): if not self.password: self.authorize() cmd = self.cmd + shlex.split(cmd) if self.quiet: proc = Popen(cmd, stdin=PIPE, stdout=DEVNULL, stderr=DEVNULL, encoding='utf8') else: proc = Popen(cmd, stdin=PIPE, encoding='utf8') proc.stdin.write(self.password) proc.stdin.write('\n') return proc __call__ = run # allows to call the instance root = Sudo() siemens = Sudo(user='siemens') print('Hello from Python') # the wait blocks until the program ends print('cal') root('cal').wait() print('whoami') root('whoami').wait() # second one does not require the password, because it's stored # if you don't use the blocking method wait, the order of output is not preserved print('uname -a') siemens('uname -a').wait() print('whoami') siemens('whoami').wait() print() print('Program ends')
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Aug-29-2019, 07:16 PM
(Aug-29-2019, 08:29 AM)DeaD_EyE Wrote: I think you want to look how /etc/soduers work. ok i have to say cause my noob state on python programming your code is excellent i will try it tomorrow by doing some modification on my own program give me some time and i will reply back thanks again
You should experiment with this code. I guess you'll get unexpected results.
It depends on the program you want to start. The direct use of stdin, stdut and stderr is a little bit tricky and if you're doing it wrong, it blocks forever.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Aug-30-2019, 08:02 AM
(Aug-29-2019, 07:24 PM)DeaD_EyE Wrote: You should experiment with this code. I guess you'll get unexpected results. i get something i will try to fix it thanks everyone for the help i mark this thread as solved |
|
Users browsing this thread: 2 Guest(s)