open terminal with sudo python [duplicate] - python

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.

Related

How to make python script to run sudo command [duplicate]

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.

Is there a way to run several consecutive lines in the system command window using python [duplicate]

This question already has answers here:
Execute Commands Sequentially in Python?
(5 answers)
Closed 3 years ago.
I have the following line of commands used in sequence from the command window.
cd path 1 && file.bat && cd path 2 && printfNav path3 > Testing.txt
Is it possible to have this done through python?
You are looking for the os.system() method. You will need to import os into your python program and then using this method you can send commands to the terminal. The method takes in a string and acts as if you typed that into your terminal. For example os.system('pwd') would cause the terminal to print out your current working directory, and so on so forth with other commands.

Python - communicating with gnome-terminal [duplicate]

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\"'")

how to execute shell command from python [duplicate]

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

Running shell commands within python interpreter [duplicate]

This question already has answers here:
How do I execute a program or call a system command?
(65 answers)
Closed 8 years ago.
Is there a simple method for calling shell command line arguments (like ls or pwd) from within python interpreter?
In plain python, you need to use something along the lines of this:
from subprocess import check_output
check_output("ls", shell=True)
In IPython, you can run either of those commands or a general shell command by starting off with !. For example
! echo "Hello, world!" > /tmp/Hello.txt
If you're using python interactively, you would almost certainly be happier with IPython.
If you meant to use the Python shell interactively while being able to call commands (ls, pwd, ...) check out iPython.

Categories

Resources