Skip to main content
Interactive shell sessions allow you to maintain a persistent connection with a command-line interface on the remote server. This is useful for commands that require continuous interaction or when you need to maintain state across multiple commands.

Starting a Shell Session

Use the startShell() method to initiate an interactive shell session:

PTY (Pseudo-Terminal) Types

The library supports multiple PTY types for different terminal emulation needs:
Basic terminal type with minimal features. Use this for simple command execution without special terminal requirements.
Choose the PTY type based on your needs:
  • Use VANILLA for simple scripts and basic command execution
  • Use XTERM for full-featured interactive sessions
  • Use VT100/VT102 for compatibility with older systems

Reading from the Shell

Set up an event listener to receive output from the shell:
1

Register the Shell event handler

2

Start the shell session

3

Process incoming data

The Shell event will fire whenever the remote shell sends output:

Writing to the Shell

Send commands to the active shell session using writeToShell():
Don’t forget to include the newline character (\n) at the end of your commands to execute them. Without it, the command will be typed but not executed.

Complete Example

Here’s a full example of an interactive shell session:

Automatic Shell Initialization

The writeToShell() method automatically starts a shell session (using PtyType.VANILLA) if one isn’t already active. However, it’s recommended to explicitly call startShell() with your desired PTY type for better control.

Closing the Shell

When you’re done with the shell session, close it to free up resources:
  • Closing the shell will unregister the Shell event listener
  • Any pending output may be lost when the shell is closed
  • After closing, you need to call startShell() again to start a new session

State Persistence

One of the key advantages of shell sessions over execute() is state persistence:

Best Practices

  1. Always register event handlers before starting the shell to avoid missing initial output
  2. Add delays between commands if you need to wait for output (or implement a proper response parser)
  3. Use appropriate PTY types for your use case
  4. Close the shell when done to free resources
  5. Handle errors appropriately as shell operations can fail

Use Cases

  • Running interactive programs (database CLIs, REPLs, etc.)
  • Executing a series of related commands that depend on shared state
  • Monitoring real-time output from long-running processes
  • Working with shell scripts that require user input
  • Maintaining a persistent working directory across commands

Next Steps

Last modified on March 26, 2026