Module: Xlogin::SessionModule
Instance Attribute Summary collapse
-
#config ⇒ Object
Returns the value of attribute config.
-
#name ⇒ Object
Returns the value of attribute name.
Instance Method Summary collapse
- #close ⇒ Object
- #closed? ⇒ Boolean
- #cmd(*args, &block) ⇒ Object
- #disable_log(out = $stdout) ⇒ Object
- #duplicate ⇒ Object
- #enable_log(out = $stdout) ⇒ Object
- #initialize(template, uri, **opts) ⇒ Object
- #prompt ⇒ Object
- #puts(*args, &block) ⇒ Object
- #type ⇒ Object
- #waitfor(*args, &block) ⇒ Object
Instance Attribute Details
#config ⇒ Object
Returns the value of attribute config.
13 14 15 |
# File 'lib/xlogin/session.rb', line 13 def config @config end |
#name ⇒ Object
Returns the value of attribute name.
12 13 14 |
# File 'lib/xlogin/session.rb', line 12 def name @name end |
Instance Method Details
#close ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/xlogin/session.rb', line 91 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
104 105 106 |
# File 'lib/xlogin/session.rb', line 104 def closed? @closed end |
#cmd(*args, &block) ⇒ Object
64 65 66 |
# File 'lib/xlogin/session.rb', line 64 def cmd(*args, &block) @mutex.synchronize { super(*args, &block) } end |
#disable_log(out = $stdout) ⇒ Object
120 121 122 123 124 125 126 |
# File 'lib/xlogin/session.rb', line 120 def disable_log(out = $stdout) @output_loggers = build_loggers(@output_logs - [out]) if block_given? yield @output_loggers = build_loggers end end |
#duplicate ⇒ Object
108 109 110 |
# File 'lib/xlogin/session.rb', line 108 def duplicate @template.build(@uri, **config.to_h) end |
#enable_log(out = $stdout) ⇒ Object
112 113 114 115 116 117 118 |
# File 'lib/xlogin/session.rb', line 112 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
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 54 |
# File 'lib/xlogin/session.rb', line 15 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 |
#prompt ⇒ Object
60 61 62 |
# File 'lib/xlogin/session.rb', line 60 def prompt cmd('').lines.last.chomp end |
#puts(*args, &block) ⇒ Object
68 69 70 71 |
# File 'lib/xlogin/session.rb', line 68 def puts(*args, &block) args = instance_exec(*args, &@template.interrupt) if @template.interrupt super(*args, &block) end |
#type ⇒ Object
56 57 58 |
# File 'lib/xlogin/session.rb', line 56 def type @template.name end |
#waitfor(*args, &block) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/xlogin/session.rb', line 73 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 |