I have a problem. I am writing a piece of software, which is required to perform an operation which requires the user to be in sudo mode. running 'sudo python filename.py' isn't an option, which leads me to my question. Is there a way of changing to sudo half way through a python script, security isn't an issue as the user will know the sudo password the program should run in the following way to illustrate the issue
My problem lies in step 3, any pointers or frameworks you could suggest would be of great help.
Cheers
Chris
It is better to run as little of the program as possible with elevated privileges. You can run the small part that needs more privilege via the subprocess.call()
function, e.g.
import subprocess returncode = subprocess.call(["/usr/bin/sudo", "/usr/bin/id"])
Don't try and make yourself sudo just check if you are and error if your not
class NotSudo(Exception): pass if os.getuid() != 0: raise NotSudo("This program is not run as sudo or elevated this it will not work")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With