
我正在嘗試使用腳本自動部署一些 .deb 套件。我想在sudo dpkg -i $myDeb.deb
可以透過 ssh 存取的遠端電腦清單中執行。
我嘗試在 bash 腳本中使用“expect”自動執行命令,但顯然我做錯了一些事情,因為我收到了許多不同的錯誤(基本上取決於我放置引號的位置)
這是我擁有的函數(將使用以下內容呼叫:_remoteInstallation "myPackage115.deb" "192.168.1.55"
。我知道在遠端電腦中,.deb 將位於 $HOME/Documents/ 中:
function _remoteInstallation(){
local retval=1
local debToInstall=$(basename "$1")
local remoteMachine="$2"
spawned=$(expect -d -c "
set timeout 1800
spawn "/usr/bin/ssh -t borrajax@$remoteMachine /usr/bin/sudo /usr/bin/dpkg -i /home/borrajax/Documents/$debToInstall"'
expect {
\"Are you sure you want to continue connecting\" { send \"yes\r\"; exp_continue }
\"password\" { send \"myPassword\r\"; exp_continue }
\"[sudo] password\" { send \"myPassword\r\"; exp_continue }
default { exit 1 }
}
" )
retval=$?
return $retval
}
透過像這樣的生成區域中的引號,我得到
expect: invalid option -- 't'
如果我把它改成:
spawn /usr/bin/ssh -t borrajax@$remoteMachine '/usr/bin/sudo /usr/bin/dpkg -i /home/borrajax/Documents/$debToInstall'
看起來正在嘗試在本地運行 sudo dpkg 命令(首先 ssh 到“$remoteMachine”,然後在本地運行 sudo dpkg,就像兩個單獨的命令)
有了這個:
spawn '/usr/bin/ssh -t borrajax@$remoteMachine \'/usr/bin/sudo /usr/bin/dpkg -i /home/borrajax/Documents/$debToInstall\''
我明白了couldn't execute "'/usr/bin/ssh": no such file or directory
(這不是真的)
....此時,我已經沒有想法了......:-)
任何提示將不勝感激。謝謝。
答案1
我用於解決類似問題的方法是將所有遠端腳本放入一個文件中,例如/usr/local/bin/debinstall.sh
.對於您的情況,我的建議是:有一個特殊的目錄,您可以將包放入其中 - 讓我們稱其/tmp/remoteinstall
為一個示例。此外,將您連接到的用戶放入該/etc/suduers
文件中,並允許其執行sudo dpkg -i *
而不提示輸入密碼。debinstall.sh
然後看起來像這樣:
#!/bin/bash
cd /tmp/remoteinstall
sudo dpkg -i *.deb && rm -f *
使該腳本歸 和 所有chmod 744 /usr/local/bin/debinstall.sh
。
在本地,您的工作只是上傳 .deb 檔案並調用腳本:
cd /path/to/files
scp * user@remotemachine:/tmp/remoteinstall
ssh user@remotemachine /usr/local/bin/debinstall.sh
debinstall.sh
然後會安裝你的包包,然後清空目錄僅當安裝成功完成時。
如果 中缺少某些內容$PATH
,請記住 和.bashrc
都不.profile
是以這種方式執行的 - 因此您可能希望在遠端腳本的開頭獲取它們,或者在那裡定義適當的 PATH。