From: Fengfeng Li Date: 2012-04-09T12:32:18+09:00 Subject: Re: How to process telnet data(binary) THUFIR H. wrote in post #1054221: > On Thu, 29 Mar 2012 23:55:16 +0900, Fengfeng Li wrote: > >>> Would you put a link to more complete source? https://gist.github.com/ > [...] >> I haven't finished it yet, becuase the problem still exists. > > > While I may not be able to help, more information surely couldn't hurt. > My suggestion was to post the code which you have -- no worries. > > > -Thufir I cann't open https://gist.github.com/ on my work PC, so I post it below, a little long. Plaese help me with it. Thanks. PS. @m_session is a Net::Telnet object #------------------------- #This is the main code #------------------------- fc = linux_send("String" => "gzip -c #{file_name}") if fc =~ /\x1f\x8b/ debugOut "download #{file_name} by telnet..." fc.gsub!("\xff\xff\xfa", "\xff\xfa") fc = CDS::unzip_stream(@m_fileCache.getCachePath(), file_name, fc) end #------------------------- #These are correlative methods #------------------------- module CDS def self.writeFile(file_path, file_content) #ok fh = File.open(file_path, "wb") fh.write file_content fh.close end def self.unzip_file(path1, path2) org_path = Dir.pwd cache_path = File.dirname(path1) Dir.chdir(cache_path) cmd = "#{$winrar} e -ibck -inul -o+ #{path1.gsub('/', '\\')}" ret_code = system(cmd) fc = "" if File.exist?(path2) fc = readFile(path2).to_s begin File.delete(path2) rescue Exception end end begin File.delete(path1) rescue Exception end #恢复工作目录 Dir.chdir(org_path) return fc end #解压缩 zip 文件流 def self.unzip_stream(cache_path, file_name, zip_content) #保存为临时文件 path1 = getAFreeName(File.join(cache_path, "tmp_file")) writeFile(path1, zip_content) #解压临时文件,读取内容 path2 = File.join(cache_path, File.split(file_name)[-1]) fc = unzip_file(path1, path2) return fc end end def linux_send(args) #参数处理 if args.is_a?(Hash) cmd = args["String"] timeout = args["Timeout"] || 30 prompt = args["Match"] || args["Prompt"] elsif args.is_a?(String) cmd = args timeout = 30 end show_live(cmd) #如果出现timeout错误,反复尝试几次 retry_time = 5 begin #如果连接已建立,则每次都取当前提示符和当前目录 @m_curr_prompt, @m_curr_pwd = "", "" if @m_session_login get_pwd() end debugOut ">>>write_session: #{cmd}" response_msg = "" #指定的提示符会被使用;没指定时如果不是cd命令且已知上一次提示符,使用上一次的;否则使用默认的 if prompt # debugOut " waiting for match(#{prompt})" response_msg = @m_session.cmd("String" => cmd, "Timeout" => timeout, "Match" => prompt) elsif @m_curr_prompt.any? && cmd.strip !~ /^cd\s/i # debugOut " waiting for match(#{@m_curr_prompt})" response_msg = @m_session.cmd("String" => cmd, "Timeout" => timeout, "Match" => Regexp.new(@m_curr_prompt.gsub("[", "\\[").gsub("]", "\\]")) ) else # debugOut " waiting for match(#{@m_session.m_prompt})" response_msg = @m_session.cmd("String" => cmd, "Timeout" => timeout) end rescue TimeoutError => msg debugOut "#{msg}, try again!!!" debugOut "" retry_time -= 1 retry if retry_time >= 0 end #将回显中的当前提示符替换为空 response_msg.gsub!(@m_curr_prompt, "") if @m_curr_prompt.any? return response_msg end -- Posted via http://www.ruby-forum.com/.