使用 byobu 连接到远程服务器并运行命令

使用 byobu 连接到远程服务器并运行命令

我有一个远程服务器,我通常使用 ssh 连接到它,连接后 byobu 会自动启动(通过 .profile 中使用脚本设置的一行byobu-enable)。现在,我想在连接到同一服务器以使用远程 jupyter 笔记本时设置不同的工作流程。我希望远程服务器启动 jupyter,然后让 ssh 将jupyter端口转发到我的客户端计算机。我将其添加到本地 .ssh/config 中

Host remote-server-jupyter
    HostName      123.45.6.789
    User          pgcudahy
    LocalForward  8889 localhost:8889
    ServerAliveInterval 30
    ServerAliveCountMax 3
    RemoteCommand cd ~/Projects && jupyter notebook --no-browser --port=8889

问题是“RemoteCommand”会干扰 byobu 启动,因此在连接并运行该命令后,我留下的是纯文本 shell,而不是漂亮的多路复用屏幕。如何在连接时同时获取 byobu 和远程命令?

重要的是,我不希望这些命令在每个连接上运行,除非我指定需要特定的工作流程。显然,我可以连接到 byobu,然后在服务器上运行脚本来设置我的工作区,但我想将这一切包装到来自客户端的一个自动命令中。更好的是拥有单独的配置文件,不仅运行自定义命令,还设置一个具有多个窗口和每个窗口中不同命令的自定义 byobu 工作区。

答案1

答案的关键在于这个堆栈溢出问题。使用该ssh -t标志打开交互式伪终端。然后byobu new-sessionbyobu send-keys命令传递给 byobu 会话。

首先在上创建一个 .ssh/config当地的机器建立ssh连接

Host remote-server-jupyter
    HostName      123.45.6.789
    User          pgcudahy
    LocalForward  8889 localhost:8889
    ServerAliveInterval 30
    ServerAliveCountMax 3

然后在主目录下放置一个脚本偏僻的设置 byobu 会话的机器。我需要一个脚本而不是命令列表,以便我可以测试是否已创建“jupyter”会话。我将其命名为remote-jupyter-startup.sh

#!/bin/bash
# Test if there's a 'jupyter' session already set up
if [ -z "$(byobu list-sessions | grep jupyter)" ]
    # If not, then set up the session
    then
    # Create a new detached session named 'jupyter'
    byobu new-session -d -s jupyter
    # Pass a 'cd' command to the session, then 'C-m' to execute
    byobu send-keys -t jupyter 'cd ~/Projects' 'C-m'
    # Pass the 'jupyter' command to the session, then 'C-m' to execute
    byobu send-keys -t jupyter 'jupyter notebook --no-browser --port=8889' 'C-m'
    # Create a second window in the session
    byobu new-window -t jupyter:1
fi
# Attach to the session
byobu attach-session -t jupyter

使其可执行

chmod +x remote-jupyter-startup.sh

现在在当地的我可以运行的机器

ssh remote-server-jupyter -t "./remote-jupyter-startup.sh;"

相关内容