论坛首页 编程语言技术论坛

让NET::SSH 和 Jruby 在windows上工作

浏览 2605 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2009-01-03   最后修改:2009-01-03
环境:Windows XP, Jruby, net-ssh

这两天在搞ims自动测试,需要远程登录,顺便研究了一下纯RUBY语言的NET::SSH。
这个包的最新版本是2.0.8,在linux/Jruby下不能正常工作,需要降到2.0.4。考虑到跨平台又在windows XP 上尝试了一下,仍然有错误。
原因有二,一个是File.expand_path的问题,一个是因为jruby没有dl/import库。

google了半天找到了解决方法,对于第一个原因的解决方案不是很满意,因此添加了重写File.expand_path的一种方案。把解决方法列在这里,希望对大家有帮助。

每种问题都需要修改的一个文件
问题一:
gems\net-ssh-2.0.4\lib\net\ssh\known_hosts.rb 
或者 方案二:修改gems\net-ssh-2.0.4\lib\net\ssh.rb

问题二:
gems\net-ssh-2.0.4\lib\net\ssh\authentication\pageant.rb.


1. know_hosts.rb文件最后。
在执行了File.expand_path,在windows上会得到类似于c:/document and setting/login/.ssh/known_hosts 的路径(注意斜线的方向)。这种解决办法就是不写known_hosts文件。稍后看第二种方案,重写File.expand_path,产生正确的路径名。

# Tries to append an entry to the current source file for the given host
    # and key. If it is unable to (because the file is not writable, for
    # instance), an exception will be raised.
    def add(host, key)
      begin #jpt
        File.open(source, "a") do |file|
          blob = [Net::SSH::Buffer.from(:key, key).to_s].pack("m*").gsub(/\s/, "")
          file.puts "#{host} #{key.ssh_type} #{blob}"
        end
      rescue
#Note that I am just ignoring the error
        puts $!.to_s #just print error msg and goes into its merry way
      end
    end


第二种方案就是重写File.expand_path方法,因为ssh.rb是包的入口,所以把重写的方法写在这里。
class File
  class << self
    alias_method :expand_path2, :expand_path
  end
end

class File
  def File.expand_path(source)
    source = File.expand_path2(source)
    if (ENV['OS'] =~ /win/i) #RUBY_PLATFORM can not be use in current Jruby. you alway get 'java'
      source.gsub!(/\//, '\\')
    end
    return source
  end
end


2. 简化pageant.rb,不用dl包(还没有在jruby实现)
#require 'dl/import'   
#require 'dl/struct'   
  
require 'net/ssh/errors'  
  
module Net; module SSH; module Authentication   
  
  # This module encapsulates the implementation of a socket factory that   
  # uses the PuTTY "pageant" utility to obtain information about SSH   
  # identities.   
  #   
  # This code is a slightly modified version of the original implementation   
  # by Guillaume Marçais (guillaume.marcais@free.fr). It is used and   
  # relicensed by permission.   
  module Pageant   
  
    # From Putty pageant.c   
    AGENT_MAX_MSGLEN = 8192   
    AGENT_COPYDATA_ID = 0x804e50ba   
       
    # The definition of the Windows methods and data structures used in   
    # communicating with the pageant process.   
    module Win   
    end  
  
    # This is the pseudo-socket implementation that mimics the interface of   
    # a socket, translating each request into a Windows messaging call to   
    # the pageant daemon. This allows pageant support to be implemented   
    # simply by replacing the socket factory used by the Agent class.   
    class Socket   
  
    end  
  
  end  
  
end; end; end  
#require 'dl/import'
#require 'dl/struct'

require 'net/ssh/errors'

module Net; module SSH; module Authentication

  # This module encapsulates the implementation of a socket factory that
  # uses the PuTTY "pageant" utility to obtain information about SSH
  # identities.
  #
  # This code is a slightly modified version of the original implementation
  # by Guillaume Marçais (guillaume.marcais@free.fr). It is used and
  # relicensed by permission.
  module Pageant

    # From Putty pageant.c
    AGENT_MAX_MSGLEN = 8192
    AGENT_COPYDATA_ID = 0x804e50ba
    
    # The definition of the Windows methods and data structures used in
    # communicating with the pageant process.
    module Win
    end

    # This is the pseudo-socket implementation that mimics the interface of
    # a socket, translating each request into a Windows messaging call to
    # the pageant daemon. This allows pageant support to be implemented
    # simply by replacing the socket factory used by the Agent class.
    class Socket

    end

  end

end; end; end



   发表时间:2009-06-05  
后来应用这个net-ssh成功了吗? 我发现有些命令运行有问题,基本命令问题不大。
0 请登录后投票
   发表时间:2009-06-07  
成功了,你的问题是?
0 请登录后投票
论坛首页 编程语言技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics