IF ELSE in EXPECT script

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. :slight_smile:

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.

You should consider increasing the verbosity of the sftp process using the -v or even the -vv option.

See for example:

sftp verbose output

Or the sftp man page......

Assuming Linux, for example, see also:

and

    -v      Verbose mode.  Causes ssh to print debugging messages about its progress.	This is helpful in debugging connection, authentication,
	     and configuration problems.  Multiple -v options increase the verbosity.  The maximum is 3.

As it says above, increasing logging verbosity is helpful when debugging connection, authentication and configuration problems.

1 Like

That is an excellent idea! I'll give that a try and go from there.

Thank you for your time and support with this question.

1 Like

Just to close the loop on this, it turns out you are a genius, Neo. Adding in a -vv switch, it turns out that successful connections are going to one specific IP, and the unsuccessful are going to a *second* IP. So I just need to open up port 22 on that second IP and life is good again.

Thank you very much for your help with this.