I have an Expect script I've re-purposed from another process, and I would like to add in an IF THEN ELSE condition. I would greatly appreciate some help with the syntax.
The purpose of the script is to send a file to an FTP site. It works as it is right now (as in, the script actually does FTP the file over), it's just that occasionally the FTP site will reject my connection. It's totally intermittent, and I'm working with the company to get them to fix their damn FTP site ... but you know how it is. It MUST be something on my end.
Anyway, my goal is to get them to fix their FTP site or to migrate me to another one, but in the interim, and because it's good coding anyway, I'd like to add an IF ELSE THEN to my script. IF the FTP site returns a "Connection Refused", THEN try again, ELSE use my current code. Basically I'd like to keep trying until it works or fails 5 times kinda thing.
CURRENT SCRIPT THAT WORKS TO SEND THE FILE (unless there is a connection error):
HOST=$1
USER=$2
PASSWD=$3
FILE=$4
#lftp<<END_SCRIPT
#open sftp://$HOST
#user $USER $PASSWD
#put $FILE
#bye
#END_SCRIPT
expect -c "
spawn sftp ${USER}@${HOST}
expect \"password:\"
send \"${PASSWD}\r\"
expect \"sftp>\"
send \"cd /import/nonCC/data\r\"
expect \"sftp>\"
send \"put ${FILE}\r\"
expect \"sftp>\"
send \"exit\n\"
interact "
Here is the error I get if the connection is refused ...
spawn sftp xxx@pretendftp.site
Connecting to pretendftp.site...
ssh: connect to host pretendftp.site port 22: Connection refused
Couldn't read packet: Connection reset by peer
send: spawn id exp3 not open
while executing
"send "pretendpassword\r""
And out of interest, the message I get when the transfer is successful ...
spawn sftp xxx@pretendftp.site.
Connecting to pretendftp.site....
xxx@pretendftp.site.'s password:
sftp> cd /import/nonCC/data
sftp> put <PeopleSoft directory here>/division__20191025.txt
Uploading <PeopleSoft directory here>/division__20191025.txt to /import/nonCC/data/division__20191025.txt
<PeopleSoft directory here>/log_ou 0% 0 0.0KB/s --:-- ETA
<PeopleSoft directory here>/log_ou 100% 11KB 11.2KB/s 00:00
Anyway, I would greatly appreciate help with the script to capture the connection refused error in order to try again.