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