I have the root password saved in a variable. How I can give the root authorization at my program?
This can be an example:
password = "mypassword"
a = BeRoot(password)
if a == True:
print("now you have become root user")
I have the root password saved in a variable. How I can give the root authorization at my program?
This can be an example:
password = "mypassword"
a = BeRoot(password)
if a == True:
print("now you have become root user")
You shouldn't.
A user with sufficient rights should run the program if it need's more power.
Hard-coding the root password directly in your file would be fatal, as everyone could read it. Sure you could restrict reading permissions to admins, but they could run the program with enough right's anyway.
It is impossible to do it in the way you try. Your program is already started under some non-root user and you cannot change it inside this program. You can spawn other process which will be run under the name of root using sudo
.
import os
os.popen("sudo -S somecommand", 'w').write("mypassword")
See also this question (from where the code snipped is taken).
Try configure your python user to do sudo without password
Add in /etc/sudoers
gandalf ALL=(ALL) NOPASSWD: ALL
An replace gandalf with the appropriate username
In python
import os
os.system("sudo echo 'I am a root'")
sudo before any command that you want to run as root