-
Notifications
You must be signed in to change notification settings - Fork 0
/
plex.rb
executable file
·190 lines (147 loc) · 4.12 KB
/
plex.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
require 'net/ssh'
class Plex
attr_reader :hostname
# https://stackoverflow.com/a/29497680
ANSI_ESCAPE_CODES = Regexp.new('[\u001b\u009b][\[();?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]')
CONNECTION_ERRORS = [ SocketError, Errno::ECONNREFUSED ]
def initialize(hostname, **kwargs)
@hostname = hostname
@agent = kwargs[:agent] || false
@logger = kwargs[:logger] || Logger.new
@mode = kwargs[:mode] || :exec
@out = kwargs[:out] || STDOUT
@user = kwargs[:user] # when nil ~/.ssh/config is used
@buffer = []
@idle = false
@eof = false
@prompt = Regexp.new('(^% | \d+ # | \d+ \$ )$') # ymmv
end
def open
begin
@ssh = Net::SSH.start(@hostname,
@user,
forward_agent: @agent,
logger: @logger)
rescue *CONNECTION_ERRORS => e
@logger.error("can't connect to #{@hostname}: #{e}")
@ssh = nil
end
case @mode
when :exec
@idle = true # exec channel is opened every time, so consider it idle at start
when :shell
@idle = false # process shell channel setup before accepting commands
self.shell
end
end
def shell
return unless @ssh
@shell = @ssh.open_channel do |channel|
@logger.info("opened shell channel to #{@hostname}")
channel.request_pty do |ch, success|
raise RuntimeError, "pty failed" unless success
end
channel.send_channel_request("shell") do |ch, success|
raise RuntimeError, "shell failed" unless success
ch.on_data do |ch, data|
@buffer << data
end
ch.on_extended_data do |ch, type, data|
@buffer << data
end
ch.on_request("exit-status") do |ch, data|
code = data.read_long
@buffer << "# exit: #{code}\n"
@eof = true
end
ch.on_request("exit-signal") do |ch, data|
signal = data.read_long
@buffer << "# signal: #{signal}\n"
@eof = true
end
end
end
end
def exec(cmd)
return unless @ssh
@exec = @ssh.open_channel do |channel|
@logger.info("opened exec channel to #{@hostname}")
channel.request_pty do |ch, success|
raise RuntimeError, "pty failed" unless success
end
channel.exec(cmd) do |ch, success|
raise RuntimeError, "exec failed" unless success
ch.on_data do |ch, data|
@buffer << data
end
ch.on_extended_data do |ch, type, data|
@buffer << data
end
ch.on_request("exit-status") do |ch, data|
code = data.read_long
@buffer << "# exit: #{code}\n"
@idle = true
end
ch.on_request("exit-signal") do |ch, data|
signal = data.read_long
@buffer << "# signal: #{signal}\n"
@idle = true
end
end
end
end
def process
@ssh.process(0.1)
unless @buffer.empty?
# join buffer lines, strip out ANSI chaos
clean = @buffer.join.gsub(ANSI_ESCAPE_CODES, '').gsub("\r", "")
# emit all the \n terminated lines, keep the current in-progress line
lines = clean.split(/\n/, -1)
current = lines.pop
emit = lines.join("\n")
unless emit.empty?
@out.puts emit
end
if current.empty?
@buffer = [] # avoid a buffer of just ""
else
@buffer = [ current ]
end
if current.match(@prompt)
@idle = true
@out.puts current.gsub(@prompt, '')
@buffer = []
end
end
end
def send(data)
return if @eof
@idle = false
case @mode
when :shell
@buffer = [ 'sshplex% ' ] # add synthetic prompt to output
@shell.send_data(data + "\n")
when :exec
@buffer = [ "# exec: #{data}\n" ] # record command in output
self.exec(data)
end
end
def etx!
return if @eof
case @mode
when :shell
@shell.send_data("\x03")
when :exec
@exec.send_data("\x03")
end
end
def connected?
end
def eof?
@eof
end
def idle?
@idle
end
end