Class: Microsandbox::SshClient
- Inherits:
-
Object
- Object
- Microsandbox::SshClient
- 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.
Instance Method Summary collapse
-
#attach(term: nil, detach_keys: nil) ⇒ Integer
Attach the local terminal to an interactive SSH shell.
-
#close ⇒ nil
Close the SSH client session.
-
#exec(command, tty: false) ⇒ SshOutput
Run a command over SSH and collect its output.
-
#initialize(native) ⇒ SshClient
constructor
A new instance of SshClient.
-
#sftp {|sftp| ... } ⇒ SftpClient, Object
Open an SFTP session over this connection.
Constructor Details
#initialize(native) ⇒ SshClient
Returns a new instance of SshClient.
144 145 146 |
# File 'lib/microsandbox/ssh.rb', line 144 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.
162 163 164 |
# File 'lib/microsandbox/ssh.rb', line 162 def attach(term: nil, detach_keys: nil) @native.attach(term&.to_s, detach_keys&.to_s) end |
#close ⇒ nil
Close the SSH client session. Idempotent. Sends the graceful protocol
disconnect (russh Disconnect::ByApplication); if the client is instead
left to GC, only the in-process server task is aborted and that disconnect
is skipped. Prefer the block form of Microsandbox::SshOps#open_client, or call this in
an ensure, so the session closes cleanly.
189 190 191 192 |
# File 'lib/microsandbox/ssh.rb', line 189 def close @native.close nil end |
#exec(command, tty: false) ⇒ SshOutput
Run a command over SSH and collect its output.
152 153 154 |
# File 'lib/microsandbox/ssh.rb', line 152 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. Without a block you own the returned Microsandbox::SftpClient and must call Microsandbox::SftpClient#close (letting it GC skips the graceful disconnect).
172 173 174 175 176 177 178 179 180 181 |
# File 'lib/microsandbox/ssh.rb', line 172 def sftp session = SftpClient.new(@native.sftp) return session unless block_given? begin yield session ensure session.close end end |