Feasibility of Specifying a Startup File When Opening an SSH2 Session

Jun 29, 2024 · 1 min read · 160 Words · -Views -Comments

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