Forum: Ruby communicating w/ command-line program (OR ruby expect)

Ef48af8b07b976160c860ddec9aea55f?d=identicon&s=25 Cameron Matheson (cam)
on 2007-09-24 23:58
(Received via mailing list)
Hello,

I'm trying to communicate w/ a command-line process w/ ruby.  I
thought IO.popen would be the right tool for the job, but I need to be
able to read and write to the process, and I can't get it to work
(i've googled for examples, but everything I have seen is only doing
one-way communication).  Anyway, it looks like what I need is
something like "Expect" for ruby.  Does anyone know if such a tool
exists?  (Or any other way in which I could read/write from a
command-line program).

Thanks!
Cam
Bc8e3e2f7acb3a7d93c10e235dc0b4e1?d=identicon&s=25 yermej@gmail.com (Guest)
on 2007-09-25 00:10
(Received via mailing list)
On Sep 24, 4:57 pm, "Cameron Matheson" <cameron.mathe...@gmail.com>
wrote:
>
> Thanks!
> Cam

If you're just calling a command line tool like grep or something
(reads standard input and then returns result to standard out),
IO.popen is what you need:

cmd_output = IO.popen "cmd -args some stuff", 'r+' do |pipe|
  pipe.write the_input
  pipe.close_write
  pipe.read.chomp
end

if $? == 0
  # success, do something
else
  # cmd exited with an error, deal with it
end

If you need to interact with something (a la irb, ftp, telnet, etc.)
you'll need to find something else, I think.

Jeremy
Ef48af8b07b976160c860ddec9aea55f?d=identicon&s=25 Cameron Matheson (cam)
on 2007-09-25 00:19
(Received via mailing list)
Hi,

On 9/24/07, yermej@gmail.com <yermej@gmail.com> wrote:
> If you need to interact with something (a la irb, ftp, telnet, etc.)
> you'll need to find something else, I think.

This is what I'm looking for (something to interact w/ a program that
has an ftp-like interface).

Thanks,
Cam
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2007-09-25 00:30
(Received via mailing list)
On Sep 24, 2007, at 5:18 PM, Cameron Matheson wrote:

> Hi,
>
> On 9/24/07, yermej@gmail.com <yermej@gmail.com> wrote:
>> If you need to interact with something (a la irb, ftp, telnet, etc.)
>> you'll need to find something else, I think.
>
> This is what I'm looking for (something to interact w/ a program that
> has an ftp-like interface).

For FTP use Net::FTP, of course.  Otherwise, Ruby does ship with a
simple expect library:

$ cat /usr/local/lib/ruby/1.8/expect.rb
$expect_verbose = false

class IO
   def expect(pat,timeout=9999999)
     buf = ''
     case pat
     when String
       e_pat = Regexp.new(Regexp.quote(pat))
     when Regexp
       e_pat = pat
     end
     while true
       if IO.select([self],nil,nil,timeout).nil? then
         result = nil
         break
       end
       c = getc.chr
       buf << c
       if $expect_verbose
         STDOUT.print c
         STDOUT.flush
       end
       if mat=e_pat.match(buf) then
         result = [buf,*mat.to_a[1..-1]]
         break
       end
     end
     if block_given? then
       yield result
     else
       return result
     end
     nil
   end
end

__END__

Hope that helps.

James Edward Gray II
8a2de74362a8ee5f9c5e86b1ba0b1640?d=identicon&s=25 Joaquin Rivera padron (joahking)
on 2009-10-15 03:34
hey there,
I don't know if you already got it (hope so :-) ) but here you'll find
helpful tips:
http://codeidol.com/other/rubyckbk/System-Administ...

hth
joahking
www.least-significant-bit.com
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.