Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby net/sftp远程读取文件并保存到数据库,获取错误状态异常(4,“故障”)_Ruby_File_Gets_Net Sftp - Fatal编程技术网

Ruby net/sftp远程读取文件并保存到数据库,获取错误状态异常(4,“故障”)

Ruby net/sftp远程读取文件并保存到数据库,获取错误状态异常(4,“故障”),ruby,file,gets,net-sftp,Ruby,File,Gets,Net Sftp,我正在尝试从远程sftp站点下载XML,将文件内容读入REXML,并将数据保存到数据库中。到目前为止,代码可以读取文件,f.gets行失败,出现错误: /usr/local/lib/ruby/gems/1.9.1/gems/net-sftp-2.0.5/lib/net/sftp/session.rb:846:in `wait_for': Net::SFTP::StatusException (4, "failure") (Net::SFTP::StatusException) f

我正在尝试从远程sftp站点下载XML,将文件内容读入REXML,并将数据保存到数据库中。到目前为止,代码可以读取文件,f.gets行失败,出现错误:

/usr/local/lib/ruby/gems/1.9.1/gems/net-sftp-2.0.5/lib/net/sftp/session.rb:846:in `wait_for': Net::SFTP::StatusException (4, "failure") (Net::SFTP::StatusException)
        from /usr/local/lib/ruby/gems/1.9.1/gems/net-sftp-2.0.5/lib/net/sftp/session.rb:257:in `read!'
        from /usr/local/lib/ruby/gems/1.9.1/gems/net-sftp-2.0.5/lib/net/sftp/operations/file.rb:162:in `fill'
        from /usr/local/lib/ruby/gems/1.9.1/gems/net-sftp-2.0.5/lib/net/sftp/operations/file.rb:100:in `block in gets'
        from /usr/local/lib/ruby/gems/1.9.1/gems/net-sftp-2.0.5/lib/net/sftp/operations/file.rb:93:in `loop'
        from /usr/local/lib/ruby/gems/1.9.1/gems/net-sftp-2.0.5/lib/net/sftp/operations/file.rb:93:in `gets'
        from ./test.rb:10:in `block (3 levels) in <main>'
        from /usr/local/lib/ruby/gems/1.9.1/gems/net-sftp-2.0.5/lib/net/sftp/operations/file_factory.rb:45:in `open'
        from ./test.rb:9:in `block (2 levels) in <main>'
        from /usr/local/lib/ruby/gems/1.9.1/gems/net-sftp-2.0.5/lib/net/sftp/operations/dir.rb:33:in `block in foreach'
        from /usr/local/lib/ruby/gems/1.9.1/gems/net-sftp-2.0.5/lib/net/sftp/operations/dir.rb:33:in `each'
        from /usr/local/lib/ruby/gems/1.9.1/gems/net-sftp-2.0.5/lib/net/sftp/operations/dir.rb:33:in `foreach'
        from ./test.rb:8:in `block in <main>'
        from /usr/local/lib/ruby/gems/1.9.1/gems/net-sftp-2.0.5/lib/net/sftp/session.rb:939:in `call'
        from /usr/local/lib/ruby/gems/1.9.1/gems/net-sftp-2.0.5/lib/net/sftp/session.rb:939:in `block in do_version'
        from /usr/local/lib/ruby/gems/1.9.1/gems/net-sftp-2.0.5/lib/net/sftp/session.rb:939:in `each'
        from /usr/local/lib/ruby/gems/1.9.1/gems/net-sftp-2.0.5/lib/net/sftp/session.rb:939:in `do_version'
        from /usr/local/lib/ruby/gems/1.9.1/gems/net-sftp-2.0.5/lib/net/sftp/session.rb:909:in `when_channel_polled'
        from /usr/local/lib/ruby/gems/1.9.1/gems/net-ssh-2.5.2/lib/net/ssh/connection/channel.rb:311:in `call'
        from /usr/local/lib/ruby/gems/1.9.1/gems/net-ssh-2.5.2/lib/net/ssh/connection/channel.rb:311:in `process'
        from /usr/local/lib/ruby/gems/1.9.1/gems/net-ssh-2.5.2/lib/net/ssh/connection/session.rb:214:in `block in preprocess'
        from /usr/local/lib/ruby/gems/1.9.1/gems/net-ssh-2.5.2/lib/net/ssh/connection/session.rb:214:in `each'
        from /usr/local/lib/ruby/gems/1.9.1/gems/net-ssh-2.5.2/lib/net/ssh/connection/session.rb:214:in `preprocess'
        from /usr/local/lib/ruby/gems/1.9.1/gems/net-ssh-2.5.2/lib/net/ssh/connection/session.rb:197:in `process'
        from /usr/local/lib/ruby/gems/1.9.1/gems/net-ssh-2.5.2/lib/net/ssh/connection/session.rb:161:in `block in loop'
        from /usr/local/lib/ruby/gems/1.9.1/gems/net-ssh-2.5.2/lib/net/ssh/connection/session.rb:161:in `loop'
        from /usr/local/lib/ruby/gems/1.9.1/gems/net-ssh-2.5.2/lib/net/ssh/connection/session.rb:161:in `loop'
        from /usr/local/lib/ruby/gems/1.9.1/gems/net-sftp-2.0.5/lib/net/sftp/session.rb:802:in `loop'
        from /usr/local/lib/ruby/gems/1.9.1/gems/net-sftp-2.0.5/lib/net/sftp/session.rb:787:in `connect!'
        from /usr/local/lib/ruby/gems/1.9.1/gems/net-sftp-2.0.5/lib/net/sftp.rb:32:in `start'
        from ./test.rb:7:in `<main>'
/usr/local/lib/ruby/gems/1.9.1/gems/net-sftp-2.0.5/lib/net/sftp/session.rb:846:in'wait_for':net::sftp::StatusException(4,“失败”)(net::sftp::StatusException)
from/usr/local/lib/ruby/gems/1.9.1/gems/net-sftp-2.0.5/lib/net/sftp/session.rb:257:in'read!'
from/usr/local/lib/ruby/gems/1.9.1/gems/net-sftp-2.0.5/lib/net/sftp/operations/file.rb:162:填写'fill'
from/usr/local/lib/ruby/gems/1.9.1/gems/net-sftp-2.0.5/lib/net/sftp/operations/file.rb:100:in'block in get'
from/usr/local/lib/ruby/gems/1.9.1/gems/net-sftp-2.0.5/lib/net/sftp/operations/file.rb:93:in'loop'
来自/usr/local/lib/ruby/gems/1.9.1/gems/net-sftp-2.0.5/lib/net/sftp/operations/file.rb:93:in`gets'
from./test.rb:10:in‘block(3层)in’
来自/usr/local/lib/ruby/gems/1.9.1/gems/net-sftp-2.0.5/lib/net/sftp/operations/file_factory.rb:45:在“打开”中
from./test.rb:9:in`block(2层)in'
from/usr/local/lib/ruby/gems/1.9.1/gems/net-sftp-2.0.5/lib/net/sftp/operations/dir.rb:33:in'block in foreach'
from/usr/local/lib/ruby/gems/1.9.1/gems/net-sftp-2.0.5/lib/net/sftp/operations/dir.rb:33:在'each'中
from/usr/local/lib/ruby/gems/1.9.1/gems/net-sftp-2.0.5/lib/net/sftp/operations/dir.rb:33:in'foreach'
from./test.rb:8:in'block in'
from/usr/local/lib/ruby/gems/1.9.1/gems/net-sftp-2.0.5/lib/net/sftp/session.rb:939:in'call'
from/usr/local/lib/ruby/gems/1.9.1/gems/net-sftp-2.0.5/lib/net/sftp/session.rb:939:in“do_版本中的块”
from/usr/local/lib/ruby/gems/1.9.1/gems/net-sftp-2.0.5/lib/net/sftp/session.rb:939:in'each'
from/usr/local/lib/ruby/gems/1.9.1/gems/net-sftp-2.0.5/lib/net/sftp/session.rb:939:in'do_version'
from/usr/local/lib/ruby/gems/1.9.1/gems/net-sftp-2.0.5/lib/net/sftp/session.rb:909:in“when_channel_poll”
来自/usr/local/lib/ruby/gems/1.9.1/gems/net-ssh-2.5.2/lib/net/ssh/connection/channel.rb:311:in'call'
来自/usr/local/lib/ruby/gems/1.9.1/gems/net-ssh-2.5.2/lib/net/ssh/connection/channel.rb:311:在“进程”中
from/usr/local/lib/ruby/gems/1.9.1/gems/net-ssh-2.5.2/lib/net/ssh/connection/session.rb:214:在“预处理中的块”中
from/usr/local/lib/ruby/gems/1.9.1/gems/net-ssh-2.5.2/lib/net/ssh/connection/session.rb:214:在'each'中
from/usr/local/lib/ruby/gems/1.9.1/gems/net-ssh-2.5.2/lib/net/ssh/connection/session.rb:214:在“预处理”中
来自/usr/local/lib/ruby/gems/1.9.1/gems/net-ssh-2.5.2/lib/net/ssh/connection/session.rb:197:在“进程”中
from/usr/local/lib/ruby/gems/1.9.1/gems/net-ssh-2.5.2/lib/net/ssh/connection/session.rb:161:in“block-in-loop”
from/usr/local/lib/ruby/gems/1.9.1/gems/net-ssh-2.5.2/lib/net/ssh/connection/session.rb:161:in'loop'
from/usr/local/lib/ruby/gems/1.9.1/gems/net-ssh-2.5.2/lib/net/ssh/connection/session.rb:161:in'loop'
from/usr/local/lib/ruby/gems/1.9.1/gems/net-sftp-2.0.5/lib/net/sftp/session.rb:802:in'loop'
from/usr/local/lib/ruby/gems/1.9.1/gems/net-sftp-2.0.5/lib/net/sftp/session.rb:787:in'connect!'
from/usr/local/lib/ruby/gems/1.9.1/gems/net-sftp-2.0.5/lib/net/sftp.rb:32:in'start'
from./test.rb:7:in`'
这是我的密码:

#!/usr/bin/ruby

require 'rubygems'
require 'net/sftp'
require 'rexml/document'

Net::SFTP.start('some.example.host','myuser',:password => 'fake') do |sftp|
    sftp.dir.foreach("/path/to/mydir/") do |xml|
        sftp.file.open("/path/to/mydir/"+xml.name) do |f|
            puts f.gets # < THIS BREAKS IT
        end
    end  
end
#/usr/bin/ruby
需要“rubygems”
需要“净/sftp”
需要“rexml/文档”
Net::SFTP.start('some.example.host','myuser',:password=>'fake')do | SFTP|
sftp.dir.foreach(“/path/to/mydir/”)do|xml|
打开(“/path/to/mydir/”+xml.name)do | f|
这就打破了它
结束
结束
结束
代码是脏的,现在是一个测试。我希望读取远程目录中的所有文件,并打印每个文件的第一行


谢谢你的帮助

我想你想要的是:

sftp.download!("/path/to/remote", "/path/to/local")
还是这个

 data = sftp.download!("/path/to/remote")

从文档中得到了以下信息:

我在尝试访问SFTP用户无权访问的文件时收到了类似的错误

Net::SFTP::StatusException: Net::SFTP::StatusException
    from /usr/local/rbenv/versions/1.9.3-p374/lib/ruby/gems/1.9.1/gems/net-sftp-2.0.5/lib/net/sftp/session.rb:846:in `wait_for'
    from /usr/local/rbenv/versions/1.9.3-p374/lib/ruby/gems/1.9.1/gems/net-sftp-2.0.5/lib/net/sftp/session.rb:561:in `stat!'
    from (irb):80
    from /usr/local/rbenv/versions/1.9.3-p374/lib/ruby/gems/1.9.1/gems/railties-3.2.13/lib/rails/commands/console.rb:47:in `start'
    from /usr/local/rbenv/versions/1.9.3-p374/lib/ruby/gems/1.9.1/gems/railties-3.2.13/lib/rails/commands/console.rb:8:in `start'
    from /usr/local/rbenv/versions/1.9.3-p374/lib/ruby/gems/1.9.1/gems/railties-3.2.13/lib/rails/commands.rb:41:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'
Net::SFTP::StatusException:Net::SFTP::StatusException
from/usr/local/rbenv/versions/1.9.3-p374/lib/ruby/gems/1.9.1/gems/net-sftp-2.0.5/lib/net/sftp/session.rb:846:“等待”
from/usr/local/rbenv/versions/1.9.3-p374/lib/ruby/gems/1.9.1/gems/net-sftp-2.0.5/lib/net/sftp/session.rb:561:in'stat!'
起始(irb):80
来自/usr/local/rbenv/versions/1.9.3-p374/lib/ruby/gems/1.9.1/gems/railties-3.2.13/lib/rails/commands/console.rb:47:在“开始”中
来自/usr/local/rbenv/versions/1.9.3-p374/lib/ruby/gems/1.9.1/gems/railties-3.2.13/lib/rails/commands/console.rb:8:在“开始”中
来自/usr/local/rbenv/versions/1.9.3-p374/lib/ruby/gems/1.9.1/gems/railties-3.2.13/lib/rails/commands.rb:41:in`'
来自脚本/rails:6:in'require'
来自脚本/rails:6:in`'

如果您不想下载文件,请参阅[此处][1][1]:如果您不想下载文件,请参阅[此处][1][1]: