This question already has answers here:
Using sudo with Python script
(14 answers)
Closed 3 years ago.
Sorry, I am new to python and I need to make a python script, to run some terminal commands. Normal commands are ok. But, the problem is with sudo commands. For example: I need to run:
sudo -i
So, I tried the following code.
import os
os.system("sudo -i")
This code is executing the command, but in terminal, it is asking for password. So, is it possible to add the password with the command in the script, so that, it won't ask for passwords? For example: if my password is "MYPASS12", how can I add it in the code, so it won't make problem.
-S flag makes sudo read from STDIN
echo mypassword | sudo -S command
But it is better to check if your script is run by root rather than echoing the password.
Related
I am running my python in ubuntu and trying to make a script that communicates with the shell. My script is simple
import os
os.system('sudo su - xyz')
when I run the script and try to login xyz, it asks for the password in my shell. But how do I send the password via the python script and just login.? Is there a better way to do this ?
Piping a hardcoded password into stdin so that you don't have to input it for a command which requires sudoer privileges is overthinking it (in addition to being highly insecure).
Just assume in your Python script that you are root (i.e. don't execute commands with sudo).
import os
os.system('su - xyz')
Then launch your Python script with sudo.
$ sudo python foo.py
Unless you have some weird requirements, this is the most natural way to do it.
You can try piping the password into the commandline string.
NOTE: This is VERY insecure. Anyone has access to your python script will know your password and can get a hold of your system.
import os
password = 'ABCDEFG'
os.system('echo {} | sudo -S su - xyz'.format(password))
EDIT: Note adding echo and -S to sudo, so it can accept password from a pipe
This question already has answers here:
Using sudo with Python script
(14 answers)
Closed 7 years ago.
I have to open a terminal using sudo from python. Consider my password is pass and I need to run a command within script which is sudo critical-stack-intel pull.
I have following small piece of code:
import subprocess
import shlex
command = "sudo critical-stack-intel pull"
popen = subprocess.Popen(shlex.split(command),stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
popen.communicate("pass")
popen.wait()
# print help(p)
If i run the file as python myfile.py, it asks me for password within terminal. This is not what I desire. I want the python to handle the password I gave and run normally. How do I get this done?
EDIT
Using popen.communicate(pass + "\n") along with sudo -S did what i desired.
You can use the -S option of sudo to pass the password via stdin. Most likely, though, it's a better idea to allow sudo access to critical-stack-intel without password using /etc/sudoers.
This question already has an answer here:
Communication between two gnome-terminal sessions
(1 answer)
Closed 7 years ago.
Through python, is there a way I can open gnome-terminal and then send commands to it, which are then run in that window? For example, where I could do something like
terminal.communicate("echo testing")
and the gnome-terminal prints the output? I've seen similar posts using subprocess Popen and communicate, although I wasn't getting the newly opened terminal to run the commands. Thanks for any help
you could do os.system to execute commands, like This
import os
os.system("gnome-terminal -e 'bash -c \"sudo apt-get update; exec bash\"'")
I am making a python script that will not be running in the terminal and as it has to sudo multiple scripts I need to be able to enter the password from the script. What I mean is this: A program that I wrote and only I am going to use starts. It uses a gui box to ask for my sudo password. I give it to it, and then it enters it when needed. I get the textbox part downpath, but how do I enter the password into the sudo program from python?
I found my answer here: https://superuser.com/questions/67765/sudo-with-password-in-one-command-line
It works like this:
$echo <password> | sudo -S <command>
This question already has answers here:
How do I execute a program or call a system command?
(65 answers)
Running a linux command from python
(5 answers)
Closed 8 years ago.
how can I execute the following command in python please
sudo mount --bind /media/networkshare/camera /var/www/media
Technically you could use Python's subprocess module for this (see also this answer):
import subprocess
subprocess.check_call(['sudo', 'mount', '--bind', '/media/networkshare/camera',
'/var/www/media'])
Of course, this will still prompt you for your password. If you don't want it to prompt for a password, then you'll have to setup sudo so that it can execute a single command as root. See the following guide for how to do that:
https://askubuntu.com/questions/155791/how-do-i-sudo-a-command-in-a-script-without-being-asked-for-a-password