how to expect eof with expect+pty

Discussion in 'Ruby' started by Simon Strandgaard, Dec 20, 2006.

  1. 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 Strandgaard


    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>
     
    Simon Strandgaard, Dec 20, 2006
    #1
    1. Advertising

  2. On 12/20/06, Simon Strandgaard <> 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 Strandgaard

    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*')
     
    Simon Strandgaard, Dec 20, 2006
    #2
    1. Advertising

  3. On 12/20/06, Simon Strandgaard <> wrote:
    > On 12/20/06, Simon Strandgaard <> 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.

    [snip]

    Now its working.. thanks ruby-talk :)


    require 'expect'
    require 'timeout'
    require 'pty'

    def gnupg_encrypt(filename, passphrase)
    msg = ""
    PTY.spawn("gpg -c #{filename}") do |r,w,pid|
    w.sync = true
    r.expect("Enter passphrase: ", 1) do |e|
    raise "Enter passphrase" unless e
    w.puts passphrase
    end
    r.expect("Repeat passphrase: ", 1) do |e|
    raise "Repeat passphrase" unless e
    w.puts passphrase
    end
    timeout(1) do
    msg += r.getc.chr until r.eof
    end
    end
    if msg =~ /\S/
    p msg
    return -1
    end
    puts "OK"
    return 0
    rescue Timeout::Error => e
    p e, msg
    return -2
    rescue => e
    p e
    return -3
    end


    if $0 == __FILE__
    File.open("test", "a+") do |f|
    500000.times { f.write("helloworld") }
    end

    gnupg_encrypt("test", "secretpassword")
    system('ls test*')
    end





    --
    Simon Strandgaard
    http://opcoders.com/
     
    Simon Strandgaard, Dec 20, 2006
    #3
  4. On Wed, 20 Dec 2006 22:56:44 +0900
    "Simon Strandgaard" <> wrote:

    > On 12/20/06, Simon Strandgaard <> wrote:
    > > On 12/20/06, Simon Strandgaard <> 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.

    > [snip]
    >
    > Now its working.. thanks ruby-talk :)
    >


    If you don't care about overwriting, doesn't "--yes" remove the
    overwrite prompt (it does interactively) ?

    PTY.spawn("gpg --yes -c #{filename}")
     
    Jonathan Hudson, Dec 20, 2006
    #4
  5. On 12/20/06, Jonathan Hudson <> wrote:
    [snip]
    > If you don't care about overwriting, doesn't "--yes" remove the
    > overwrite prompt (it does interactively) ?
    >
    > PTY.spawn("gpg --yes -c #{filename}")
    >


    Its for my daily repository backup. I didn't knew about
    the '--yes' option, this helps alot. Thanks


    --
    Simon Strandgaard
    http://opcoders.com/
     
    Simon Strandgaard, Dec 20, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Evan Driscoll
    Replies:
    0
    Views:
    230
    Evan Driscoll
    Oct 24, 2012
  2. Evan Driscoll
    Replies:
    0
    Views:
    182
    Evan Driscoll
    Oct 24, 2012
  3. Evan Driscoll
    Replies:
    0
    Views:
    209
    Evan Driscoll
    Oct 24, 2012
  4. David Hutto
    Replies:
    0
    Views:
    220
    David Hutto
    Oct 24, 2012
  5. David Hutto
    Replies:
    0
    Views:
    204
    David Hutto
    Oct 24, 2012
Loading...

Share This Page