Module: SimpleSSH

Defined in:
lib/simplessh.rb,
lib/simplessh/either.rb,
lib/simplessh/result.rb,
lib/simplessh/foreign.rb,
lib/simplessh/helpers.rb,
lib/simplessh/session.rb

Defined Under Namespace

Modules: Foreign, Helpers Classes: Either, Result, Session

Class Method Summary collapse

Class Method Details

.open_session(hostname, port, known_hosts) ⇒ Object



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

def self.open_session(hostname, port, known_hosts)
  either_pointer = SimpleSSH::Foreign.open_session(hostname, port, known_hosts)

  SimpleSSH::Either.from_pointer!(either_pointer) do |sess_pointer|
    SimpleSSH::Session.from_pointer(sess_pointer)
  end
end

.with_session_key(hostname, username, opts = {}, &block) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/simplessh.rb', line 32

def self.with_session_key(hostname, username, opts = {}, &block)
  port        = opts[:port] || 22
  passphrase  = opts[:passphrase]  || ""
  public_key  = opts[:public_key]  || File.join(Dir.home, ".ssh", "id_rsa.pub")
  private_key = opts[:private_key] || File.join(Dir.home, ".ssh", "id_rsa")
  known_hosts = opts[:known_hosts] || File.join(Dir.home, ".ssh", "known_hosts")

  sess = self.open_session(hostname, port, known_hosts).flat_map do |s|
    s.authenticate_with_key(username, public_key, private_key, passphrase)
  end

  res = sess.flat_map do |s|
    block.call s
  end

  sess.flat_map { |s| s.close }
  res
end

.with_session_password(hostname, username, password, opts = {}, &block) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/simplessh.rb', line 16

def self.with_session_password(hostname, username, password, opts = {}, &block)
  port        = opts[:port] || 22
  known_hosts = opts[:known_hosts] || File.join(Dir.home, ".ssh", "known_hosts")

  sess = self.open_session(hostname, port, known_hosts).flat_map do |s|
    s.authenticate_with_password(username, password)
  end

  res = sess.flat_map do |s|
    block.call s
  end

  sess.flat_map { |s| s.close }
  res
end