Class: Antimony::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/antimony.rb,
lib/antimony/session.rb

Constant Summary collapse

KEYBOARD_METHODS =
{
  f1: "\x1B\x31".freeze,
  f2: "\x1B\x32".freeze,
  f3: "\x1B\x33".freeze,
  f4: "\x1B\x34".freeze,
  f5: "\x1B\x35".freeze,
  f6: "\x1B\x36".freeze,
  f7: "\x1B\x37".freeze,
  f8: "\x1B\x38".freeze,
  f9: "\x1B\x39".freeze,
  f10: "\x1B\x30".freeze,
  f11: "\x1B\x2D".freeze,
  f12: "\x1B\x3D".freeze,
  f13: "\x1B\x21".freeze,
  f14: "\x1B\x40".freeze,
  f15: "\x1B\x23".freeze,
  f16: "\x1B\x24".freeze,
  f17: "\x1B\x25".freeze,
  f18: "\x1B\x5E".freeze,
  f19: "\x1B\x26".freeze,
  f20: "\x1B\x2A".freeze,
  f21: "\x1B\x28".freeze,
  f22: "\x1B\x29".freeze,
  f23: "\x1B\x5F".freeze,
  f24: "\x1B\x2B".freeze,
  enter: "\n".freeze,
  tab: "\t".freeze,
  backspace: "\177".freeze,
  arrow_up: "\e[A".freeze,
  arrow_down: "\e[B".freeze,
  arrow_right: "\e[C".freeze,
  arrow_left: "\e[D".freeze
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(host, username, password) ⇒ Session

Returns a new instance of Session.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/antimony/session.rb', line 4

def initialize(host, username, password)
  @cursor_position = 0
  @screen_text = blank_screen
  @session_log = []

  puts "Establishing SSH tunnel ..."
  @gateway = Net::SSH::Gateway.new(host, username, :password => password)
  tunnel_port = @gateway.open(host, 23)

  puts "Connecting to #{host} using local port #{tunnel_port} ..."
  @connection = Net::Telnet.new(
    'Host' => 'localhost',
    'Port' => tunnel_port,
    'Timeout' => 10
  )

  update_screen
end

Instance Method Details

#closeObject



23
24
25
26
# File 'lib/antimony/session.rb', line 23

def close
  @connection.close
  @gateway.shutdown!
end

#log(text) ⇒ Object



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

def log(text)
  @session_log.push(text)
end

#log_screenObject



71
72
73
# File 'lib/antimony/session.rb', line 71

def log_screen
  log(printable_screen_text.join("\n"))
end


50
51
52
53
54
# File 'lib/antimony/session.rb', line 50

def print_screen
  puts SCREEN_SEPARATOR
  puts printable_screen_text
  puts SCREEN_SEPARATOR
end

#printable_screen_textObject



46
47
48
# File 'lib/antimony/session.rb', line 46

def printable_screen_text
  @screen_text.join.scan(LINE)
end

#screen_textObject



42
43
44
# File 'lib/antimony/session.rb', line 42

def screen_text
  @screen_text.join
end

#send_keys(keys, count = 1, text = true) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/antimony/session.rb', line 28

def send_keys(keys, count = 1, text = true)
  count.times { @connection.print keys }
  update_screen
  add_text(keys) if text
  log_screen
  print_screen if Antimony.configuration.show_output
end

#session_logObject



56
57
58
59
60
61
62
63
64
65
# File 'lib/antimony/session.rb', line 56

def session_log
  txt = EMPTY.clone
  @session_log.each_with_index do |entry, i|
    txt += LOG_SEPARATOR + ENTER
    txt += "#{i}: #{ENTER}"
    txt += entry + ENTER
  end
  txt += LOG_SEPARATOR
  txt
end

#value_at(row, column, length) ⇒ Object



36
37
38
39
40
# File 'lib/antimony/session.rb', line 36

def value_at(row, column, length)
  start_index = ((row - 1) * 80) + (column - 1)
  end_index = start_index + length - 1
  @screen_text[start_index..end_index].join
end