I think you missed a level of quotes escaping. At this high level of escaping, it is best to simply make a little script for every stage where otherwise quotes would be required.
Otherwise, you could try this modified version (but mind you, I don't encourage this coding style!)
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
}
On a system, the only thing that is really persistent is a file. That's pretty much what you should use. Here's an solution using an init.d script.
Let's consider the following (simple) script, /etc/init.d/myupdate
:
#! /bin/sh
### BEGIN INIT INFO
# Provides: myupdate
### END INIT INFO
PATH=/sbin:/bin:/usr/sbin:/usr/bin
case "$1" in
start)
/path/to/update/script
;;
stop|restart|reload)
;;
esac
If you activate it with update-rc.d myupdate defaults
, the start
action will be executed upon boot. Now, when your update script calls for a reboot:
touch /var/run/rebooting-for-updates
sudo reboot
With this solution, you can divide your update script into two parts :
before_reboot(){
# Do stuff
}
after_reboot(){
# Do stuff
}
if [ -f /var/run/rebooting-for-updates ]; then
after_reboot
rm /var/run/rebooting-for-updates
update-rc.d myupdate remove
else
before_reboot
touch /var/run/rebooting-for-updates
update-rc.d myupdate defaults
sudo reboot
fi
It'll execute the before_reboot
code section, create a file in /var/run
, and reboot. Upon boot, the script will be called again, but since the file exists, after_reboot
will be called instead of before_reboot
.
Note that update-rc.d
requires root privileges.
Without using a file (from Stephen Ostermiller's comment):
If you are familiar with the getopts
utility, you may want to use options instead of files. In the init script, call the script with:
/path/to/update/script -r
And in your script, check for options instead of files. Call your script once without the option, and init.d will call it again on boot, this time with -r
.
# Set AFTER_REBOOT according to options (-r).
if [ "x$AFTER_REBOOT" = "xyes" ]; then
# After reboot
else
# Before reboot
fi
You'll find more information about option handling here (for short options only). I also edited my script with calls to update-rc.d
to keep this a one-time job (from another comment).
Best Answer
Of course it is doable:
and that's it...
But there is one problem: you will be asked for password three times. To avoid that you could generate ssh keys and authorize users by these keys.
To generate ssh keys run
ssh-keygen -t rsa
, answer questions and copy public key to remote host (machine B) to~/.ssh/authorized_keys
file. Private key should be saved in~/.ssh/id_rsa
on local machine (A).