Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Linux Script Sudo and "&"
#11
(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
:)
Reply
#12
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!
Reply
#13
(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:
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')

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
Reply
#14
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!
Reply
#15
(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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Is possible to run the python command to call python script on linux? cuten222 6 719 Jan-30-2024, 09:05 PM
Last Post: DeaD_EyE
  [SOLVED] [Linux] Script in cron stops after first run in loop Winfried 2 923 Nov-16-2022, 07:58 PM
Last Post: Winfried
  How to compile a Python script for a Windows / Linux executable? netanelst 2 1,316 May-24-2022, 07:02 AM
Last Post: netanelst
  sudo apt Not Working Dacdiver 11 18,408 Nov-09-2021, 05:48 PM
Last Post: jefsummers
  sudo RyAn 2 1,526 Nov-09-2021, 12:43 PM
Last Post: DeaD_EyE
  cant use ping, sudo or other commands in remote shell script. throwaway34 7 3,579 May-17-2021, 11:29 AM
Last Post: throwaway34
  Error when running script on startup in Linux NoahTheNerd 0 1,950 Mar-07-2021, 04:54 PM
Last Post: NoahTheNerd
  Login to NordVPN on Linux with python script AGreenPig 2 5,958 Feb-09-2021, 10:44 AM
Last Post: AGreenPig
  script works in windows but not in linux ovidius80 2 2,733 Apr-29-2020, 02:10 PM
Last Post: ovidius80
  Methods of running a script on Linux distro Vysero 6 3,864 Aug-21-2018, 09:11 PM
Last Post: heras

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020