SFTP file check through a remote host


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers SFTP file check through a remote host
# 1  
Old 05-06-2019
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.

Code:

#!/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

Last edited by MrShinyPants; 05-06-2019 at 11:22 AM..
# 2  
Old 05-06-2019
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.
# 3  
Old 05-12-2019
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.
# 4  
Old 05-12-2019
Is the file created?
Is the flag changed?
Is the while condition met?
Is the sftp operation executed?
# 5  
Old 05-12-2019
So, I ran the script in debug mode and it stops when doing sftp:


Code:
+ 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.

Last edited by MrShinyPants; 05-12-2019 at 04:36 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Check/get the exit status of a remote command executed on remote host through script

Geeks, Could you please help me out in my script and identify the missing piece. I need to check/get the exit status of a remote command executed on remote host through script and send out an email when process/processes is/are not running on any/all server(s). Here's the complete... (5 Replies)
Discussion started by: lovesaikrishna
5 Replies

2. Shell Programming and Scripting

Pause processes in remote host and resume execution in another remote host

Hi, Given addresses of 2 remote machines, using a shell script is it possible to get the state of running processes in "src" stop all the processes in "src" exit out of "src" ssh into "dest" resume the state of executing processes captured in step 1 in "dest" Assumption: "src" is... (3 Replies)
Discussion started by: Saeya Darsan
3 Replies

3. UNIX for Dummies Questions & Answers

Check port 2222 is open on a remote host

I am able to connect to a remote host using the legacy IP and port 2222. Today the remote has a new IP I am unable to connect. How to check if the remote host is blocking or if its my server is unable to connect. Err Msg : telnet: Unable to connect to remote host: Connection refused Err... (5 Replies)
Discussion started by: ITDev01
5 Replies

4. Shell Programming and Scripting

Reading file from remote host?

flamingo:~ jnojr$ ssh -t macbook sudo -v Password: Connection to macbook closed. flamingo:~ jnojr$ ssh -t macbook sudo cat /etc/cma.conf | grep CMABuildNumber <CMABuildNumber>2918</CMABuildNumber> Connection to macbook closed. flamingo:~ jnojr$ ssh -t macbook sudo -k Connection to macbook... (3 Replies)
Discussion started by: jnojr
3 Replies

5. Shell Programming and Scripting

check for file existence on remote machine using sftp

Hi all, I am a beginner to shell script.Can any one please help me on the below requirement I need to check whether the file (called 3Com_Files_Delivered.txt) exists on the remote mechaine or not? if so i need to copy all the files from there to my local mechaine.Especially i am... (7 Replies)
Discussion started by: narasimha123
7 Replies

6. Shell Programming and Scripting

command to check whether the remote host is up or not

Hi, My script needs to check whether the remote host is up or not. If it is up i need to start few servers in that host or else just a notification should be sent that the remote host is down? Could someone help me out in this? Regards Arun (4 Replies)
Discussion started by: arunkumarmc
4 Replies

7. UNIX for Advanced & Expert Users

Help! How to find the local host after few ssh hops to remote host???

I do a ssh to remote host(A1) from local host(L1). I then ssh to another remote(A2) from A1. When I do a who -m from A2, I see the "connected from" as "A1". => who -m userid pts/2 2010-03-27 08:47 (A1) I want to identify who is the local host who initiated the connection to... (3 Replies)
Discussion started by: gomes1333
3 Replies

8. Shell Programming and Scripting

running commands to remote host from centralized host

Gurus/Experts We have a centralized UNIX/Solaris server from where we can actually ssh to all other UNIX/Solaris servers...I need to write a script that reside on this centerlized server and do FileSystem monitoring (basically run df -h or -k) of other remote servers and then send an email to me... (6 Replies)
Discussion started by: anjum.suri
6 Replies

9. Shell Programming and Scripting

check ssh connection to remote host

I am using KSH and I need to check whether the remote host has been configured with ssh public key. Is there any way we can check inside a script? (6 Replies)
Discussion started by: praveenbvarrier
6 Replies

10. UNIX for Dummies Questions & Answers

Bash Script to check Remote Host Connection

Hi all, Can anyone tell/guide me how to check remote host is up/running using bash script? Thanks. Zulfiqar (5 Replies)
Discussion started by: zulfikarmd
5 Replies
Login or Register to Ask a Question