gitmyhub

net-ssh-shell

Ruby ★ 79 updated 11y ago ⑂ fork ▣ archived

A net-ssh extension library that provides an API for programmatically interacting with a login shell

A Ruby library that keeps a persistent shell session open on a remote server, so commands share state like directory changes and environment variables across multiple executions.

RubyNet::SSHRubyGemssetup: easycomplexity 2/5

Net-ssh-shell is a Ruby library that lets you run commands on a remote server as if you were typing them into the same terminal session. Normally, when you run commands on a remote machine programmatically, each command runs in its own isolated bubble. This library changes that by keeping a single, persistent shell open, so anything you do in one command carries over to the next.

The key benefit is statefulness. If you run "cd /usr/local" to change directories and then run "pwd" to see where you are, the second command remembers the directory change from the first. You can also set environment variables in one command and use them in the next. Without this library, you would need to chain everything into a single command or manage complex state tracking yourself.

It works as an extension to Net::SSH, a popular Ruby library for connecting to remote servers. You open a connection, start a shell session, and then execute commands one after another within that session. Each command returns an object that includes details like the exit status and the command that was run, so you can check whether things succeeded. The library also supports "subshell" environments, meaning you can switch users or enter a privileged shell like "sudo bash" and continue running commands within that elevated context.

This would be useful for anyone building infrastructure automation, deployment scripts, or configuration management tools in Ruby. For example, if you are writing a script that connects to a server, switches to a specific directory, sets some environment variables, and then runs a build command, this library lets you write those steps naturally instead of cramming them into a single line. It is also handy for scenarios where you need to escalate privileges mid-session or interact with shells that expect a ongoing conversation rather than one-off commands.

The project is relatively small and focused, doing one thing well rather than trying to be a full automation framework. It requires Net::SSH version 2 and installs as a standard Ruby gem.

Where it fits