Simple Ruby SFTP Example Syntax Example

I have a problem that seems very flaky, is it a problem with Ruby or something I did? Please help - my project has stalled until I resolve it.

This code works on Mac OS Leopard:

require 'uri'
require 'net/ssh'
require 'net/sftp'
include Net

def copy_from_uri( uri, local_path )
    # SFTP copy
    SFTP.start( uri.host, uri.user, :password => 'password' ) do |sftp|
        puts "downloading from #{uri.host}, path #{uri.path}\n"
        sftp.download( uri.path, local_path )
    end
end

remote_uri = URI.parse( "sftp://example.com/test.mp4" )
local_file = "/tmp/remote_copy_test.mp4"
result = copy_from_uri( remote_uri, local_file );

What might cause the following error?

$ ruby sftp_fail.rb 
/Library/Ruby/Site/1.8/net/sftp.rb:43:in `start': undefined method `shutdown!' 
for nil:NilClass (NoMethodError)
    from sftp_fail.rb:8:in `copy_from_uri'
    from sftp_fail.rb:18

FYI I installed RUBYOPT correctly, so the gems are loaded and my gems are updated, according to:

$gem list --local
net-sftp (2.0.2, 1.1.0)
net-ssh (2.0.15, 1.1.2)
+3
source share
3 answers

This tells you that some kind of object that you are trying to cause shutdown! method on nil. Now this code is not in your example, so it’s hard to say why it would be, but I really doubt that this is a mistake in the language.

, , , ?

result = copy_from_uri( remote_uri, local_file );

URI # ( ), , .

+3

​​net-sftp v2.0.2:

def self.start(host, user, options={}, &block)
  # ...
rescue Object => anything
  begin
    session.shutdown!
  rescue Exception
    # swallow exceptions that occur while trying to shutdown
  end

  raise anything
end

#start , ... , NoMethodError. rescue Exception , Net::SFTP::Exception, . (. this commit).

net-sftp 2.0.4, . , , , .

+2

I just came across this, but it was caused by another reason. My file host changed its RSA key, so the key in ~/.ssh/known_hostswas wrong - this led to the same error as shown, filtered from SSH. Removing an invalid key fixed the problem.

+1
source

Source: https://habr.com/ru/post/1721249/


All Articles