· 1 分钟阅读时长 · 208 字 · -阅读 -评论

title: “Feasibility of Specifying a Startup File When Opening an SSH2 Session” tags:

  • WebShell
  • Linux
  • Development Tools slug: c57a1702 date: 2024-06-29 23:51:19 summary: “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.”

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.

Alan H
Authors
开发者,数码产品爱好者,喜欢折腾,喜欢分享,喜欢开源