# Communicating w/ command-line program (OR ruby expect)

**URL:** <https://www.ruby-forum.com/t/communicating-w-command-line-program-or-ruby-expect/115407>\
**Category:** Ruby\
**Created:** [September 24, 2007, 11:58pm UTC](https://www.ruby-forum.com/t/communicating-w-command-line-program-or-ruby-expect/115407 "2007-09-24T23:58:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![heynow](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f04885/32.png) [@heynow](https://www.ruby-forum.com/u/heynow)\
**Post date:** [September 24, 2007, 11:58pm UTC](https://www.ruby-forum.com/t/communicating-w-command-line-program-or-ruby-expect/115407/1 "2007-09-24T23:58:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![heynow](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f04885/32.png) [@heynow](https://www.ruby-forum.com/u/heynow)\
**Post date:** [September 25, 2007, 12:10am UTC](https://www.ruby-forum.com/t/communicating-w-command-line-program-or-ruby-expect/115407/2 "2007-09-25T00:10:41Z")

</div>

On Sep 24, 4:57 pm, “Cameron M.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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

---

<div class="post-metadata">

**Author:** ![heynow](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f04885/32.png) [@heynow](https://www.ruby-forum.com/u/heynow)\
**Post date:** [September 25, 2007, 12:19am UTC](https://www.ruby-forum.com/t/communicating-w-command-line-program-or-ruby-expect/115407/3 "2007-09-25T00:19:42Z")

</div>

Hi,

On 9/24/07, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![heynow](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f04885/32.png) [@heynow](https://www.ruby-forum.com/u/heynow)\
**Post date:** [September 25, 2007, 12:30am UTC](https://www.ruby-forum.com/t/communicating-w-command-line-program-or-ruby-expect/115407/4 "2007-09-25T00:30:43Z")

</div>

On Sep 24, 2007, at 5:18 PM, Cameron M. wrote:

> Hi,
> 
> On 9/24/07, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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 G. II

---

<div class="post-metadata">

**Author:** ![heynow](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f04885/32.png) [@heynow](https://www.ruby-forum.com/u/heynow)\
**Post date:** [October 15, 2009, 3:34am UTC](https://www.ruby-forum.com/t/communicating-w-command-line-program-or-ruby-expect/115407/5 "2009-10-15T03:34:44Z")

</div>

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-Administration/Scripting-an-External-Program/](http://codeidol.com/other/rubyckbk/System-Administration/Scripting-an-External-Program/)

hth  
joahking

> **[Least Significant Bit](http://www.least-significant-bit.com)**
>
> The blog of Joaquin Rivera Padron (@joahking). Here I share my thoughts about programming, architecture, scalability, Open Source, Ruby, Testing, BDD, linux and more.
