Sometimes when connecting to a remote SSH server I get Connection Closed By *IP*; Couldn't read packet: Connection reset by peer. But after trying one or two more times it connects properly.
This presents a problem with a few bash scripts I use to automatically upload my archived backups to the SSH server, like so;
export SSHPASS=$sshpassword
sshpass -e sftp -oBatchMode=no -b - root@$sshaddress << !
cd $remotefolder
put $backupfolder/Qt_$date.sql.gz
bye
!
How can I have this part loop until it actually properly connects?
UPDATE: (Solution)
RETVAL=1
while [ $RETVAL -ne 0 ]
do
export SSHPASS=$sshpassword
sshpass -e sftp -oBatchMode=no -b - root@$sshaddress << !
cd $remotefolder
put $backupfolder/Qt_$date.tgz
bye
!
RETVAL=$?
[ $RETVAL -eq 0 ] && echo Success
[ $RETVAL -ne 0 ] && echo Failure
done
I am not a shell scripting expert, but I would check the return value of
sshpasswhen it exits.From
man ssh:From
man sshpath:So try to run the command, and check its return value. If the return value was not
0(forSUCCESS) then try again. Repeat using awhileloop until you succeed.Sidenote: why are you using
sshpassinstead of public-key (passwordless) authentication? It is more secure (you don’t have to write down your password) and makes logging in via regularsshas easy asssh username@host.There’s even an easy tool to set it up:
ssh-copy-id.