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

K

kazaam

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
 
K

kazaam

Thanks for pointing me in the right direction. I now wrote a little loop todo 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 Icould 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?


--
kazaam <[email protected]>

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)

iD8DBQFHP+zPhpGHCq8B1fYRAhrdAKDfAHatYuaVM92yfUR5zpDR9o8PEwCg8Ova
S5vNkf4hUUGdYMMDie5AIY4=
=yNzK
-----END PGP SIGNATURE-----
 
A

ara.t.howard

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?

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,270
Messages
2,571,353
Members
48,038
Latest member
HunterDela

Latest Threads

Top