现在的位置: 首页 > 综合 > 正文

使用Net::SSH下载文件并转换文件格式

2015年01月14日 ⁄ 综合 ⁄ 共 1151字 ⁄ 字号 评论关闭
require "rubygems"
require 'net/ssh'
require 'net/sftp'
require 'net/scp'
require 'pathname'
require "iconv"

class Downfile
  @@_path =File.expand_path(File.join(File.dirname(__FILE__),'..','..','common-error','promoprod'))
  @@ofile_path=File.join(@@_path,'common-error.log')
  @@ofile_path1=File.join(@@_path,'common-error-1.log')

  def initialize(from, to = 'GBK')
    @to = to
    @from = from
  end
  def filedown
    File.delete(@@ofile_path) if File.exist?(@@ofile_path)
    sleep 1
    res=false
    Net::SSH.start("promoprod.sit.alipay.net", '***', :password => "*****",:port=>'**') do |ssh|
      res=ssh.scp.download!('/********/common-error.log', @@_path)
    end
    return res
  end

  def changer_file_encoding
    contents = File.open(@@ofile_path).read
    output = Iconv.conv(@to, @from, contents)
    file = File.open(@@ofile_path, 'w')
    file.write(output)
  end

  def fileedit
    @a=[]
    file=File.open(@@ofile_path)
    $line= file.readlines
    #    p $line[20]
    $num=0
    $num.upto($line.size-1) do |x|
      value= $line[x]
      if value.include?(" ERROR ") && !@a.include?(value)
        @a<<value
        $index= @a.index(value)
        next
      end
      if value.include?("\tat") or value.include?(" ERROR ") or value.include?('	at ')
        break
      else
        @a[$index]<<value
      end
    end
    file.close
    return @a
  end

end

oo=Downfile.new("GBK")
oo.filedown
oo.changer_file_encoding
puts  oo.fileedit



抱歉!评论已关闭.