透過 ssh 執行遠端腳本時使用者「讀取」命令

透過 ssh 執行遠端腳本時使用者「讀取」命令

我先說明一下我的情況。我在 .bashrc 有一個別名:

doscript () { ssh root@server$1 -p2202 "bash -s" < ~/scripts/$2 $3; }

我有一個腳本,基本上由幾個操作組成,例如:

check current settings
read comment
do actions
write $comment in a file
restart service

所以我運行一個命令

doscript server scriptname parameter

但是“閱讀評論”部分不起作用。它不會等到我輸入評論,而是立即執行,不會在評論文件中添加任何內容。

在這種情況下有沒有辦法輸入變數?

答案1

在不進行太多更改的情況下執行此操作的常用方法是將檔案複製到遠端,然後執行它,從而使 stdin 可用於 tty 輸入。例如

scp -P 2202 ~/scripts/$2 root@server$1:./myscript
ssh -t root@server$1 -p2202 bash ./myscript $3

或者如果您無法scp將該行替換為

ssh root@server$1 -p2202 'cat >./myscript' <~/scripts/$2

相關內容