Identify failed file transfers during SFTP


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Identify failed file transfers during SFTP
# 1  
Old 07-16-2014
Identify failed file transfers during SFTP

Hi All,

I have a pretty demanding requirement for an SFTP script I have been trying to put together.

I have nearly 100 files (all with the names staring with T_PROD) generated in my local server daily. I need to transfer each of these files to a remote server via SFTP (that's a client demand, can't use SSH or others). Since these files are pretty big (approx 10 MB each), we need to have a check on succesful transfer of each of the files. In case a file is not transferred properly, we will be trying to re-send the file, at max 3 times.

Since it's SFTP, we're not allowed to execute shell commands in the remote server. I tried creating the md5sum hash values, but stuck thereafter. How do we identify the exact files those failed the sanity check, and try re-sending them?

A barebone structure of the script is as below

Code:
#!/bin/bash
. ./params # parameter file

cd output

sftp ${FTPUSER}@${FTPSERVER} << EOF

cd $FTPFOLDER
put T_PROD*
!md5sum * > chklist.md5

bye
EOF

I'm running this script in Linux kernel 2.6.39

Do let me know if any further information is required.

- Best,

Avik
# 2  
Old 07-16-2014
Welcome Aviktheory11,

Your sftp session is not a normal shell session for running commands remotely so it is a little awkward. You command below
Code:
!md5sum * > chklist.md5

will run on the local server, so I'm not sure that is what you really want.

If you were using plain ftp then you would get a zero return code if the connection opened and you had to resort to switching on flags to get more verbose output that can be captured in a log file and read afterwards. sftp on the other hand is better with errors. Could you do something like the following instead?:-
Code:
#!/bin/bash
. ./params # parameter file

cd output

errcnt=0
for file in T_PROD*
do
   sftp ${FTPUSER}@${FTPSERVER} << EOF
      cd $FTPFOLDER
      put $file
        EOF
# Note not an indentation with spaces on previous line.  Used tab for clarity.
   if [ $? -ne 0 ]
   then
      printf "Something went wrong with file \"$file\"\n"
      ((errcnt=$errcnt+1))
   else
      printf "File \"$file\" send successfully.\n"
   fi
done

if [ $errcnt -ne 0 ]
then
   printf "ERROR in $errcnt transfers!\n"
done

# 3  
Old 07-16-2014
I'd say sftp is a secure and safe protocol. It either succeeds or it doesn't. Run it with raised logging level (-v option) . Check the exit code after completion.
# 4  
Old 07-16-2014
Why not just use scp?

Run it for each file and check the exit code, log errors as appropriate.
# 5  
Old 07-17-2014
Quote:
Originally Posted by achenle
Why not just use scp?

Run it for each file and check the exit code, log errors as appropriate.
Hi achenle, as I've said in my first thread, using SFTP is a business requirement, and we can't help abiding by that.



Quote:
Originally Posted by rbatte1
Welcome Aviktheory11,

Your sftp session is not a normal shell session for running commands remotely so it is a little awkward. You command below
Code:
!md5sum * > chklist.md5

will run on the local server, so I'm not sure that is what you really want.

If you were using plain ftp then you would get a zero return code if the connection opened and you had to resort to switching on flags to get more verbose output that can be captured in a log file and read afterwards. sftp on the other hand is better with errors. Could you do something like the following instead?:-
Code:
#!/bin/bash
. ./params # parameter file

cd output

errcnt=0
for file in T_PROD*
do
   sftp ${FTPUSER}@${FTPSERVER} << EOF
      cd $FTPFOLDER
      put $file
        EOF
# Note not an indentation with spaces on previous line.  Used tab for clarity.
   if [ $? -ne 0 ]
   then
      printf "Something went wrong with file \"$file\"\n"
      ((errcnt=$errcnt+1))
   else
      printf "File \"$file\" send successfully.\n"
   fi
done

if [ $errcnt -ne 0 ]
then
   printf "ERROR in $errcnt transfers!\n"
done



Thanks for the help rbatte1. However, will just checking the return code of sftp ensure the correct transfer of files ? I'm dealing with file transfer things just recently [that's why I've used the md5sum, din't realise it worked only locally], and I believe the return code is 0 even if a file was transferred partially. Please correct me if I'm wrong.

I'll try out your suggestion though. Thanks a lot for the help again !!!

- Best,

Avik
# 6  
Old 07-17-2014
You could also list the file catching the output and comparing it later. Use an embedded dir $file in your script after you send the file. You can compare the number of bytes with the original file.

Unfortunately in this case sftp is pretty robust. I will see if I can test it out by sending a large file and then killing the sftpd process on the remote side to see what happens when I get a chance (unless anyone else would care to have a go first)



Robin

Last edited by rbatte1; 07-17-2014 at 12:24 PM..
# 7  
Old 07-17-2014
If you want to run commands remotely, ssh is your best bet. sftp is not a shell. Neither is scp.

Quote:
Originally Posted by Aviktheory11
Hi achenle, as I've said in my first thread, using SFTP is a business requirement, and we can't help abiding by that.
Look -- sftp is scp. It's the same protocol. If a server supports sftp it supports scp and vice versa.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Sftp transfers file partially

Hi ALL, I have a shell script using except : #!/bin/bash HOST=abc.com USER=abc PASSWORD=123 SOURCE_FILE=file1.zip TARGET_DIR=/A/B/C /usr/bin/expect <<- EOF spawn /usr/bin/sftp $USER@$HOST expect "password:" send "$PASSWORD\r" expect "sftp>" send "cd patch1\n" ... (11 Replies)
Discussion started by: Asad
11 Replies

2. Solaris

Failed to identify flash rom on Sunfire V240 running Solaris 10

Hi Guys, I have performed OBP & ALOM upgrade on V240 system. One of my system, running Solaris 10, having issue to identify flash rom during ALOM 1.6.10 version upgrade (OBP upgraded to latest one). May I know what the reason of this error and how can I fix it so I can upgrade ALOM using... (0 Replies)
Discussion started by: myrpthidesis
0 Replies

3. Shell Programming and Scripting

Pls Help: SFTP Error Handling for transfers

I am working on a shell script where after making sftp connection to a remote server the file are being transferred. The problem is how to capture return code for the file which is missing at the remote location. I have tried to capture the return code which return value of "0" even the transfer of... (4 Replies)
Discussion started by: Khan28
4 Replies

4. Shell Programming and Scripting

How to find whether a particular command has failed inside an sftp script?

hi, how can i know whether a command inside an sftp script has failed or not? i have a sftp expect script #!/usr/bin/expect spawn /usr/bin/sftp abc@ftp.abc.com expect "sftp>" send "cd dir\r" expect "sftp>" send "mput abc.txt\r" expect "sftp>" send "mput def.xls\r" expect "sftp>"... (5 Replies)
Discussion started by: Little
5 Replies

5. Programming

Automatic SFTP transfers using OpenSSH on Windows and C#

I would like to create console application in c# to automate the process of downloading some files from a SFTP server to my local hard drive at a set time each week/day. SFTP Server installed OpenSSH for windows and client machine also. Any ideas how I could do such a task? or sample code. ... (0 Replies)
Discussion started by: sufiiyan
0 Replies

6. UNIX for Advanced & Expert Users

Identify failed disk in Linux RAID

Good Evening, 2 years ago, I set up an Ubuntu file-server for a friend, who is a photograph amateur. Basically, the server offers a software RAID-5 that can be accessed remotely from a MAC. Unfortunately, I didn't labeled the hard drives (i.e. which physical drive corresponds to the /dev/sdX... (2 Replies)
Discussion started by: Loic Domaigne
2 Replies

7. AIX

AIX ftp/sftp script monitor to failed logins

Hi All, Any idea on how to write a script on AIX 5.3 to monitor ftp or sftp login failed. Thanks and more power, Itik (2 Replies)
Discussion started by: itik
2 Replies

8. AIX

to identify failed pv

Hi friends,.... am sindhiya, i have joined as AIX level 1 support. help me to identify the failed pv in vg which has some 4 physical volumes? (2 Replies)
Discussion started by: sindhiya
2 Replies

9. AIX

SFTP Failed---Request for subsystem 'sftp' failed on channel 0

Hi, While I am trying SFTP my machine to another unix machine , it was working fine till 10 min back. But now i am getting the below error "Request for subsystem 'sftp' failed on channel 0" Could you please someone help me to solve or analyise the root cause... Cheers:b:, Mahiban (0 Replies)
Discussion started by: mahiban
0 Replies

10. UNIX for Advanced & Expert Users

SFTP error Assertion failed

I get this error when I try to FTP from an HP Alpha Server to a UNIX box. FATAL: BUILD13$:SSHFC_TRANSFER.C;1:1835 SshFCTransfer (function name unavailable) Assertion failed: tdata ->current_dest_file->attributes->flags & 0x00000004 the sftp /put fails just before it does the actual transfer.... (2 Replies)
Discussion started by: NoelSacay
2 Replies
Login or Register to Ask a Question