# How to expect eof with expect+pty

**URL:** <https://www.ruby-forum.com/t/how-to-expect-eof-with-expect-pty/79947>\
**Category:** Ruby\
**Created:** [December 20, 2006, 7:06pm UTC](https://www.ruby-forum.com/t/how-to-expect-eof-with-expect-pty/79947 "2006-12-20T19:06:10Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![simonsaid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b5e925/32.png) [@simonsaid](https://www.ruby-forum.com/u/simonsaid)\
**Post date:** [December 20, 2006, 7:06pm UTC](https://www.ruby-forum.com/t/how-to-expect-eof-with-expect-pty/79947/1 "2006-12-20T19:06:10Z")

</div>

using gnupg to encrypt a file with only a passphrase.

prompt\> echo ‘hello world’ \>\> test  
prompt\> gpg -c test  
Enter passphrase:  
Repeat passphrase:  
prompt\>

note: I have not shown the many backspaces.

however if there already exists a ‘test.gpg’ file  
then there is third question.

prompt\> echo ‘hello world’ \>\> test  
prompt\> gpg -c test  
Enter passphrase:  
Repeat passphrase:  
File `test.gpg’ exists. Overwrite? (y/N)

I want to listen for:  
'Enter passphrase: ’  
'Repeat passphrase: ’  
eof?

In case I get something else than eof? then  
I want to raise an error.

–  
Simon S.

File.open(“test”, “a+”) do |f|  
500000.times { f.write(“helloworld”) }  
end

require ‘pty’  
require ‘expect’  
PTY.spawn(“gpg -c test”) do |r,w,pid|  
w.sync = true  
r.expect("Enter passphrase: ", 1) do |e|  
raise unless e  
w.puts “secretpassword”  
end  
r.expect("Repeat passphrase: ", 1) do |e|  
raise unless e  
w.puts “secretpassword”  
end

puts “eof=#{r.eof}”

# how to read eventual data

# how to expect eof?

end

system(‘ls test\*’)

# output when running the program

prompt\> ruby a.rb  
eof=false  
test test.gpg  
prompt\>

---

<div class="post-metadata">

**Author:** ![simonsaid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b5e925/32.png) [@simonsaid](https://www.ruby-forum.com/u/simonsaid)\
**Post date:** [December 20, 2006, 7:08pm UTC](https://www.ruby-forum.com/t/how-to-expect-eof-with-expect-pty/79947/2 "2006-12-20T19:08:11Z")

</div>

On 12/20/06, Simon S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:  
[snip]

> I want to listen for:  
> 'Enter passphrase: ’  
> 'Repeat passphrase: ’  
> eof?  
> [snip]

Ok I got a partial solution… now it would be nice if I could access  
the string accumlated in readlines when this thing timeouts.

–  
Simon S.

File.open(“test”, “a+”) do |f|  
500000.times { f.write(“helloworld”) }  
end

require ‘expect’  
require ‘timeout’  
require ‘pty’

timeout(3) do  
PTY.spawn(“gpg -c test”) do |r,w,pid|  
w.sync = true  
r.expect("Enter passphrase: ", 1) do |e|  
raise unless e  
w.puts “secretpassword”  
end  
r.expect("Repeat passphrase: ", 1) do |e|  
raise unless e  
w.puts “secretpassword”  
end

```
r.readlines
puts "eof=#{r.eof}"

```

end  
end

system(‘ls test\*’)
