# Copying files to remote machine

**URL:** <https://www.ruby-forum.com/t/copying-files-to-remote-machine/107973>\
**Category:** Ruby\
**Created:** [June 27, 2007, 11:24pm UTC](https://www.ruby-forum.com/t/copying-files-to-remote-machine/107973 "2007-06-27T23:24:59Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![enchanter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ce7236/32.png) [@enchanter](https://www.ruby-forum.com/u/enchanter)\
**Post date:** [June 27, 2007, 11:24pm UTC](https://www.ruby-forum.com/t/copying-files-to-remote-machine/107973/1 "2007-06-27T23:24:59Z")

</div>

Hey all,

I’ve got a little script that is attempting to copy images to a  
remote server. The significant parts of the script were taken  
directly from recipe 14.11 in the ruby cookbook.

relevant sections:

# directly from cookbook recipe 14.11

def remote\_copy(session, source\_path, destination\_path=nil)  
destination\_path ||= source\_path  
cmd = %{cat \> “#{destination\_path.gsub('”', ‘"’)}"}  
cat\_process = session.process.open(cmd)

cat\_process.on\_success do |p|  
p.write(open(source\_path) { |f| f.read })  
p.close  
puts “Copied #{source\_path} to #{destination\_path}.”  
end  
end

# slightly modified from same recipe

Net::SSH.start  
(‘[remote.server.com](http://remote.server.com)’, :username=\>‘user’, :password=\>‘pass’) do |session|  
Film.find(:all).each do |film|  
if film.small\_local\_cover\_image\_exists?  
remote\_copy(session, film.small\_local\_cover\_path,

film.small\_remote\_cover\_path)  
end  
end  
session.loop  
end

When I run it works for about the first 4 or 5 films then dies with  
this message:

/usr/lib/ruby/gems/1.8/gems/net-ssh-1.0.10/lib/net/ssh/transport/  
session.rb:258:in `wait_for_message': disconnected: Received data for nonexistent channel 2. (2) (Net::SSH::Transport::Disconnect) from /usr/lib/ruby/gems/1.8/gems/net-ssh-1.0.10/lib/net/ssh/ transport/session.rb:242:in `wait\_for\_message’  
from /usr/lib/ruby/gems/1.8/gems/net-ssh-1.0.10/lib/net/ssh/  
connection/driver.rb:148:in `process' from /usr/lib/ruby/gems/1.8/gems/net-ssh-1.0.10/lib/net/ssh/ connection/driver.rb:138:in `loop’  
from /usr/lib/ruby/gems/1.8/gems/net-ssh-1.0.10/lib/net/ssh/  
session.rb:164:in `loop' from script/cron/move_images_to_static_server.rb:53 from /usr/lib/ruby/gems/1.8/gems/net-ssh-1.0.10/lib/net/ssh/ session.rb:127:in `initialize’  
from /usr/lib/ruby/gems/1.8/gems/net-ssh-1.0.10/lib/net/  
ssh.rb:47:in `start’  
from script/cron/move\_images\_to\_static\_server.rb:37

ubuntu 6.06 “Dapper Drake” - ruby 1.8.4 - net-ssh 1.0.10

Thanks in advance for any help!  
Tim

---

<div class="post-metadata">

**Author:** ![enchanter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ce7236/32.png) [@enchanter](https://www.ruby-forum.com/u/enchanter)\
**Post date:** [June 28, 2007, 7:26am UTC](https://www.ruby-forum.com/t/copying-files-to-remote-machine/107973/2 "2007-06-28T07:26:32Z")

</div>

This issue has been discussed on the mail list before. I would  
recommend using ftp or sftp if possible, otherwise search the list!
