如何在 Windows 终端中打开多个选项卡并从命令行运行 ssh?

Ana*_*rKi 6 shell windows-terminal wsl2

我有 WSL2 和 Ubuntu 实例,有没有办法在 Windows 终端中从命令行打开多个选项卡并在其中运行内容?

为了更详细,我想做以下事情,所以请告知是否有办法:

  1. 手动打开 Windows 终端(点​​击它)。这里我已经设置Ubuntu是我的默认值
  2. 在我的 Ubuntu 选项卡中,我想要一个可以运行的 shell 脚本
  3. shell 脚本应该像 Ubuntu 一样打开一些 n 个选项卡
  4. 然后在每个新选项卡中自动运行特定的 ssh(登录到远程计算机)。
  5. 奖金为我刚刚打开的每个选项卡设置颜色

我目前手动完成所有这些工作,并希望以某种方式实现自动化。

我说这篇文章https://wslguy.net/2021/05/19/windows-terminal-bash-shell-open-new-tab-in-same-directory/其中有一些指标并承诺这可能会发生,但是我不知道该怎么做。

Not*_*1ds 6

请注意,以下内容需要 Windows 11。对于 Windows 10,请根据@AnarKi 的回答cmd.exe /c进行调用。原因请参阅本答案底部的脚注。wt.exe

为了清楚起见,使用每个选项的长格式,该 shell 脚本通常应为:

#!/usr/bin/env sh
wt.exe --window 0 new-tab --profile "Ubuntu" --title "host1" --tabColor "#F00" ssh host1
wt.exe --window 0 new-tab --profile "Ubuntu" --title "host2" --tabColor "#0F0" ssh host2
wt.exe --window 0 new-tab --profile "Ubuntu" --title "host3" --tabColor "#00F" ssh host3
Run Code Online (Sandbox Code Playgroud)

但是,您可能会发现:

  • Windows 终端的 Ubuntu 配置文件命令行中的某些内容
  • 或者 Ubuntu shell 启动脚本中的某些内容
  • 或者(虽然我没有看到这个问题),正如您链接的文章提到的,起始目录目录

...正在妨碍它应有的“干净”。在这种情况下,我建议设置一个新的 Windows 终端配置文件,该配置文件将仅使用/bin/sh.

然后使用:

#!/usr/bin/env sh
wt.exe --window 0 new-tab --profile "Ubuntu /bin/sh" --title "host1" --tabColor "#F00" ssh host1
wt.exe --window 0 new-tab --profile "Ubuntu /bin/sh" --title "host2" --tabColor "#0F0" ssh host2
wt.exe --window 0 new-tab --profile "Ubuntu /bin/sh" --title "host3" --tabColor "#00F" ssh host3
Run Code Online (Sandbox Code Playgroud)

脚注:Windows 终端wt.exe被称为“应用程序执行别名”。它本质上是一个.exe旨在启动 Microsoft Store(又名“UWP”、“Universal”、“Metro”和“Modern”)应用程序的应用程序,而不是“真正的”Windows 二进制文件。

在 Windows 10 下,WSL 无法运行应用程序执行别名,因为它们不是由 WSL binfmt_misc实现处理的“真正”可执行文件。

但是,在 Windows 11 下,这似乎已更改,并且应用程序执行别名在 WSL 中得到了正确处理。

这只是一个理论,但这可能是为了处理将 WSL 本身添加到 Microsoft Store 中而添加的(目前在 Windows 11 下处于预览版)。这将允许该wsl.exe命令(现在可能是应用程序执行别名(从应用商店安装时))仍然在 WSL 本身中运行。