Class: Microsandbox::SshClient

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

Overview

A native, in-process SSH client session to a sandbox, from Microsandbox::SshOps#open_client. Mirrors the ‘SshClient` of the official SDKs.

Examples:

sb.ssh.open_client do |client|
  out = client.exec("uname -a")
  puts out.stdout
end

Instance Method Summary collapse

Constructor Details

#initialize(native) ⇒ SshClient

Returns a new instance of SshClient.



136
137
138
# File 'lib/microsandbox/ssh.rb', line 136

def initialize(native)
  @native = native
end

Instance Method Details

#attach(term: nil, detach_keys: nil) ⇒ Integer

Attach the local terminal to an interactive SSH shell. Host-TTY coupled (puts the terminal in raw mode and forwards SIGWINCH); blocks until the remote shell exits or the detach sequence is typed.

Parameters:

  • term (String, nil) (defaults to: nil)

    TERM value to request (defaults to $TERM)

  • detach_keys (String, nil) (defaults to: nil)

    detach key sequence (e.g. “ctrl-p,ctrl-q”)

Returns:

  • (Integer)

    the remote shell’s exit status



154
155
156
# File 'lib/microsandbox/ssh.rb', line 154

def attach(term: nil, detach_keys: nil)
  @native.attach(term&.to_s, detach_keys&.to_s)
end

#closenil

Close the SSH client session. Idempotent.

Returns:

  • (nil)


175
176
177
178
# File 'lib/microsandbox/ssh.rb', line 175

def close
  @native.close
  nil
end

#exec(command, tty: false) ⇒ SshOutput

Run a command over SSH and collect its output.

Parameters:

  • command (String)

    the command line (interpreted by the remote shell)

  • tty (Boolean) (defaults to: false)

    allocate a pseudo-terminal

Returns:



144
145
146
# File 'lib/microsandbox/ssh.rb', line 144

def exec(command, tty: false)
  SshOutput.new(@native.exec(command.to_s, tty ? true : false))
end

#sftp {|sftp| ... } ⇒ SftpClient, Object

Open an SFTP session over this connection. With a block, the session is yielded and closed when the block returns.

Yield Parameters:

Returns:



162
163
164
165
166
167
168
169
170
171
# File 'lib/microsandbox/ssh.rb', line 162

def sftp
  session = SftpClient.new(@native.sftp)
  return session unless block_given?

  begin
    yield session
  ensure
    session.close
  end
end