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

C

Cameron Matheson

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
 
Y

yermej

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

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
 
C

Cameron Matheson

Hi,

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
 
J

James Edward Gray II

Hi,



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
 

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

Similar Threads


Members online

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top