5

I am sending a file using SFTP and public key for non-root user, looks like the file is sent, but I cant find it on the target folder, maybe due to permission.

sftp> ls
sftp> put /tmp/testx
Uploading /tmp/testx to /folder1/target_folder
/tmp/testx                                 100%    5     0.0KB/s   00:01
sftp> get testx
Couldn't stat remote file: No such file or directory
File "/folder1/target_folder/testx" not found.
sftp> ls
sftp>

here is the -vvv :

debug3: SSH_FXP_REALPATH . -> /folder1
debug3: Looking up /tmp/file_to_send
debug3: Sent message sender_host 4 T:17 I:2
debug3: Wrote 80 bytes for a total of 2653
debug3: Received stat reply T:105 I:2
debug3: Sent message SSH2_FXP_OPEN I:3 P:/folder1/target_directory/file_to_send
debug3: Wrote 112 bytes for a total of 2765
debug3: Sent message SSH2_FXP_WRITE I:4 O:0 S:6206
debug3: Wrote 6288 bytes for a total of 9053
debug3: SSH2_FXP_STATUS 0

target directory

 drw-rw-rw-    1  0   0       target_directory

How can I make sure the file is delivered, without server access ?

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Nabil Sham
  • 2,305
  • 4
  • 26
  • 38
  • Is there a particular reason you're using the command-line tool rather than a client library that implements the protocol directly and provides documented guarantees about its handling of server-provided error messages? – Charles Duffy Jan 29 '15 at 21:11

3 Answers3

10

All you can do is to check that there are no errors, when uploading the file. That's all information the SFTP server gives you.

With command-line OpenSSH sftp client, you can check its exit code (you need to use the -b switch).

echo "put file.txt" | sftp -b - user@host
if [ $? -eq 0 ]
then
    echo "File uploaded"
else
    echo "File NOT uploaded"
fi

See also How to perform checksums during a SFTP file transfer for data integrity?


It's perfectly possible that the SFTP server does not allow you to download a file that you have just uploaded.

There are two common reasons for such behavior:

  • Public "upload" directory. This is to prevent you from downloading other user's files.
  • There's some process that immediately picks uploaded file for some processing.
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
2

I know this is an old thread... Here's the best I've been able to come up with for password auth uploading into a blind folder using the openssh sftp client. (It was a lot more exact with ftp...)

SSHPASS=${PASSWORD} sshpass -e sftp -vvv -oStrictHostKeyChecking=no -oBatchMode=no -b - ${LOGON}@${FTPSITE} >${LOB}ftp.log 2>&1 <<EOF
pwd
cd upload
put ${XMTFILE}
quit
EOF
# sftp does not use FTP codes - it only gives the final status of the requested operation
Status=$?
# Not perfect, but sending a single file using -vvv should produce 2 lines containing "debug3: SSH2_FXP_STATUS 0"
Tmp=`grep "debug3: SSH2_FXP_STATUS 0" ${LOB}ftp.log | wc -l`
[ ${Tmp} -ne 2 ] && Status=1 # Override sftp exit value based on log info
1

For a completely arbitrary server implementation, this is not possible. It's often desirable to make incoming directories write-only to untrusted users, to prevent abuse of publicly (or otherwise widely) accessible file servers (such as using them to host unrelated content; once upon a time, FTP servers with stolen credentials were often used by pirates -- preventing uploaded content from being downloaded without administrative review prevents this). Thus: If the server administrator chooses not to let you see uploaded files, then, well, you can't see uploaded files.

However -- the SFTP protocol provides a contract: If you tell the server to close a handle after writing to it, and the server says that all involved operations succeeded without error, then if the file wasn't successfully received (whatever the server then decides to do with it, which may or may not include making it accessible for downloads), this is indicative of a server-side bug and is not the fault of your SFTP client.

When I was last implementing a custom SFTP server -- for customer support purposes, implemented using Paramiko -- I sent email notifications after successful uploads; obviously, though, that's an implementation-specific behavior).

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • In the user interactive mode, I see progress with 100% indicating the file was sent, I know this is not available in batch mode. What line in the -vvv log is equivalent to this "100%" ? in other words, what should I look for in log to say the file was sent ? – Nabil Sham Jan 29 '15 at 20:58
  • The "100%" doesn't really tell you much either -- it tells you the number of bytes sent matches the number of bytes expected, but that doesn't mean they were all written. It sounds like this is less a question about the underlying SFTP protocol, and more about the codebase for the OpenSSH command-line tool implementing that protocol; I know the former far better than the latter. – Charles Duffy Jan 29 '15 at 21:08
  • It's noteworthy that paramiko, by default, does a `stat()` on files post-upload to ensure that their remote and local sizes match. (This can be disabled for servers that rename, move, or otherwise don't allow access to just-uploaded files by typical clients). _If_ that works, it's a more sure thing that trusting the absence of errors to be meaningful, but obviously it doesn't work on all servers, including the one you have here. – Charles Duffy Jan 29 '15 at 21:09