Class: Harrison::SSH

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

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ SSH

Returns a new instance of SSH.



7
8
9
10
11
12
13
14
# File 'lib/harrison/ssh.rb', line 7

def initialize(opts={})
  if opts[:proxy]
    @proxy = Net::SSH::Proxy::Command.new("ssh #{opts[:proxy]} \"nc %h %p\" 2>/dev/null")
    @conn = Net::SSH.start(opts[:host], opts[:user], forward_agent: true, proxy: @proxy, timeout: 10)
  else
    @conn = Net::SSH.start(opts[:host], opts[:user], forward_agent: true, timeout: 10)
  end
end

Instance Method Details

#closeObject



45
46
47
48
49
# File 'lib/harrison/ssh.rb', line 45

def close
  # net-ssh doesn't seem to know how to close proxy::command connections
  Process.kill("TERM", @conn.transport.socket.pid) if @proxy
  @conn.close
end

#closed?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/harrison/ssh.rb', line 51

def closed?
  @conn.closed?
end

#descObject



55
56
57
58
59
60
61
# File 'lib/harrison/ssh.rb', line 55

def desc
  if @proxy
    "#{@conn.host} (via #{@proxy.command_line.split(' ')[1]})"
  else
    @conn.host
  end
end

#download(remote_path, local_path) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/harrison/ssh.rb', line 29

def download(remote_path, local_path)
  puts "[#{desc}] INFO: scp-down #{local_path} <<< #{remote_path}" if Harrison::DEBUG

  @conn.scp.download!(remote_path, local_path)

  return true
end

#exec(command) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/harrison/ssh.rb', line 16

def exec(command)
  puts "[#{desc}] INFO: ssh-exec #{command}" if Harrison::DEBUG

  result = invoke(@conn, command)

  if Harrison::DEBUG || result[:status] != 0
    warn "[#{desc}] STDERR: #{result[:stderr]}" unless result[:stderr].empty?
    warn "[#{desc}] STDOUT: #{result[:stdout]}" unless result[:stdout].empty?
  end

  (result[:status] == 0) ? result[:stdout] : nil
end

#hostObject



63
64
65
# File 'lib/harrison/ssh.rb', line 63

def host
  @conn.host
end

#upload(local_path, remote_path) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/harrison/ssh.rb', line 37

def upload(local_path, remote_path)
  puts "[#{desc}] INFO: scp-up #{local_path} >>> #{remote_path}" if Harrison::DEBUG

  @conn.scp.upload!(local_path, remote_path)

  return true
end