0

example:

from subprocess import *
proc = Popen(['cat','-'], stdin = PIPE, stdout = PIPE)
msg = 'Through STDIN TO STDOUT' 
stdout_value = proc.communicate(msg)[0] 
print 'stdout:', stdout_value

output : stdout: Through STDIN TO STDOUT

in this example I have passed the msg string to the called program(cat -) as stdin, what if the called program was the (passwd) command, is it possible to pass two stdin([password value] and [verification password value]) to it ?

2 Answers 2

1

Yes, it is possible to pass two lines of input:

msg = 'line1\nline2\n'
Sign up to request clarification or add additional context in comments.

Comments

0

Yes and no. Assuming passwd reads from standard input, you would simply write two lines to proc's standard input, either by setting msg to something like mypass\nmypass\n, or simply making two calls to proc.communicate. However, for security reasons passwd reads directly from the terminal, unless you specifically tell it to read from standard input with passwd --stdin (where available).

4 Comments

On what system does passwd read from /dev/tty? On my Ubuntu 14.04 LTS system, it reads from stdin.
I might be misinterpreting the need for the --stdin option that passwd takes on the Scientific Linux box I'm looking at. It does seem to read from standard input by default, based on the result of passwd < /dev/null.
I'm not doubting you, I am geniunely curious about differences between systems.
No, I made an unwarranted assumption based on the existence of --stdin (and by analogy to sudo or ssh--something reads directly from the terminal, I'm pretty sure). I'm not sure what context requires --stdin.

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.