So, this is kind of confusing but essentially I'm using Django and I want to instantiate a subprocess to run a perl script. I've read that this can be done with
arg = "/some/file/path/"
pipe = subprocess.Popen(["./uireplace", arg], stdin=subprocess.PIPE)
which works when I call it in the appropriate function in views.py but the script requires a sudo. I then call this
pipe = subprocess.Popen(["sudo","./uireplace", arg], stdin=subprocess.PIPE)
which works when I run it in python from a terminal but this doesn't work when it's called by a random user on the web. Is there any way to be able to automatically enter in a username and password for that sudo? The issue is that this can't be done with a prompt so it simply fails.
Solves this problem on the OS level. Giving any user from the web the right to use sudo does not sound right. Just make ./uireplace executable. There are lots of options for chmod to fine tune this.
Related
I am trying to run this command sudo nmap -sP -n with python subprocess library, my goal is creating script when I run the command the script will read the password from a file and add it to the subprocess and execute the command. The problem is that I allways have to write password to execute the command.
I have tried this, but it did not work for me.
p = subprocess.Popen(["sudo", "nmap", "-sP", "-n", network], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
p.stdin.write(bytes("Mypassword", "utf-8"))
I found some solutions here Use subprocess to send a password, I tried all of them but it did not work for me. Any idea how can I solve the problem?
The sudo man page specifies:
-S, --stdin
Write the prompt to the standard error and read the password from the stan‐
dard input instead of using the terminal device. The password must be fol‐
lowed by a newline character.
Running the following code should fix your issue:
p = subprocess.Popen(["sudo", "-S", "cmd"], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
p.stdin.write("password\n")
Obviously, make sure you give the right password or this won't work. Also, don't forget to add \n at the end.
As an alternative, you can run nmap as an unprivileged user. This will allow you to use nmap --privileged ... which doesn't require a password. As specified in the link though, make sure you understand the security concerns to make sure it isn't an issue for your use case.
I'm new in a company for IT and very few people here know Python so I can't ask then for help.
The problem: I need to create a script in Python that connects via ssh from my VM to my client server, after I access with my script I need to find a log file and search for a few data.
I tested my script within my Windows with a copy of that file and it searched everything that I need. However, I don't know how to do that connection via SSH.
I tried like this but I don't know where to start:
from subprocess import Popen, PIPE
import sys
ssh = subprocess.check_output(['ssh', 'my_server', 'password'], shell = True)
ssh.stdin.write("cd /path/")
ssh.stdin.write("cat file | grep err|error")
This generates a error "name 'subprocess' is not defined".
I don't understand how to use the subprocess nor how to begin to develop the solution.
Note: I can't use Paramiko because I don't have permission to install packages via pip or download the package manually.
You didn't import subprocess itself so you can't refer to it.
check_output simply runs a process and waits for it to finish, so you can't use that to run a process you want to interact with. But there is nothing interactive here, so let's use that actually.
The first argument to subprocess.Popen() and friends is either a string for the shell to parse, with shell=True; or a list of token passed directly to exec with no shell involved. (On some platforms, passing a list of tokens with shell=True actually happens to work, but this is coincidental, and could change in a future version of Python.)
ssh myhost password will try to run the command password on myhost so that's not what you want. Probably you should simply set things up for passwordless SSH in the first place.
... But you can use this syntax to run the commands in one go; just pass the shell commands to ssh as a string.
from subprocess import check_output
#import sys # Remove unused import
result = check_output(['ssh', 'my_server',
# Fix quoting and Useless Use of Cat, and pointless cd
"grep 'err|error' /path/file"])
I'm writing a Python script that will run on a Raspberry that will read the temperature from a sensor and log to Thingspeak. I have this working with a bash script but wan't to do it with Python since it will be easier to manipulate and check the read values. The sensor reading is done with a library called loldht. I was trying to do it like this:
from subprocess import STDOUT, check_output
output = check_output("/home/pi/bin/lol_dht22/loldht", timeout=10)
The problem is that I have to run the library with sudo to be able to access the pins. I will run the script as a cron. Is it possible to run this with sudo?
Or could I create a bash script that executes 'sudo loldht' and then run the bash script from python?
I will run the script as a cron. Is it possible to run this with sudo?
You can put python script.py in the cron of a user with sufficient privileges (e.g. root or a user with permissions to files and devices in question)
I don't know which OS you're using, but if Raspbian is close to Debian, there is no need for sudo or root, just use a user with sufficient permissions.
It seems I can also do this check_output check_output(["sudo", "/home/pi/bin/lol_dht22/loldht", "7"], timeout=10)
Sure but the unix user that's going to invoke that Python script will need the sudo privilege (Otherwise can't call the sudo from subprocess). In which case you might as well do as above, run the cron from a user with the required permissions.
You can run sudo commands with cron. Just use sudo crontab -e to set the cron and it should work fine.
You should very careful with running things as root. Since root has access to everything, a simple error can potentially render the system unusable.
The proper way to have access to the hardware as a normal user is to change the permissions on the required device files.
It seems that the utility you mention uses the WiringPi library. Some digging in the source code indicates that it uses the /dev/gpiomem (or /dev/mem) devices.
On raspbian, device permissions are set with udev. See here and also here.
You could give every user access to /dev/gpiomem and other gpio devices by creating a file e.g. /etc/udev/rules.d/local.rules and putting the following text in it:
ACTION=="add", KERNEL=="gpio*", MODE="0666"
ACTION=="add", KERNEL=="i2c-[0-9]*", MODE="0666"
The first line makes the gpio devices available, the second one I2C devices.
Short version:
How to execute a linux command which requires input after execution using Python?
Long version:
I am building some fancy website stuff using Python to give my SVN server a way to be managed easier. (I can't remember all the linux commands)
So I want to create, delete and edit repo's and users using a webpage. I just came to the problem I do not know how to execute the following command using Python:
sudo htdigest /etc/apache2/dav_svn.htdigest "Subversion Repo" [username]
Well I know how to execute the command with os.system() or subprocess.Popen(), but the problem is that once that command is executed it asks to enter a password twice before continuing. Using multiple calls using os.system() or subprocess.Popen() won't work since they just create a new shell.
Is there a way in Python to let an argument be used once it is required?
It all depends, you can either use popen and handle bidirectional communication or if you are just waiting for known prompts, I would use pexpect:
So assuming, you wanted to spawn a program called myprocess and waited for the password prompt that had a > (greater than sign):
import pexpect
child = pexpect.spawn('myprocess')
child.expect('>')
child.sendline(password_var)
child.expect('>')
child.sendline(password_var)
i want to run and control PSFTP from a Python script in order to get log files from a UNIX box onto my Windows machine.
I can start up PSFTP and log in but when i try to run a command remotely such as 'cd' it isn't recognised by PSFTP and is just run in the terminal when i close PSFTP.
The code which i am trying to run is as follows:
import os
os.system("<directory> -l <username> -pw <password>" )
os.system("cd <anotherDirectory>")
i was just wondering if this is actually possible. Or if there is a better way to do this in Python.
Thanks.
You'll need to run PSFTP as a subprocess and speak directly with the process. os.system spawns a separate subshell each time it's invoked so it doesn't work like typing commands sequentially into a command prompt window. Take a look at the documentation for the standard Python subprocess module. You should be able to accomplish your goal from there. Alternatively, there are a few Python SSH packages available such as paramiko and Twisted. If you're already happy with PSFTP, I'd definitely stick with trying to make it work first though.
Subprocess module hint:
# The following line spawns the psftp process and binds its standard input
# to p.stdin and its standard output to p.stdout
p = subprocess.Popen('psftp -l testuser -pw testpass'.split(),
stdin=subprocess.PIPE, stdout=subprocess.PIPE)
# Send the 'cd some_directory' command to the process as if a user were
# typing it at the command line
p.stdin.write('cd some_directory\n')
This has sort of been answered in: SFTP in Python? (platform independent)
http://www.lag.net/paramiko/
The advantage to the pure python approach is that you don't always need psftp installed.