SFTP file check through a remote host

Hi all, posting my first time, hope not breaking posting rules with it, if yes, let me know.

I'm trying to build a script to check a file in an sftp server through a remote server.

The intention is to check the file in a sftp host, and if the file is found or not, it should send an email.
At certain point if the file is not available, we must take actions and call the provider to provide the file ASAP.

host where check is located -> remote host check -> sftp host

The following script was modified from another script I found here and tried to adapt for what I need but it is not working.
when I run it, it stays running and no email is sent to me.


#!/bin/bash
icdata=data
icinp=input
ictmp=/tmp
iclog=log
key="keys/key"
file="FILE_${date}_NAME"
date=$(date +%Y%m%d)
logname="FILE_${date}_NAME.log"
file="FILE_${date}_NAME"
logdate=$(date +%Y%m%d%H%M%S);
ssh_args=( -o "StrictHostKeyChecking=no" -qni "$key" user@host )
flag="N"


#REMOVING LOG FILES OLDER THAN 30 DAYS

find $iclog -type f -name "FILE_*_NAME.log" -follow -mtime +30 -exec rm {} \; -print


#----Log Errors in Log File---------#

inf_log()
{
        echo "INFO: $@"  >> $iclog/$logname
}

err()
{
        echo "No $@"  >> $iclog/$logname
        exit 1
}


#SFTP CHECK

echo Log BEGIN For $logdate>$iclog/$logname


while [ "$flag" != "Y" ]
do
ssh -T "${ssh_args[@]}"
sftp -oPort=6022 SFTP_USER@10.11.22.33 << EOF
ls -la $file
bye
EOF

if [[ -f $file ]]; then
inf_log "File is available on SFTP for tidal job to retrieve........."
ssh -T "${ssh_args[@]}"
sftp -b - -oPort=6022 SFTP_USER@10.11.22.33 <<EOF >$iclog/$logname
ls -la $file
bye
EOF
flag='Y'
else
sleep 300
fi
done

#-------Mail SFTP status----------------------------#

grep -q '$file' $comlog/$lognm

if [ $? -eq 0 ]; then
        inf_log "File is available!"
        echo "File available - No Actions needed\n"|mailx -s "FILE_${date}_NAME File check Success " me@duh.com
else
        echo "File not yet available - Please call PROVIDER\n"|mailx -s "FILE_${date}_NAME File check Failed " me@duh.com
        err " File is not Found!!! - Please call PROVIDER ASAP"
        exit 1
fi



Any suggestion or correction would be appreciated.

Thank you

Looks like $file doesn't exist and never is created, so if 's then branch is never executed, flag isn't set to "Y", and the while loop's exit condition is not met.

Proper indentation helps you and others read and understand your code. btw.

Hi again,

So i was testing the script with minor corrections but it still fails.
I execute it and stays running doing nothing.
I check the log file and it shows that log file is beginning.

it seems that it stops at the sftp check section.

can anyone assist me with this?

Thank you in advance.

Is the file created?
Is the flag changed?
Is the while condition met?
Is the sftp operation executed?

So, I ran the script in debug mode and it stops when doing sftp:

+ iclog=log
+ key=keys/key
+ file=FILE__NAME
+ date=20190510
+ logname=FILE_20190510_NAME.log
+ file=FILE_20190510_NAME
++ date +%Y%m%d%H%M%S
+ logdate=20190512141350
+ ssh_args=(-o "StrictHostKeyChecking=no" -qni "$key" USER@HOST)
+ flag=N
+ find log -type f -name 'FILE*_NAME.log' -follow -mtime +30 -exec rm '{}' ';' -print
+ echo Log BEGIN For 20190512141350
+ '[' N '!=' Y ']'
+ ssh -T -o StrictHostKeyChecking=no -qni keys/key USER@HOST
+ sftp -oPort=6022 USER1@10.11.22.33
ssh: connect to host 10.11.22.33 port 6022: Connection timed out
Couldn't read packet: Connection reset by peer
+ [[ -f FILE_20190510_NAME ]]
+ sleep 300

It seems to be the ssh connection to the host that will access the sftp that is not working.

@RudiC thank you for your questions, i was being short in my temper.