Link to home
Start Free TrialLog in
Avatar of NetRock6
NetRock6Flag for Canada

asked on

Python: Running sudo

Hi ...

Trying running a python script that calls a bash script as sudo.  to get all the standard input/output using the below:
sas_cmd = ['sudo', 'apacheart']
proc = subprocess.Popen(sas_cmd, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

Open in new window

it is not calling the script at all ....
But, if i do the following:
proc = subprocess.Popen(['sudo','/usr/bin/apacheart'])

Open in new window

It calls the script.
Can someone please help to get the std/stout.
Thanks
Avatar of dfke
dfke

Hi,

something like:
from subprocess import Popen, PIPE

p = Popen(['sudo', '/usr/bin/apacheart'], stdin=PIPE, stdout=PIPE, stderr=PIPE)
output, err = p.communicate(b"input data that is passed to subprocess' stdin")
rc = p.returncode

Open in new window

Cheers
Avatar of NetRock6

ASKER

@dfke,
Thanks for your help.
It calls the script but i do not see any stdout.
Any idea why?

Thanks
Did you print the output variable and see nothing?
I print 'rc' and get 0 ...
rc contains the return code which is 0 when the command is successful. You want to print the variable named output if you are using the code that dfke posted above.
You mean:
print (output)
ASKER CERTIFIED SOLUTION
Avatar of dfke
dfke

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Great dfke. Thanks.
This is what I exactly was looking for. Is there a way to open a shell and direct the outputs in that shell.
Thank you again for your great help.
Cheers