2

我在 ruby​​ 1.9.3 中有这个:

Net::SFTP.start(ENV['SFTP_HOST'], ENV['SFTP_USERNAME'], password: ENV['SFTP_PASSWORD'], key_data: [ENV['SFTP_KEY']], keys_only: true) do |sftp|
  sftp.upload!(StringIO.new("testing"), "/text.txt")
end

它似乎正确地执行了身份验证步骤(我可以列出目录等中的文件)但是当我执行upload!上面的命令时它返回:

Net::SSH::Disconnect: disconnected: Failed to read binary packet data! (2)
from /Users/barmstrong/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/net-ssh-2.5.2/lib/net/ssh/transport/session.rb:176:in `block in poll_message'
from /Users/barmstrong/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/net-ssh-2.5.2/lib/net/ssh/transport/session.rb:166:in `loop'
from /Users/barmstrong/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/net-ssh-2.5.2/lib/net/ssh/transport/session.rb:166:in `poll_message'
from /Users/barmstrong/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/net-ssh-2.5.2/lib/net/ssh/connection/session.rb:451:in `dispatch_incoming_packets'
from /Users/barmstrong/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/net-ssh-2.5.2/lib/net/ssh/connection/session.rb:213:in `preprocess'
from /Users/barmstrong/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/net-ssh-2.5.2/lib/net/ssh/connection/session.rb:197:in `process'
from /Users/barmstrong/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/net-ssh-2.5.2/lib/net/ssh/connection/session.rb:161:in `block in loop'
from /Users/barmstrong/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/net-ssh-2.5.2/lib/net/ssh/connection/session.rb:161:in `loop'
from /Users/barmstrong/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/net-ssh-2.5.2/lib/net/ssh/connection/session.rb:161:in `loop'
from /Users/barmstrong/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/net-sftp-2.0.5/lib/net/sftp/session.rb:802:in `loop'
from /Users/barmstrong/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/net-sftp-2.0.5/lib/net/sftp/operations/upload.rb:195:in `wait'
from /Users/barmstrong/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/net-sftp-2.0.5/lib/net/sftp/session.rb:103:in `upload!'

我也试过

    # open and write to a pseudo-IO for a remote file
    sftp.file.open("/test.txt", "wb") do |f|
      f.puts "testing"
    end

尝试编写二进制文件,但没有运气。

有任何想法吗?谢谢!

4

1 回答 1

2

好吧,我找到了一个解决方案,但我不知道它为什么会起作用。

只是在玩sftp.dir.foreach("/") {|e| puts e.name }之前我添加了upload!它,它起作用了!

所以它现在看起来像这样。

Net::SFTP.start(ENV['SFTP_HOST'], ENV['SFTP_USERNAME'], password: ENV['SFTP_PASSWORD'], key_data: [ENV['SFTP_KEY']], keys_only: true) do |sftp|
  sftp.dir.foreach("/") {}
  sftp.upload!(StringIO.new("testing"), "/text.txt")
end

做一个dir不应该有任何影响,但出于某种原因它确实。如果有人对此有一些见解,我会很好奇。谢谢!

顺便说一句,ftp 服务器将自己标识为“SSH-2.0-Maverick_SSHD”,因为它的价值。

于 2012-10-12T01:42:33.443 回答