Python Check Permissions? - python

Please note, I'm NOT asking about the user running it but rather did he run it as sudo command or not
I have a python script, which I want to obligate the user to run it like this:
sudo python3 script.py
and not like this:
python3 script.py
How can I do verify in run time if it was run correctly or not (if so end program using sys.exit())?

Check out this topic:
What is the best way for checking if the user of a script has root-like privileges?
It has a bunch of solutions that you might find helpful.

Related

How to implement multiple commands with root permissions with only one password prompt?

I'm working on a GUI applications which calls two system commands respectively.
Those two commands require root permissions to be executed.
The first approach I made, is to call gksu <command_1>, then gksu <command_2>.
This works fine but the user must enter his password twice respectively, and I believe this is not good idea from a UX perspective.
I tried to call gksu with the first command and sudo with the second, but I get this error:
sudo: no tty present and no askpass program specified
So I tried to separate those command in a python file and call a command from the original file that looks like gksu python3 commands.py.
I'm not sure whether this would be executed after I release a compiled version of the whole project, as I intend to use pyinstaller --onefile on it !
So, what I need exactly is to make the app be able to run a specific script with super user privileges considering the final state of the app which would be an executable-binary file and that doesn't include running the whole app with root permissions .
Thanks to Itz Wam, His answer guided me to the correct solution which is Using pkexec instead of gksu like this:
pkexec bash -c "command_1;command_2"
You could execute this :
gksu -- bash -c 'command1; command2; command3'
It will ask your password one time and execute the 3 commands as root
Source : https://askubuntu.com/questions/183608/gksudo-2-commands-with-one-pw-entry

How to execute a Python script in Node.js with sudo privillege

The script create some files in directories which need sudo permissions and executes few command that also need sudo privillage.
I want to execute that script giving sudo privillage.
Is there any way to do that ?
I am trying to execute it with python-shell module as well as spawn child process.
I never got any answer on it, So I researched it on my own. The besy way to run any shell command or script is by using node-cmd moudle. It works soo bright .
Just run the node script with sudo privillege, and you are good to go .
It's bad practice to give sudo, as a hacker could do anything if there is any security issues. You could give the user witch runs the web server the permission to do the task your task is intending to do.
In general try to avoid root whenever you can.

Issues with running a Python program with Cron

I am trying to run a Python script called probemon.py in cron (crontab -e) and cannot get it to work. The path to the file is /home/pi/probemon.py and this must be run with the sudo command usually (i.e. sudo python probemon.py). I have tried many methods, including:
52 23 * * * sudo python /home/pi/probemon/probemon.py
and yet nothing works. Any ideas about how to do this?
Try adding the command to the sudo-users crontab instead of trying to run it with a sudo from a normal users crontab. I think what happends is that "sudo ..." will ask for the sudo-password, and wait in this stage forever, since noone is providing one.

is there a way to use fabric run() and sudo() at a time?

I'm trying to automate a test in python 2.7 (in eclipse on linux ubuntu 12.04).
The test checks configurations on another pc, so I'm using fabric for the ssh connection.
I need to execute a script:
run("cd somepath && ./execute_script.sh")
The problem is that my script needs a sudo to run, but changing the command to this:
sudo("cd somepath && ./execute_script.sh")
does not work since "cd" doesn't work in combination with sudo.
I also cannot split the command in two parts, because that would create 2 shells, and the second one would forget the path I've been going to in the first one.
If I do it like this:
run("cd somepath && sudo ./execute_script.sh")
the test wouldn't work completely automatic since you would have to enter the password at a time.
Is there a way to some sort of combine run() and sudo()?
How about:
from fabric.api import cd,sudo
with cd('somepath'):
sudo('./execute_script.sh')

Running commands from within python that need root access

I have been playing around with subprocess lately. As I do more and more; I find myself needing root access. I was wondering if there is an easy way to enter the root password for a command that needs it with subprocess module. So when I am prompted for the password my script and provide it and run the command. I know this is bad practice by where the code will be running is sandboxed and separate from the rest of the system; I also dont want to be running as root.
I would really appreciate small example if possible. I know you can do this with expect, but i am looking something more python centric. I know pexpect exsists but its a bit overkill for this simple task.
Thanks.
It would probably be best to leverage sudo for the user running the Python program. You can specify specific commands and arguments that can be run from sudo without requiring a password. Here is an example:
There are many approaches but I prefer the one that assigns command sets to groups. So let's say we want to create a group to allow people to run tcpdump as root. So let's call that group tcpdumpers.
First you would create a group called tcpdumpers. Then modify /etc/sudoers (using the visudo command):
# Command alias for tcpdump
Cmnd_Alias TCPDUMP = /usr/sbin/tcpdump
# This is the group that is allowed to run tcpdump as root with no password prompt
%tcpdumpers ALL=(ALL) NOPASSWD: TCPDUMP
Now any user added to the tcpdumpers group will be able to run tcpdump like this:
% sudo tcpdump
From there you could easily run this command as a subprocess.
This eliminates the need to hard-code the root password into your program code, and it enables granular control over who can run what with root privileges on your system.

Categories

Resources