Forum: Ruby using popen

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
7caba80fca2ac4ddbb7f55957e5c0dec?d=identicon&s=25 Jun Young Kim (Guest)
on 2009-04-15 12:14
(Received via mailing list)
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")

puts stdout.sysread(1024)
----------------------------------------------------------

looks like a hang.

Could you tell me why this thing is happened?
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (candlerb)
on 2009-04-15 15:01
Jun Young Kim wrote:
> ----------------------------------------------------------
> require 'open3'
> include Open3
>
> stdin, stdout, stderr = popen3("ftp 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.
7caba80fca2ac4ddbb7f55957e5c0dec?d=identicon&s=25 Jun Young Kim (Guest)
on 2009-04-16 03:00
(Received via mailing list)
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.
220 GNU FTP server ready.
Name (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.

2009. 04. 15, ¿ÀÈÄ 10:01, Brian Candler ÀÛ¼º:
666b4e17b4bb0e2d999037a25f65a7cb?d=identicon&s=25 Heesob Park (phasis)
on 2009-04-16 04:27
(Received via mailing list)
2009년 4월 16일 (목) 오전 9:59, Jun Young Kim <jykim@altibase.com>님의 
말:
> 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")
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') 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 Heesob
This topic is locked and can not be replied to.