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

来源:互联网 发布:lync for mac 2016 编辑:程序博客网 时间:2024/05/17 14:27
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  endendoo=Downfile.new("GBK")oo.filedownoo.changer_file_encodingputs  oo.fileedit


原创粉丝点击