STAKD
STAKD

Reputation: 15

SFTP - Listing files in a directory and writing the output to a .txt file

[EDIT] I am writing a script that should pull files from a remote server using SFTP and pull them onto a unix directory using KornShell. I am having various issues with this, the main one being that my remote directory is not recognised a file path. The output is basically that no such file or directory exists.

I am able to manually remote onto the server through the command line in Unix but my script won't do the same.

Any advice on this would be massively appreciated.

I have tried the following:

# Check files on remote server
#
  echo "cd ${SOURCEDIR}" > /tmp/scotland_ftp_insftp
  echo -ls "*" >> /export/home/opsup/SFTPtest/scotland_filelist.txt
  echo bye >> /tmp/scotland_ftp_insftp
  sftp -b /tmp/scotland_ftp_insftp ${SFTPUSER}@${SFTPSERVER}: 2>${SFTPCHKFILE}
          if [[ $? -ne 0 ]]
          then
             echo `date +%d\ %b\ %H:%M` `basename $0` ${USER} "sftp failed" >> ${ERRLOG}
             return 9
          fi
#

My .chk file outputs:

realpath /Scotland/upload: No such file.

I have also tried:

# Check files on remote server
#
sftp -b ${SFTPUSER}@${SFTPSERVER}:/D:/Scotland/upload/test; ls >>                  /export/home/opsup/SFTPtest/scotland_filelist.txt
   if [[ $? -ne 0 ]]
      then
      echo `date +%d\ %b\ %H:%M` `basename $0` ${USER} "sftp failed" >> ${ERRLOG}
      return 9
   fi

Which outputs:

No such file or directory (user@server:/D:/Scotland/upload/test).

File paths are in a cfg file and are in the format of temp/dir/to/file

I have worked out the issue, it was due to the script looking for the batch file in the wrong place. Here is the corrected script.

  # Check files on BSA-FTP-P-INTN3
  #
     echo "cd ${SOURCEDIR}" > scotland_in_sftp
     echo -ls >> scotland_in_sftp
     echo bye >> scotland_in_sftp
  #
  # SFTP to get files from BSA-FTP-P-INT01
  #
    sftp -b scotland_in_sftp ${SFTPUSER}@${SFTPSERVER}: > 
    scotland_filelist.txt
    if [[ $? -ne 0 ]]
       then
       echo `date +%d\ %b\ %H:%M` `basename $0` ${USER} "sftp failed" >> 
       ${ERRLOG}
       return 9
    fi

How do I stop the script writing everything to the scotland_filelist.txt?

I just need the files but it is writing everything to the file.

Upvotes: 0

Views: 419

Answers (2)

user27396864
user27396864

Reputation: 1

if [[ $? -ne 0 ]] then echo date +%d\ %b\ %H:%M basename $0 ${USER} "sftp failed" >> ${ERRLOG} return 9 fi

Upvotes: 0

STAKD
STAKD

Reputation: 15

Answer from glennjackman in the comments -

"For additional quietness: "Echo of the command may be suppressed by prefixing the command with a ‘@’ character." -- so @cd ..., -@ls, @bye"

Upvotes: 0

Related Questions