當命令在 ssh 會話中完成時在 OSX 上執行本機命令

當命令在 ssh 會話中完成時在 OSX 上執行本機命令

在我的 OS/X 筆記型電腦上,我可以輸入“說完成”,而我的 Mac 則顯示“完成”。

在本地,我只會這樣做:

do-something.sh && say done

這讓我可以在單獨的視窗/桌面上亂搞,直到腳本完成。

但現在我從我的 Mac ssh 到遠端 Linux 機器。我想要的是能夠告訴我的 Mac 在特定命令完成時運行“說完成”。有什麼好的辦法嗎?

答案1

向同事 A Golden 致敬,他立刻想出了這個辦法。這是他們的回應的剪切和粘貼,它有效並且完全解決了這個問題。

I picked "say-hi-locally" and set it up on my linux server as:
alias say-hi-locally="echo EMIT SAY HI"

Then, since I use ITerm2 as my local terminal, I set up a trigger in ITerm2 with the regexp "EMIT SAY HI" and told that to run the local command "say hi"

Now I can do:
do-something.sh; say-hi-locally

and when it finishes it will echo "EMIT SAY HI" which ITerm2 will then pick up and then my computer says "hi" to me! :-)

答案2

在你的 Mac 上,啟動一個 TCP 伺服器,它將接收到的任何內容傳送到say

mac $ while true; do nc -l 1234 | say; done

然後當您使用 SSH 時,使用-R將連接埠 1234 轉送到您的伺服器:

mac $ ssh -R 1234:localhost:1234 linux

現在,您的 Linux 伺服器應該有一個到 Mac 上的 nc 伺服器的隧道,並且可以用來nc向其發送文字:

linux $ echo done | nc localhost 1234

答案3

你的 Mac 上需要有一個 ssh 伺服器。然後您可以執行以下操作:

[you@mac]$ ssh linux
[you@linux]$ interactive_cmd; ssh mac say done

您登入您的 linux 主機,執行命令,完成後,linux 主機 ssh 會返回您的 mac 並執行該say命令。

答案4

ssh linux do-something.sh && say done

看起來就像在做你想做的事。在遠端主機和你的 Mac 之間實現一些通訊似乎不可行。

如果您想運行更多命令並且擔心延遲、使用ControlMasterControlPersist選項(許多問題已經得到解答)。

相關內容