我正在尝试使用脚本自动部署某些.deb软件包。我想sudo dpkg -i $myDeb.deb
在可以使用ssh访问的远程计算机列表中执行。
我试图在bash脚本中使用“期望”使命令自动化,但是显然我做错了,因为我遇到了许多不同的错误(基本上,这取决于我在引号中的位置)
这就是我所拥有的功能(将以类似以下名称的方式调用:_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
(这是不正确的)
...在这一点上,我的想法已经用光了... :-)
任何提示将不胜感激。谢谢你。
我认为您错过了某种程度的引号转义。在如此高的转义率下,最好在每个阶段都需要编写一个小脚本,否则将需要使用引号。
否则,您可以尝试使用此修改版本(但请注意,我不鼓励使用这种编码样式!)
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
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句