我的代码
IMAGE_DIR = 'D:\File_Server\Nisa_Costcutter\Master Nisa CC Logos'
require 'net/ssh'
require 'net/scp'
def scopy_file(file)
puts "Transferring #{file.path}"
Net::SCP.upload!('192.168.254.5',
'passenger',
file,
'/var/www/pinpointlms.co.uk/shared/logos',
:ssh => {password: '*****'})
end
puts "Starting Upload"
Dir.foreach(IMAGE_DIR) do |name|
if name.length > 4 && name[-4..-1].upcase == '.BMP'
filename=name.strip()
file = File.new(File.join(IMAGE_DIR, filename))
if (Time.now - file.mtime) > 86400
scopy_file(file)
end
end
end
puts "End of Transfer"
我正在尝试使用Ruby将一些文件从Windows框复制到Ubuntu框但我得到以下输出:
Starting Upload
Transferring D:\File_Server\Nisa_Costcutter\Master Nisa CC Logos/Z2579.BMP
C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/net-scp-1.1.2/lib/net/scp.rb:359:in `block (3 levels) in start_command': SCP did not finish successfully (1)
(Net::SCP::Error) from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/net-ssh-2.8.0/lib/net/ssh/connection/channel.rb:591:in `call'
from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/net-ssh-2.8.0/lib/net/ssh/connection/channel.rb:591:in `do_close'
from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/net-ssh-2.8.0/lib/net/ssh/connection/session.rb:586:in `channel_close'
from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/net-ssh-2.8.0/lib/net/ssh/connection/session.rb:465:in `dispatch_incoming_packets'
from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/net-ssh-2.8.0/lib/net/ssh/connection/session.rb:221:in `preprocess'
from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/net-ssh-2.8.0/lib/net/ssh/connection/session.rb:205:in `process'
from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/net-ssh-2.8.0/lib/net/ssh/connection/session.rb:169:in `block in loop'
from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/net-ssh-2.8.0/lib/net/ssh/connection/session.rb:169:in `loop'
from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/net-ssh-2.8.0/lib/net/ssh/connection/session.rb:169:in `loop'
from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/net-ssh-2.8.0/lib/net/ssh/connection/session.rb:118:in `close'
from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/net-scp-1.1.2/lib/net/scp.rb:205:in `ensure in start'
from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/net-scp-1.1.2/lib/net/scp.rb:205:in `start'
from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/net-scp-1.1.2/lib/net/scp.rb:221:in `upload!'
from C:/Users/administrator.GASKANDHAWLEY/Desktop/copy_images2.rb:8:in `scopy_file'
from C:/Users/administrator.GASKANDHAWLEY/Desktop/copy_images2.rb:24:in`block in <main>'
from C:/Users/administrator.GASKANDHAWLEY/Desktop/copy_images2.rb:17:in`foreach'
from C:/Users/administrator.GASKANDHAWLEY/Desktop/copy_images2.rb:17:in
`'
我是一名红宝石初学者,所以任何有关如何进一步调试此代码的帮助都将非常感激。
由于
答案 0 :(得分:1)
您可能没有用户访问权限来在Ubuntu服务器上的给定目录“/var/www/pinpointlms.co.uk/shared/logos”中上传(写入)该文件。
尝试保存它而不给它一个完整的路径,因此它最终会出现在Ubuntu服务器上的用户主目录中。如果这样可行,那么您的问题与服务器上的用户权限有关。
答案 1 :(得分:1)
如果它对其他人有帮助,那么在尝试将文件上传到尚未存在的路径时,我会得到完全相同的错误。来自shell的scp将允许你这样做,但Net :: SCP将因此错误而失败。
即
scp "my.file" "/foo/bar/"
如果/ foo存在但/ foo / bar /不存在,scp将创建/ foo / bar并将文件放在那里(假设权限允许你这样做)。
然而 - 在相同的情况下 - 这将失败,问题中给出错误
scp.upload!(my_file, "/foo/bar/")
我找到的唯一解决方案是首先在本地创建您想要的路径,然后使用:recursive选项上传,如下所示:
scp.upload!("bar/", "/foo" :recursive => true)
其中./bar包含my_file。