【问题标题】:How can I run complicated commands on establishing a mosh connection?如何在建立 mosh 连接时运行复杂的命令?
【发布时间】:2014-04-10 00:35:04
【问题描述】:

使用 ssh 我可以做到这一点:

ssh REMOTE -t 'tmux a || tmux'

对于 mosh,我能做的最好的事情是:

mosh REMOTE -- tmux a

但这行不通:

mosh REMOTE -- tmux a || tmux

这个也不行:(不管是单引号还是双引号,我都试过了)

mosh REMOTE -- 'tmux a || tmux'

所以,我的问题是:我应该如何做这份工作?

【问题讨论】:

    标签: shell ssh tmux mosh


    【解决方案1】:

    好吧,看来我必须显式使用shell来执行命令:

    mosh REMOTE -- sh -c 'tmux a || tmux'
    

    编辑

    除了tmux a || tmux,更好的方法是将new-session 添加到~/.tmux.conf,然后运行tmux。那会让事情变得容易得多。我现在可以这样做:

    mosh REMOTE -- tmux
    

    太棒了!

    【讨论】:

    • 我认为您在第二种解决方案中想要的是most remote -- tmux a,否则它每次都会创建一个新会话,而不是附加到已经存在的会话。
    • 你也可以tmux new-session -A -s somenameforsession
    • 不知道自从你回答后它是否改变了,但现在它是 mosh [options] -- user@host [command] 你把遥控器放在前面 -- :)
    【解决方案2】:

    可能有比上面给出的示例更复杂的命令。我想创建一个命令,如果一个现有的 tmux 会话存在但尚未附加,则重新附加到现有的会话,或者如果没有可用的,则重新附加到一个新的会话。

    看着this example,我会做这样的事情:

    function tmosh() {
        mosh $1 -- (tmux ls | grep -vq attached && tmux at -t $( tmux ls | grep -vm1 attached | cut -d: -f1 ) ) || tmux new
    }
    

    但这不起作用,根据上面的原始问题。

    到目前为止,我的解决方案是在主机服务器上有一个包装脚本:

    tmux-reattach-if-exists
    

    其中包括:

    (tmux ls | grep -vq attached && tmux at -t $( tmux ls | grep -vm1 attached | cut -d: -f1 )) || tmux new
    

    然后我使用从 mosh 调用客户端上的脚本,如下所示:

    function tmosh() {
        mosh $1 -- tmux-reattach-if-exists
    }
    

    如果有可以直接通过 .tmux.conf 执行此操作的解决方案,那就太好了,但我似乎无法解决。

    【讨论】:

      【解决方案3】:

      把这个放在.bashrc的末尾

      s1="`ps $PPID|grep mosh|awk '{print $5}'`"
      s2=mosh-server
      if [[ "$s1" == "$s2" ]]; then source .moshrc; fi
      

      如果被mosh-server 调用,bash 将执行它在$HOME/.moshrc 中找到的任何内容 - 所以只需将您的命令放在主目录中名为 .moshrc 的文件中。

      因为 mosh 调用了登录 shell,所以应该有一行

      source .bashrc
      

      在您的.bash_profile 中,或将以上行放入.bash_profile

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-11-25
        • 2018-10-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-01-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多