# New thread for RUBY ftp problems . .

**URL:** <https://www.ruby-forum.com/t/new-thread-for-ruby-ftp-problems/59604>\
**Category:** Ruby\
**Created:** [May 4, 2006, 4:02pm UTC](https://www.ruby-forum.com/t/new-thread-for-ruby-ftp-problems/59604 "2006-05-04T16:02:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [May 4, 2006, 4:02pm UTC](https://www.ruby-forum.com/t/new-thread-for-ruby-ftp-problems/59604/1 "2006-05-04T16:02:47Z")

</div>

I’m starting a new thread, because, my problem appears even in the  
simplest try of RUBY’s ftp.

Here’s a simple attempt to just ftp a file over to one of our UNIX  
servers here at my company. It’s left hanging after the last “end” and I  
have to kill the shell.

C:\workflows\graphics\OneVision\MPC\indexes\6x9\>irb  
irb(main):001:0\> require ‘net/ftp’  
=\> true  
irb(main):002:0\> ftp = Net::FTP.open(‘[quark.bna.com](http://quark.bna.com)’) do |ftp|  
irb(main):003:1\* ftp.login(‘user’,‘passw’)  
irb(main):004:1\> ftp.chdir(‘/home/pb4072’)  
irb(main):005:1\> ftp.putbinaryfile(“lrr095.pdf”)  
irb(main):006:1\> end

Is RUBY’s ftp reliable? Or, what am I doing wrong??

Thanks.

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [May 4, 2006, 4:19pm UTC](https://www.ruby-forum.com/t/new-thread-for-ruby-ftp-problems/59604/2 "2006-05-04T16:19:39Z")

</div>

Peter B. wrote:

> I’m starting a new thread, because, my problem appears even in the  
> simplest try of RUBY’s ftp.
> 
> Here’s a simple attempt to just ftp a file over to one of our UNIX  
> servers here at my company. It’s left hanging after the last “end” and I  
> have to kill the shell.
> 
> C:\workflows\graphics\OneVision\MPC\indexes\6x9\>irb  
> irb(main):001:0\> require ‘net/ftp’  
> =\> true  
> irb(main):002:0\> ftp = Net::FTP.open(‘[quark.bna.com](http://quark.bna.com)’) do |ftp|  
> irb(main):003:1\* ftp.login(‘user’,‘passw’)  
> irb(main):004:1\> ftp.chdir(‘/home/pb4072’)  
> irb(main):005:1\> ftp.putbinaryfile(“lrr095.pdf”)  
> irb(main):006:1\> end
> 
> Is RUBY’s ftp reliable? Or, what am I doing wrong??
> 
> Thanks.  
> Peter,

I tried this sequence on my system and it worked:

thor:~ wfs$ irb  
irb(main):001:0\> require ‘net/ftp’  
=\> true  
irb(main):002:0\> ftp = Net::FTP.open(‘thor’) do |ftp|  
irb(main):003:1\* ftp.login(‘user’,‘password’)  
irb(main):004:1\> ftp.putbinaryfile(‘sorted.txt’,‘sortedXmit.txt’)  
irb(main):005:1\> end

the file I used is quite small so the code completed quickly. and I  
verified that the file was transmited.

I am on Mac OS X 10.4.6 with ruby 1.8.4

Bill

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [May 4, 2006, 5:15pm UTC](https://www.ruby-forum.com/t/new-thread-for-ruby-ftp-problems/59604/3 "2006-05-04T17:15:50Z")

</div>

On Thu, 4 May 2006, Peter B. wrote:

> =\> true  
> irb(main):002:0\> ftp = Net::FTP.open(‘[quark.bna.com](http://quark.bna.com)’) do |ftp|  
> irb(main):003:1\* ftp.login(‘user’,‘passw’)  
> irb(main):004:1\> ftp.chdir(‘/home/pb4072’)  
> irb(main):005:1\> ftp.putbinaryfile(“lrr095.pdf”)  
> irb(main):006:1\> end
> 
> Is RUBY’s ftp reliable? Or, what am I doing wrong??

- you may need passive

- you may not be logging in

- either way you will see what’s happening by doing

here are three commandline scripts i used daily moving tens of thousands  
of  
files around, usage is obivous, never had an issue with any of them:

harp:~ \> ftpls cf0  
drwxrwsr-x 3 457 11111 45056 Apr 20 17:43 cf0-0  
drwxrwsr-x 2 457 11111 954368 Apr 20 17:42 cf0-1

## harp:~ \> ftpput a.rb cf0://cf0-0/

- “/cf0-0/a.rb”

## harp:~ \> ftpget cf0://cf0-0/a.rb

- “./a.rb”

note that you can also use ftp://cf0/cf0-0/a.rb syntax and that any path  
with a  
trailing slash on the command line is interpreted as a directory.

here are the scripts.

```
 harp:~ > cat ftpls
 #!/dmsp/reference/bin/ruby
 require "net/ftp"
 require "uri"

 def uriparse uri
   unless uri =~ %r/^ftp/
     host, path = uri.split %r|:/+|o
     URI::parse "ftp://#{ host }/#{ path }"
   else
     URI::parse uri
   end
 end

 abort "#{ $0 } uri [uris]+" if ARGV.empty?
 user = ENV["FTP_USER"] || "anonymous"
 password = ENV["FTP_PASS"] || ENV["FTP_PASSWORD"] || "removed_email_address@domain.invalid"

 while((uri = ARGV.shift))
   uri = uriparse uri
   Net::FTP.open(uri.host) do |ftp|
     ftp.login user, password
     ftp.passive = true
     ftp.list(uri.path || "/"){|path| puts path}
   end
 end

 harp:~ > cat ftpput
 #!/dmsp/reference/bin/ruby
 require "net/ftp"
 require "uri"
 require "socket"
 require "yaml"

 def uriparse uri
   unless uri =~ %r/^ftp/
     host, path = uri.split %r|:/+|o
     URI::parse "ftp://#{ host }/#{ path }"
   else
     URI::parse uri
   end
 end

 abort "#{ $0 } src [srcs]+ dsturi" if ARGV.empty?
 user = ENV["FTP_USER"] || "anonymous"
 password = ENV["FTP_PASS"] || ENV["FTP_PASSWORD"] || "removed_email_address@domain.invalid"
 debug = ENV["FTP_DEBUG"]
 srcs, uri = ARGV[0..-2], ARGV[-1]

 uri = uriparse uri
 host, hostdest = uri.host, uri.path
 rpaths = []
 hostname, pid = Socket::gethostname, Process::pid

 Net::FTP.open(host) do |ftp|
   ftp.debug_mode = true if debug
   ftp.login user, password
   ftp.passive = true
   ftp.binary = true

   srcs.each do |src|
     dest =
       if hostdest =~ %r|/+\s*$|o or srcs.size > 1
         File::join(hostdest, File::basename(src))
       else
         hostdest
       end
     destdir = File::dirname dest

     destdirs = []
     d = nil
     destdirs.unshift(d) while((d = File::dirname(d || dest)) !~ 

```

%r|^\s\*/+\s\*$|o)

```
     dirname, basename = File::dirname(dest), File::basename(dest)
     tmp = File::join(dirname, ".#{ basename }.#{ hostname }.#{ pid 

```

}.ftp")

```
     if debug
       state = <<-eos
       |@src <#{ src }>
       |@host <#{ host }>
       |@dest <#{ dest }>
       |@destdir <#{ destdir }>
       |@destdirs <#{ destdirs.inspect }>
       |@tmp <#{ tmp.inspect }>
       eos
       state.gsub!(%r/^\s*\|/o,'')
       puts state
     end

     destdirs.each{|d| begin; ftp.mkdir d; rescue Net::FTPPermError; 

```

end}  
ftp.putbinaryfile src, tmp  
ftp.rename tmp, dest  
rpaths \<\< dest  
end  
end

```
 y rpaths

 harp:~ > cat ftpget
 #!/dmsp/reference/bin/ruby
 require "net/ftp"
 require "uri"
 require "yaml"

 def uriparse uri
   unless uri =~ %r/^ftp/
     host, path = uri.split %r|:/+|o
     URI::parse "ftp://#{ host }/#{ path }"
   else
     URI::parse uri
   end
 end

 abort "#{ $0 } srcuri [srcuris]+ dst" if ARGV.empty?
 user = ENV["FTP_USER"] || "anonymous"
 password = ENV["FTP_PASS"] || ENV["FTP_PASSWORD"] || "removed_email_address@domain.invalid"
 debug = ENV["FTP_DEBUG"]

 ARGV << '.' if ARGV.size == 1
 dest = ARGV.pop
 lpaths = []

 while((src = ARGV.shift))
   src = uriparse src
   Net::FTP.open(src.host) do |ftp|
     ftp.debug_mode = true if debug
     ftp.login user, password
     ftp.passive = true
     ftp.binary = true

     paths = []

     if src.path =~ %r|\*$|o
       dir = File::dirname(src.path)
       ftp.list(src.path) do |listing|
         path = listing.split(%r/\s+/o).last
         paths << File::join(dir, path)
       end
     else
       paths << src.path
     end

     paths.each do |path|
       lpath =
         if test ?d, dest
           File::join(dest, File::basename(path))
         else
           dest
         end
       ftp.getbinaryfile path, lpath
       lpaths << lpath
     end
   end
 end

 y lpaths

```

good luck.

-a
