Link to home
Start Free TrialLog in
Avatar of bje
bjeFlag for United States of America

asked on

SFTP Output to a file

Trying to capture this output to a file.  (manual SFTP)

sftp> cd /inbox
sftp> put testfile.txt
Uploading testfile.txt to /inbox/testfile.txt
testfile.txt                                                                                                       100%    5     0.1KB/s   00:00
sftp> bye


Using this command in my script, sftp -b /tmp/sftp_command_file $user@$node >> /tmp/sftp_error_file.   However, it does not write anything to the file.  

From script,
FILELIST=`ls -1 2> /dev/null`  
     for f in $FILELIST
      do
         echo "put $f inbox/$f" >>/tmp/sftp_command_file      
         echo "chmod 777 inbox/$f" >> /tmp/sftp_command_file
         echo "quit" >> /tmp/sftp_command_file
    done

sftp -b /tmp/sftp_command_file $user@$node >> /tmp/sftp_error_file

Thanks in advance for your help.
BJE
Avatar of noci
noci

why not use:

scp testfile.txt   remoteuser@remotesystem:/inbox/testfile.txt 

Open in new window

The chmod 777 can be done using ssh
ssh remoteuser@remotesystem chmod 777 /inbox/testfile.txt

Open in new window

or possibly even better with an acl that does that on all file put in the /inbox directory on the remote system....

Scp output can be written to a tekst file if you want (append >outputfile.txt)
you can verify the exit status using $? or  if scp ..... ; then

scp testfile.txt   remoteuser@remotesystem:/inbox/testfile.txt
if $?
then
  echo OK
else
  echo Oops
fi 

Open in new window


is equivalent to:
if scp testfile.txt   remoteuser@remotesystem:/inbox/testfile.txt
then
  echo OK
else 
  echo Oops...
fi 

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of David Favor
David Favor
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bje

ASKER

I tried sftp ... 2>&1 | tee -a -i /tmp/sftp.$$.log and it provides this information.

sftp> lcd /app/transfer/outbound
sftp> put testfile.txt /inbox/testfile.txt
sftp> chmod 777 /inbox/testfile.txt
sftp> quit

However, looking for this information.  The Uploading line is missing.  This displays when doing a manual session, but not through the script.

sftp> lcd /app/transfer/outbound
sftp> put testfile.txt /inbox/testfile.txt
Uploading testfile.txt to /inbox/testfile.txt
sftp> chmod 777 /inbox/testfile.txt
Changing mode on /inbox/testfile.txt
sftp> quit

Also, we are upgrading our OS.  Would it be something with the upgraded OS?

Thanks,
BJE
The upload line is missing, because you've typed it interactively (is my guess).

This means you've used STDIN, which there's no way to easily capture.

To capture you're STDIN, you'll change your script to this...

sftp -b file.sftp ... 2>&1 | tee -a -i /tmp/sftp.$$.log

Open in new window


Where file.sftp is a batch file container the above commands.

To retain an interactive script + capture STDIN/STDOUT/STDERR will require using a tool like Expect or PERLs io::capture (or similar) module.

If you go the batch file route, you'll be done in a few minutes.

Trying to capture STDIN/STDOUT/STDERR will take a good bit of time to write.
In stead of usig SFTP why not try its command line brethren (SFTP internaly uses the SSH filetransfer...) SCP internaly uses the SSH filetransfer....
so they effectively do the same...

try:
scp /app/transfer/outbound/testfile.txt remoteuser@remotesystem:/inbound/

if you need more info:
scp -v /app/transfer/outbound/testfile.txt remoteuser@remotesystem:/inbound/

those command don;t require inpu and all output can be redirected if needed.
I'd second noci's suggestion.

I'd use rsync, as rsync is smarter than scp + will only do transfer's if a transfer is required.

You can use scp + your transfer will run every time + transfer all bytes of file every time, even if only one byte changed.

So for your application, rsync is best, scp is next best, sftp is most difficult... from the standpoint of capturing all your data.

The primary difference is with SFPT you're interactively providing the file name or glob.

With rsync + scp, file names are included in the actual command, so there's no STDIN to capture.