# Using popen

**URL:** <https://www.ruby-forum.com/t/using-popen/165568>\
**Category:** Ruby\
**Created:** [April 15, 2009, 12:14pm UTC](https://www.ruby-forum.com/t/using-popen/165568 "2009-04-15T12:14:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jun\_YSKim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a587f6/32.png) [@Jun\_YSKim](https://www.ruby-forum.com/u/Jun_YSKim)\
**Post date:** [April 15, 2009, 12:14pm UTC](https://www.ruby-forum.com/t/using-popen/165568/1 "2009-04-15T12:14:19Z")

</div>

Hi, all.

look this code.

* * *

require ‘open3’  
include Open3

stdin, stdout, stderr = popen3(“ls”)

## puts stdout.sysread(1024)

this code shows up a file list normally.

but, THIS CODE

* * *

require ‘open3’  
include Open3

stdin, stdout, stderr = popen3(“ftp [ftp.gnu.org](http://ftp.gnu.org)”)

## puts stdout.sysread(1024)

looks like a hang.

Could you tell me why this thing is happened?

---

<div class="post-metadata">

**Author:** ![Jun\_YSKim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a587f6/32.png) [@Jun\_YSKim](https://www.ruby-forum.com/u/Jun_YSKim)\
**Post date:** [April 15, 2009, 3:01pm UTC](https://www.ruby-forum.com/t/using-popen/165568/2 "2009-04-15T15:01:19Z")

</div>

Jun Y. Kim wrote:

> * * *
> 
> require ‘open3’  
> include Open3
> 
> stdin, stdout, stderr = popen3(“ftp [ftp.gnu.org](http://ftp.gnu.org)”)
> 
> ## puts stdout.sysread(1024)
> 
> looks like a hang.
> 
> Could you tell me why this thing is happened?

Probably because (a) the ftp client is waiting for data from the  
terminal before it has sent 1024 bytes of reply, and/or (b) the ftp  
client expects to be run on an interactive tty.

If (b) applies, look at using require ‘pty’

But for FTP, you are almost certainly better off using Net::FTP from the  
Ruby standard library, rather than spawning an external ftp client.

---

<div class="post-metadata">

**Author:** ![Jun\_YSKim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a587f6/32.png) [@Jun\_YSKim](https://www.ruby-forum.com/u/Jun_YSKim)\
**Post date:** [April 16, 2009, 3:00am UTC](https://www.ruby-forum.com/t/using-popen/165568/3 "2009-04-16T03:00:17Z")

</div>

yes, ftp client is waiting some letters for log-in.

but, before that, client program print out “hello message” like

* * *

## Connected to [ftp.gnu.org](http://ftp.gnu.org). 220 GNU FTP server ready. Name ([ftp.gnu.org](http://ftp.gnu.org):junyoung):

under a hanging situation, I cannot also see this message.

anyway.  
(a) I tried to get 1byte by sysread. it’s not different.  
(b) Is interactive tty different from stdin, stdout?  
I believe although ftp is using tty, it should print out something in  
stdout.

1. 
  1. 15, ¿ÀÈÄ 10:01, Brian C. ÀÛ¼º:

---

<div class="post-metadata">

**Author:** ![Jun\_YSKim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a587f6/32.png) [@Jun\_YSKim](https://www.ruby-forum.com/u/Jun_YSKim)\
**Post date:** [April 16, 2009, 4:27am UTC](https://www.ruby-forum.com/t/using-popen/165568/4 "2009-04-16T04:27:46Z")

</div>

2009ë…„ 4ì›” 16ì¼ (ëª©) ì˜¤ì „ 9:59, Jun Y. Kim [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)ë‹˜ì˜ ë§:

> under a hanging situation, I cannot also see this message.
> 
> anyway.  
> (a) I tried to get 1byte by sysread. it’s not different.  
> (b) Is interactive tty different from stdin, stdout?  
> I believe although ftp is using tty, it should print out something in  
> stdout.

Try this:  
require ‘open3’  
include Open3

stdin, stdout, stderr = popen3(“ftp -inv [ftp.gnu.org](http://ftp.gnu.org)”)  
while line=stdout.gets  
print line  
end

I guess ftp is trying to interact with tty.  
Using pty and expect is more suitable in this case

require ‘pty’  
require ‘expect’

PTY.spawn(‘ftp [ftp.gnu.org](http://ftp.gnu.org)’) do |r,w,cid|  
r.expect /Name.\*:\s+/ do |line|  
print line  
w.puts “anonymous”  
end  
while line=r.gets  
print line  
end  
end

Regards,

Park H.
