Feasibility of Specifying a Startup File When Opening an SSH2 Session

This post explores whether an SSH2-based web terminal can specify a startup file when opening an interactive session, outlines possible approaches, and notes their trade-offs.

Jun 29, 2024 · 1 min read · 160 Words · -Views -Comments · Programming
WebShell Linux Development Tools
Read with AI

Choose an AI assistant

ChatGPT Open in a new tab with the article link and prompt
Claude Copy the prompt, then open Claude

Open Claude
Trouble reading the article?

If the prompt is missing, copy the full prompt below into your AI assistant. If the AI cannot read the article, paste the visible passage you want to discuss and add your question.

While reading the inshellisense code, I noticed it controls loading a startup file from a specific directory when spawning a local shell. That made me wonder: can an SSH2 web terminal session do something similar? Here’s a quick feasibility discussion.

inshellisense’s approach

Taking bash as an example:

shellArgs = ["--init-file", path.join(shellFolderPath, "shellIntegration.bash")];

this.#pty = pty.spawn(shellTarget, shellArgs ?? [], {
      name: "xterm-256color",
      cols,
      rows,
      cwd: process.cwd(),
      env: { ...convertToPtyEnv(shell, underTest, login), ...env },
    });    

This works by passing shell-specific arguments as the second parameter to spawn; for bash, there is an --init-file option.

https://static.1991421.cn/2024/2024-06-29-235653.jpeg

SSH2

https://github.com/mscdex/ssh2?tab=readme-ov-file#client-methods

There is no analogous parameter in the SSH2 connect method, so you can’t pass a startup file that way.

Two possible workarounds:

  1. In an interactive session, run source to load your integration script. Downside: it will produce visible echo/output artifacts.

    stream.write('');
    
  2. Modify the shell’s startup file (e.g., .bashrc) to load your script by default. This avoids echo issues on subsequent connections.

    conn.exec('');
    

​

Final Thoughts

Done.

Authors
Developer, digital product enthusiast, tinkerer, sharer, open source lover