Module: Mimas::SSH::Interactive

Included in:
Mimas::SSH
Defined in:
lib/mimas/ssh/interactive.rb

Instance Method Summary collapse

Instance Method Details

#interactive_ssh(host, user:, command:) ⇒ Object



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
# File 'lib/mimas/ssh/interactive.rb', line 4

def interactive_ssh(host, user:, command:)
  system('stty raw -echo')

  Net::SSH.start(host, user) do |session|
    session.open_channel do |channel|
      channel.request_pty do
        channel.exec(command) do
          channel.on_data do |_, data|
            $stdout.print(data) if data
          end

          channel.on_extended_data do |_, data|
            $stdout.print(data) if data
          end

          session.listen_to($stdin) do |stdin|
            input = stdin.readpartial(1024)
            channel.send_data(input) unless input.empty?
          end
        end
      end
    end
    session.loop
  end
ensure
  system('stty sane')
end