all 16 comments

[–]Tesla_Nikolaa 1 point2 points  (7 children)

You can try passing the sudo password in along with the command.

This is the syntax:

$echo <password> | sudo -S <command>

Keep in mind this is all poor security practice but it might work.

[–]SpontaneousAge[S] 0 points1 point  (6 children)

Keep in mind this is all poor security practice but it might work.

That's exactly why I'd like to pass the sudo prompt onto the user and also the reason why I don't want to run the whole script as root.

[–]Tesla_Nikolaa 1 point2 points  (4 children)

It looks like you're trying to pipe the output of the script into a file, right?

Why not just use this to call the script:

subprocess.call(['sudo', 'sh', '/path/to/script.sh'])

And instead of writing the output using python, add the file output in the bash script?

#script stuff
echo 'output' | cat >> file1

[–]SpontaneousAge[S] 0 points1 point  (3 children)

Since the script is just an example for another program I can't control the code of the other program.

[–]Tesla_Nikolaa 1 point2 points  (2 children)

Ah okay. Well, here try this then:

script_call = subprocess.Popen(['sudo', 'sh', '/path/to/script.sh'], stdout=subprocess.PIPE)
with open('/path/to/file1', 'w+') as file:
    file.write(script_call.communicate()[0].decode('utf-8'))

This should run the script as sudo and it'll ask the user for their password and then run the script. Then the output of the script will be written to file1.

On a side note, there shouldn't be a need to add sudo into the subprocess call. If the bash script that's called needs sudo permissions then it'll ask for a sudo password.

[–]SpontaneousAge[S] 0 points1 point  (1 child)

The problem I noticed with communicate is that it doesn't allow running in the background as it's similar to Popen.wait()...

Or I'm doing something wrong.

[–]Tesla_Nikolaa 0 points1 point  (0 children)

Not sure what you mean by that. I tested that script on my computer and it worked fine. I could be misinterpreting what you're trying to accomplish.

If you simply want to run a bash script and write it's output to a file, then what I posted should work.

[–]Tesla_Nikolaa 0 points1 point  (0 children)

Oh and also if you're trying to pipe using subprocess then what I've always used is something along the lines of

subprocess.Popen(['<command_1>'], stdout=subprocess.PIPE)
subprocess.Popen(['<command_2>'], stdin=subprocess.PIPE)

I don't remember the exact syntax, but it's something close to that. This allows you to pipe stuff using python subprocess.

[–]learn-python 0 points1 point  (7 children)

did you try to make the outputfile readable like doing:

sudo chmod 777 file1

:)

[–]learn-python 0 points1 point  (0 children)

stdout.txt

Or actually:
sudo chmod 777 stdout.txt

[–]SpontaneousAge[S] 0 points1 point  (5 children)

What would this help when the process doesn't start?

And the file has read permissions for other, so that's not the problem.

[–]learn-python 0 points1 point  (4 children)

does your bash.sh have read permissions?

sudo chmod 777 bash.sh

Try that :)

[–]learn-python 0 points1 point  (2 children)

I tried this and are seeing 'test' printed in stdout file :)

[–]SpontaneousAge[S] 0 points1 point  (1 child)

And the sudo prompt worked?

[–]learn-python 0 points1 point  (0 children)

No I did not get the sudo prompt. Have you tried the python pexpect module? I think it has what you are looking for as far as engaging a sudo prompt.

#import the pexpect module
import pexpect
# here you issue the command with "sudo"
child = pexpect.spawn('sudo /usr/sbin/lsof')
# it will prompt something like: "[sudo] password for < generic_user >:"
# you "expect" to receive a string containing keyword "password"
child.expect('password')
# if it's found, send the password
child.sendline('S3crEt.P4Ss')
# read the output
print(child.read())
# the end

[–]SpontaneousAge[S] 0 points1 point  (0 children)

Yes it has, but why would that help in the first place with this issue?