Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to communicate with a sub subprocess with subprocess.Popen?

I'm trying to write a python script that packages our software. This script needs to build our product, and package it. Currently we have other scripts that do each piece individually which include csh, and perl scripts. One such script is run like:

sudo mod args

where mod is a perl script; so in python I would do

proc = Popen(['sudo', 'mod', '-p', '-c', 'noresource', '-u', 'dtt', '-Q'], stderr=PIPE, stdout=PIPE, stdin=PIPE)

The problem is that this mod script needs a few questions answered. For this I thought that the traditional

(stdout, stderr) = proc.communicate(input='y')

would work. I don't think it's working because the process that Popen is controlling is sudo, not the mod script that is asking the question. Is there any way to communicate with the mod script and still run it through sudo?

like image 921
darrickc Avatar asked Oct 30 '08 15:10

darrickc


1 Answers

I would choose to go with Pexpect.

import pexpect
child = pexpect.spawn ('sudo mod -p -c noresource -u dtt -Q')
child.expect ('First question:')
child.sendline ('Y')
child.expect ('Second question:')
child.sendline ('Yup')
like image 173
miya Avatar answered Oct 12 '22 09:10

miya