Module: Xlogin::SessionModule

Included in:
Ssh, Telnet
Defined in:
lib/xlogin/session.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#configObject

Returns the value of attribute config.



12
13
14
# File 'lib/xlogin/session.rb', line 12

def config
  @config
end

#nameObject

Returns the value of attribute name.



11
12
13
# File 'lib/xlogin/session.rb', line 11

def name
  @name
end

Instance Method Details

#closeObject



90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/xlogin/session.rb', line 90

def close
  @mutex.synchronize do
    @output_loggers.each do |output_log, logger|
      next unless logger
      logger.close if output_log.kind_of?(String)
    end
    @gateway.shutdown! if @gateway

    super
    @closed = true
  end
end

#closed?Boolean



103
104
105
# File 'lib/xlogin/session.rb', line 103

def closed?
  @closed
end

#cmd(*args, &block) ⇒ Object



63
64
65
# File 'lib/xlogin/session.rb', line 63

def cmd(*args, &block)
  @mutex.synchronize { super(*args, &block) }
end

#disable_log(out = $stdout) ⇒ Object



119
120
121
122
123
124
125
# File 'lib/xlogin/session.rb', line 119

def disable_log(out = $stdout)
  @output_loggers = build_loggers(@output_logs - [out])
  if block_given?
    yield
    @output_loggers = build_loggers
  end
end

#duplicateObject



107
108
109
# File 'lib/xlogin/session.rb', line 107

def duplicate
  @template.build(@uri, **config.to_h)
end

#enable_log(out = $stdout) ⇒ Object



111
112
113
114
115
116
117
# File 'lib/xlogin/session.rb', line 111

def enable_log(out = $stdout)
  @output_loggers = build_loggers(@output_logs + [out])
  if block_given?
    yield
    @output_loggers = build_loggers
  end
end

#initialize(template, uri, **opts) ⇒ Object

Raises:



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/xlogin/session.rb', line 14

def initialize(template, uri, **opts)
  @uri  = uri
  @host = uri.host
  @port = uri.port
  @port ||= case @uri.scheme
            when 'ssh'    then 22
            when 'telnet' then 23
            end

  raise SessionError.new("Invalid URI - '#{uri}'") unless @host && @port

  @name     = opts.delete(:name) || @host
  @config   = OpenStruct.new(opts)
  @template = template
  @username, @password = uri.userinfo.to_s.split(':')

  ssh_tunnel(@config.via) if @config.via
  max_retry = @config.retry || 1

  @mutex  = Mutex.new
  @closed = false
  @output_logs    = [@config.log]
  @output_loggers = build_loggers

  begin
    super(
      'Host'     => @host,
      'Port'     => @port,
      'Username' => @username,
      'Password' => @password,
      'Timeout'  => @config.timeout || @template.timeout || false,
      'Prompt'   => Regexp.union(*@template.prompt.map(&:first)),
      'FailEOF'  => true,
    )
  rescue => e
    retry if (max_retry -= 1) > 0
    @closed = true
    raise e
  end
end

#promptObject



59
60
61
# File 'lib/xlogin/session.rb', line 59

def prompt
  cmd('').lines.last.chomp
end

#puts(*args, &block) ⇒ Object



67
68
69
70
# File 'lib/xlogin/session.rb', line 67

def puts(*args, &block)
  args = instance_exec(*args, &@template.interrupt) if @template.interrupt
  super(*args, &block)
end

#typeObject



55
56
57
# File 'lib/xlogin/session.rb', line 55

def type
  @template.name
end

#waitfor(*args, &block) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/xlogin/session.rb', line 72

def waitfor(*args, &block)
  args << Regexp.union(*@template.prompt.map(&:first)) if args.empty?
  line = super(*args) do |recv|
    block.call(recv) if block
    output_log(recv)
  end

  _, process = @template.prompt.find { |r, p| r =~ line && p }
  if process
    instance_eval(&process)
    line += waitfor(*args, &block)
  end
rescue EOFError
  @closed = true
ensure
  return line
end