Posts: 8
Threads: 1
Joined: Aug 2019
(Aug-28-2019, 10:49 PM)Axel_Erfurt Wrote: if the script is just for you, try that
Quote:echo <password> | sudo -S <command>
thanks for your information
i will give it a try
:)
Posts: 2,128
Threads: 11
Joined: May 2017
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
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
root = Sudo()
siemens = Sudo(user = 'siemens' )
print ( 'Hello from Python' )
print ( 'cal' )
root( 'cal' ).wait()
print ( 'whoami' )
root( 'whoami' ).wait()
print ( 'uname -a' )
siemens( 'uname -a' ).wait()
print ( 'whoami' )
siemens( 'whoami' ).wait()
print ()
print ( 'Program ends' )
|
Posts: 8
Threads: 1
Joined: Aug 2019
(Aug-29-2019, 08:29 AM)DeaD_EyE Wrote: 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
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
root = Sudo()
siemens = Sudo(user = 'siemens' )
print ( 'Hello from Python' )
print ( 'cal' )
root( 'cal' ).wait()
print ( 'whoami' )
root( 'whoami' ).wait()
print ( 'uname -a' )
siemens( 'uname -a' ).wait()
print ( 'whoami' )
siemens( 'whoami' ).wait()
print ()
print ( 'Program ends' )
|
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
Posts: 2,128
Threads: 11
Joined: May 2017
Aug-29-2019, 07:24 PM
(This post was last modified: Aug-29-2019, 07:25 PM by DeaD_EyE.)
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.
Posts: 8
Threads: 1
Joined: Aug 2019
(Aug-29-2019, 07:24 PM)DeaD_EyE Wrote: 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.
i get something
i will try to fix it
thanks everyone for the help
i mark this thread as solved
|