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

环境: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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值