I am using a script to call SFTP service to fetch some logs from the remote server. This works fine in normal scenario but, i have noticed a very long delay of around 30 minutes or more in some of the cases when the server expects the password. Following is the code i am using:#!/bin/bashdirdate=`/bin/date +%Y%m%d -d 1 day ago`INPUT_DIR=/root/SDP_BHC/inputCREDENTIALS_FILE=/root/FTP_TEST/Credentials.csv# Loop to read credentials and other details from an external file.while IFS=','; read node_id node_name ip1 ip2 ip3 user1 pass1 user2 pass2 user3 pass3 installed location circle sdpno hwtypedo        # Generate Input Directory for IP-Node        mkdir -p $INPUT_DIR/$node_id/$node_name/IP/$dirdate        echo -e \n[INFO] Node IP: $ip1        echo -e =============================\n        SOURCE_FILE=/var/opt/fds/statistics/*PSC-TrafficHandler_8*1_A_*_System*$dirdate*stat        TARGET_DIR=$INPUT_DIR/$node_id/$node_name/IP/$dirdateset prompt (%|#|\\$|%\]) $spawn /usr/bin/sftp $user1@$ip1expect { $prompt }#expect Are you sure you want to continue connecting (yes/no)?#bin promptsend yes\r#expect Password:expect { $prompt }send $pass1\rexpect sftp>send mget $SOURCE_FILE $TARGET_DIR\rset timeout 2000expect sftp>send bye\rEOD...done < $CREDENTIALS_FILEThe script hangs in some cases like when the password updated in the CREDENTIALS_FILE is not matching (because of password change in the source server from where file is being fetched). In such case, the password entry gets two responses (yes and $pass1) and then hangs for third password prompt since first two are wrong. Here, it hangs for a very long time (in some cases it doesn't which is another confusion i have why it doesn't happen in those cases).Anyways, whatever the case maybe i want to make the SFTP session timeout quickly if any of such scenario happens. How do i make the session to get a timeout quickly if the passwords do not match at all? I am already using a timeout. For a normal session, my script takes a max of 30-35 seconds to connect to the servers and FTP the files to the local machine.Could anyone please provide some pointers regarding how to fix this issue?
Huge delay in script response when using SFTP with expect
shell script;ftp;sftp;expect;timeout
You can add to the sftp command the option-o NumberOfPasswordPrompts=1to stop it asking twice for passwords. You might usefully also try-o ConnectTimeout=20-o ConnectionAttempts=1