3

Consider a command like

yum install boto

When I execute in terminal, to proceed is asks me for yes/no

Can I respond to it in python like

os.system("yum install boto")

Next "Yes" is to be passed to terminal through the same python code so that it installs. Well, I dont think this works. If it is written after tha above statement

os.system("yes")

Please tell me if this is possible?

6

2 Answers 2

7

You can use subprocess.Popen and write to stdin, you need the -S flag for sudo then just the rest of the commands.

from subprocess import Popen, PIPE
import getpass

pwd = getpass.getpass()
proc = Popen(['sudo', '-S', rest of commands ],stdout=PIPE, stdin=PIPE, stderr=PIPE,universal_newlines=True)
proc.stdin.write("{}\n".format(pwd))
out,err = proc.communicate(input="{}\n".format("yes"))
Sign up to request clarification or add additional context in comments.

1 Comment

Since proc.communicate(input='...') writes that input to proc.stdin, what's the point of the separate proc.stdin.write call? You could put both pwd and the string yes into one place.
1

You can add a pipe and do

os.system("yes | yum install boto")

it will repeat yes until the command is done

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.