class WoolenCommon::SshProxy

Attributes

the_ssh_instances[RW]

Public Class Methods

get_ssh_proxy(ip, port, user, passwd, opt={}) click to toggle source
# File lib/woolen_common/ssh_proxy.rb, line 61
def get_ssh_proxy(ip, port, user, passwd, opt={})
  options = { :port => port, :password => passwd }.merge(opt)
  @the_ssh_instances ||= {}
  @the_ssh_instances[ip] ||= {}
  @the_ssh_instances[ip][port] ||= {}
  @the_ssh_instances[ip][port][user] ||= {}
  @the_ssh_instances[ip][port][user][passwd] ||= SshProxy.new(ip, user, options)
  @the_ssh_instances[ip][port][user][passwd]
end
new(host, user, options={}) click to toggle source
# File lib/woolen_common/ssh_proxy.rb, line 72
def initialize(host, user, options={})
  @host = host
  @user = user
  @options = options
  @conn_retry = options[:proxy_conn_retry] || 5
  options[:paranoid] = false # for disable the public key verify
  options[:non_interactive] = true # for disable the interactive
  options.delete :proxy_conn_retry if options[:proxy_conn_retry]
  # 超时时间设置30秒太长了,不是很合理,实际上5秒没有回复,那就是出问题了
  @conn_timeout = options[:proxy_conn_timeout] || 5
  options.delete :proxy_conn_timeout if options[:proxy_conn_timeout]
  proxy_reset_conn
end

Public Instance Methods

check_connector_close() click to toggle source
# File lib/woolen_common/ssh_proxy.rb, line 146
def check_connector_close
  begin
    if @ssh_conn.nil? or @ssh_conn.closed?
      return true
    end
    Timeout.timeout(@conn_timeout) do
      if @ssh_conn.exec!('echo hello').include? 'hello'
        return false
      end
    end
  rescue Exception => e
    error "检查连接出错,错误信息是::#{e.message}"
    return true
  end
  true
end
exec(command, &block) click to toggle source
# File lib/woolen_common/ssh_proxy.rb, line 136
def exec(command, &block)
  if check_connector_close
    @ssh_conn.close rescue nil
    proxy_reset_conn
  end
  Timeout.timeout(@conn_timeout) do
    return @ssh_conn.exec(command.unpack('C*').pack('C*'), &block)
  end
end
exec!(command, &block) click to toggle source
# File lib/woolen_common/ssh_proxy.rb, line 126
def exec!(command, &block)
  if check_connector_close
    @ssh_conn.close rescue nil
    proxy_reset_conn
  end
  Timeout.timeout(@conn_timeout) do
    return @ssh_conn.exec!(command.unpack('C*').pack('C*'), &block)
  end
end
method_missing(name, *args, &block) click to toggle source
# File lib/woolen_common/ssh_proxy.rb, line 107
def method_missing(name, *args, &block)
  if check_connector_close
    @ssh_conn.close rescue nil
    proxy_reset_conn
  end
  #debug "SshProxy need to invoke methdo ::#{name} "
  #debug "params::#{args}"
  Timeout.timeout(@conn_timeout) do
    return_result = ''
    if @ssh_conn
      return_result = @ssh_conn.send(name, *args, &block)
      #debug "SshProxy invoke result ::#{return_result}"
    else
      error 'ssh链接建立不起来!'
    end
    return return_result
  end
end
proxy_reset_conn() click to toggle source
# File lib/woolen_common/ssh_proxy.rb, line 86
def proxy_reset_conn
  @conn_retry.times do
    begin
      Timeout.timeout(@conn_timeout) do
        # if Net::SSH::Version.to_i <= 3_000_000
        #     @ssh_conn = Net::SSH.start(@host, @user, @options)
        # else
        #     @ssh_conn = Net::SSH.start(@host, @user, @options)
        # end
        @ssh_conn = Net::SSH.start(@host, @user, @options)
        if check_connector_close
          debug 'reconnect ssh ok'
          return
        end
      end
    rescue Exception => e
      error "连接ssh服务器出错~!信息是:#{e.message},用户信息:@host:#{@host},@user:#{@user},@options:#{@options}"
    end
  end
end
sftp_download(remote_path, local_path) click to toggle source

非塞性下载

# File lib/woolen_common/ssh_proxy.rb, line 175
def sftp_download(remote_path, local_path)
  if check_connector_close
    @ssh_conn.close rescue nil
    proxy_reset_conn
  end
  @ssh_conn.sftp.connect do |sftp_session|
    return sftp_session.download!(remote_path, local_path)
  end
end
sftp_download!(remote_path, local_path) click to toggle source

阻塞性下载

# File lib/woolen_common/ssh_proxy.rb, line 164
def sftp_download!(remote_path, local_path)
  if check_connector_close
    @ssh_conn.close rescue nil
    proxy_reset_conn
  end
  @ssh_conn.sftp.connect! do |sftp_session|
    return sftp_session.download!(remote_path, local_path)
  end
end
sftp_upload(remote_path, local_path) click to toggle source
# File lib/woolen_common/ssh_proxy.rb, line 195
def sftp_upload(remote_path, local_path)
  if check_connector_close
    @ssh_conn.close rescue nil
    proxy_reset_conn
  end
  @ssh_conn.sftp.connect do |sftp_session|
    return sftp_session.upload(local_path, remote_path)
  end
end
sftp_upload!(remote_path, local_path) click to toggle source
# File lib/woolen_common/ssh_proxy.rb, line 185
def sftp_upload!(remote_path, local_path)
  if check_connector_close
    @ssh_conn.close rescue nil
    proxy_reset_conn
  end
  @ssh_conn.sftp.connect! do |sftp_session|
    return sftp_session.upload!(local_path, remote_path)
  end
end