Class: Remoting::Ssh

Inherits:
Object
  • Object
show all
Defined in:
lib/remoting/ssh.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Ssh

Returns a new instance of Ssh.



8
9
10
11
# File 'lib/remoting/ssh.rb', line 8

def initialize(*args)
  options = args.extract_options!
  @host, @user, @interactive = options.values_at(:host, :user, :interactive)  
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



6
7
8
# File 'lib/remoting/ssh.rb', line 6

def host
  @host
end

#userObject (readonly)

Returns the value of attribute user.



6
7
8
# File 'lib/remoting/ssh.rb', line 6

def user
  @user
end

Instance Method Details

#exec(commands) ⇒ Object



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
# File 'lib/remoting/ssh.rb', line 18

def exec(commands)

  if interactive?
    ssh = %(ssh #{user}@#{host})
    Kernel.exec ssh + %( -t '#{commands.join("; ")}')  # this will replace the current process and will never return the shell back
  else
    Net::SSH.start(host, user) do |ssh|
  
      ssh.open_channel do |channel|
        channel.exec(commands.join(";")) do |ch, success|
          unless success
            abort
          end
        
          channel.on_data do |ch, data|
            say "#{data}"
          end

          channel.on_extended_data do |ch, type, data|
            shell.error "#{data}"
          end

          channel.on_close do |ch|
            shell.bold "channel is closing!\n"
          end
        end
      end

      ssh.loop 
    end        
  end
  
end

#interactive?Boolean

~ exec

Returns:

  • (Boolean)


52
53
54
# File 'lib/remoting/ssh.rb', line 52

def interactive?
  !!@interactive
end

#shellObject



14
15
16
# File 'lib/remoting/ssh.rb', line 14

def shell
  @shell ||= ::Remoting::Shell.new
end