SFTP - 列出目录中的文件并将输出写入 .txt 文件

问题描述 投票:0回答:1

[编辑] 我正在编写一个脚本,该脚本应该使用 SFTP 从远程服务器拉取文件,并使用 KornShell 将它们拉到 unix 目录中。我遇到了各种问题,主要的一个是我的远程目录无法识别为文件路径。输出基本上是不存在这样的文件或目录。

我可以通过 Unix 中的命令行手动远程连接到服务器,但我的脚本不会执行相同的操作。

对此的任何建议将不胜感激。

我尝试过以下方法:

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

我的 .chk 文件输出:

realpath /Scotland/upload: No such file.

我也尝试过:

# 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

哪个输出:

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

文件路径位于cfg文件中,格式为temp/dir/to/file

我已经解决了这个问题,这是由于脚本在错误的位置查找批处理文件。这是更正后的脚本。

  # 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

如何停止脚本将所有内容写入 scotland_filelist.txt?

我只需要文件,但它正在将所有内容写入文件。

unix sftp ksh
1个回答
0
投票

来自glennjackman在评论中的回答-

“为了更加安静:“可以通过在命令前添加‘@’字符来抑制命令的回显。”——所以@cd ...、-@ls、@bye”

© www.soinside.com 2019 - 2024. All rights reserved.