
我想透過 ssh 執行以下命令:
echo "trap '/tmp/test &' 2 9" >> /tmp/output.txt
我正在嘗試執行以下操作:
ssh 127.0.0.1 -T "echo "trap '/tmp/test &' 2 9" >> /tmp/output.txt"
我使用 python 子進程如下:
command = "echo \"trap '/tmp/test &' 2 9\" >> /tmp/output.txt"
addtrap = 'ssh root@' + rhost + ' -T "' + command + '"'
subprocess.Popen(addtrap, shell=True)
但是,顯然,我遇到了一些有關引號和撇號的錯誤。有什麼選擇可以避免這個問題?
答案1
一個可靠的方法是:
ssh 127.0.0.1 sh << "EOF"
echo "trap '/tmp/test &' INT" >> /tmp/output.txt
EOF
然後你就知道你得到的是什麼 shell,這裡sh
與遠端使用者的登入 shell 相反,無論它是什麼,你都可以輕鬆地傳遞程式碼來sh
逐字解釋,而不必擔心引用。
(順便說一句,你很難想使用-T
ever。你可能想使用-t
,但僅限於運行互動式應用程序,例如vi
或互動式shell;訊號9,通常無法擷取SIGKILL,最好使用訊號名稱而不是數位)。