# Executing another cmdl-app with ruby and typing in a password?

**URL:** <https://www.ruby-forum.com/t/executing-another-cmdl-app-with-ruby-and-typing-in-a-password/120512>\
**Category:** Ruby\
**Created:** [November 17, 2007, 7:25pm UTC](https://www.ruby-forum.com/t/executing-another-cmdl-app-with-ruby-and-typing-in-a-password/120512 "2007-11-17T19:25:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kazaam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/b4bc9f/32.png) [@kazaam](https://www.ruby-forum.com/u/kazaam)\
**Post date:** [November 17, 2007, 7:25pm UTC](https://www.ruby-forum.com/t/executing-another-cmdl-app-with-ruby-and-typing-in-a-password/120512/1 "2007-11-17T19:25:55Z")

</div>

Hi,  
I’m writing a gui with ruby and glade and so far it works well. Now the  
program I write a gui for has an option which wants you to type a  
password in the console. I start it this way:  
system(“anotherapp --encrypt”)  
then in the commandline happens something until it gets printed:  
Password:  
And it wants me to type in a password. I want to send this password from  
my gui to the cmdlline. But how can I get the Password in it? I tried to  
pipe it with:  
system(“echo ‘pwd’ | anotherapp --encrypt”)  
but this doesn’t work at all.

Any idea how to do this?

greets

---

<div class="post-metadata">

**Author:** ![kazaam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/b4bc9f/32.png) [@kazaam](https://www.ruby-forum.com/u/kazaam)\
**Post date:** [November 17, 2007, 8:14pm UTC](https://www.ruby-forum.com/t/executing-another-cmdl-app-with-ruby-and-typing-in-a-password/120512/2 "2007-11-17T20:14:36Z")

</div>

On Nov 17, 2007, at 11:25 AM, kazaam wrote:

> Any idea how to do this?

this has nothing to do with ruby per se: the program requires a pty,  
the ruby distribution has examples, including an ‘expect’ based  
example, in ext/pty/expect\_sample.rb.

a @ [http://codeforpeople.com/](http://codeforpeople.com/)

---

<div class="post-metadata">

**Author:** ![kazaam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/b4bc9f/32.png) [@kazaam](https://www.ruby-forum.com/u/kazaam)\
**Post date:** [November 18, 2007, 8:45am UTC](https://www.ruby-forum.com/t/executing-another-cmdl-app-with-ruby-and-typing-in-a-password/120512/3 "2007-11-18T08:45:16Z")

</div>

Thanks for pointing me in the right direction. I now wrote a little loop  
to do this work:

require ‘pty’  
require ‘expect’

PTY.spawn(“ruby test.rb”) do |read\_t,write\_t,pid|  
write\_t.sync = true  
read\_t.expect(/^enter passphrase.\*/) {write\_t.print “123456”}  
end

But I have a problem. How to find out if the spawned process executed  
with or without errors? I mean in this example “ruby test.rb”. If I use  
system I could do:  
puts “error” if not system(“ruby test.rb”)

But PTY.spawn always just returns ‘nil’. Is there any way I can check  
for correct execution?

---

<div class="post-metadata">

**Author:** ![kazaam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/b4bc9f/32.png) [@kazaam](https://www.ruby-forum.com/u/kazaam)\
**Post date:** [November 18, 2007, 6:02pm UTC](https://www.ruby-forum.com/t/executing-another-cmdl-app-with-ruby-and-typing-in-a-password/120512/4 "2007-11-18T18:02:47Z")

</div>

On Nov 18, 2007, at 12:45 AM, kazaam wrote:

> kazaam [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> cfp:~ \> cat a.rb  
> require “yaml”  
> require “pty”

module PTY  
def self.execute \*a, &b  
begin  
spawn \*a, &b  
0  
rescue =\> e  
e.status rescue raise  
end  
end  
end

status =  
PTY.execute “ruby” do |r,w,pid|  
w.sync = true  
w.puts “42”  
y ‘gets’ =\> r.gets.strip.to\_i  
end  
y ‘status’ =\> status

## cfp:~ \> ruby a.rb

## gets: 42

status: 0

a @ [http://codeforpeople.com/](http://codeforpeople.com/)
