【问题标题】:SFTP - capture file list not working as expectedSFTP - 捕获文件列表未按预期工作
【发布时间】:2014-02-20 15:44:46
【问题描述】:

我们想在 BASH 中捕获一个文件列表(无论是 1 个还是多个),评估文件是否已更改..

  1. 如果文件已更改/更新
  2. 删除之前下载的文件
  3. 下载新文件
  4. 通知一组人(做)

我看过这个.. save the result of ls command in the remote sftp server on local machine

但是,对于我们想要做的事情,似乎无法正常工作..

环境

LINUX:RedHat (2.6.xx-xxx.x.x.xxx)

SFTP:

  • SFTP(1) BSD 通用命令手册
  • SFTP 协议版本 3

使用上面的链接:

sftp account@0.0.0.0
expect "password:"
send "somepass\n";
set file [open /home/checks/sftp_files w]
send  "ls /home/sftp_server_location/*.zip\n"
log_file
interact

我一直在读到人们在使用 SFTP 创建文件列表时遇到了问题。FTP - 来看看,没有这个问题。

我们已经在 Windows 下使用 WinSCP 执行此操作,并且工作正常。

但是,它所在的系统和数据中心正在关闭,我们需要在 LINUX 机器上获取它..

在 Windows 上,我们正在做一个高级别的:

Windows Scheduled Task that runs daily
To create a list of files beforehand: dir "d:\Downloads\*.gz" /B/S  > D:\scripts\OLDLIST.TXT
Pause for 1 second: PING 127.0.0.1 -w 1000 > NUL
Run a script for WinSCP: cscript /nologo D:\scripts\somscript.js
Pause for 1 second: PING 127.0.0.1 -w 1000 > NUL
Create a list of files after: dir "d:\GEO_IP_Downloads\*.gz" /B/S  > D:\scripts\NEWLIST.TXT
Pause for 1 second: PING 127.0.0.1 -w 1000 > NUL
Run a FC (File Compare):
%windir%\system32\FC /B D:\scripts\NEWLIST.TXT D:\scripts\OLDLIST.TXT | %windir%\system32\FIND /i "FC: D:\SCRIPTS\NEWLIST.TXT longer than D:\SCRIPTS\OLDLIST.TXT"
If there is a new file found, then e-mail:
IF NOT ERRORLEVEL 1 d:\SCRIPTS\BLAT.exe -body "New file(s) have been found at Vendor" -f somebody@fisglobal.com -to sombodyelse@fisglobal.com -server 1.2.3.4 -subject "New file(s) have been found at company" -q

WinSCP 来自 – https://winscp.net

我们使用的具体脚本来自–
https://winscp.net/eng/docs/script_download_most_recent_file#scripting

【问题讨论】:

    标签: bash sftp redhat


    【解决方案1】:

    这是您要查找的内容的粗略版本。它将要求您保留上次同步的列表。 (如何对此进行身份验证是一个不同的主题,但我建议不要将密码保存在脚本中,而是使用 ssh 密钥)。

    #!/bin/bash
    
    lastlist=/tmp/sftp_list.old
    newlist=/tmp/sftp_list.new
    localpath=/home/foo/downloads
    remotepattern='/home/bar/*.zip'
    remotehost='account@0.0.0.0'
    notifysubject="New remote files"
    notifyaddress="foo@example.com bar@example.com"
    
    cd "$localpath"
    if echo "ls -1 $remotepattern" | sftp "$remotehost" >"$newlist"; then
        filelist=$(comm -13 "$lastlist" "$newlist")
        [ $(echo "$filelist" | wc -l) -gt 0 ] && {
            {
                echo "New remote files:"
                echo "$filelist"
            } | mail -s "$notifysubject" "$notifyaddress"
    
            # download new files
            echo "$filelist" | sed 's/^/get /' | sftp "$remotehost"
    
            rm -f "$lastlist"
            mv "$newlist" "$lastlist"
        }
    else
        echo "Error connecting to the remote host"
    fi
    

    这只会下载新文件,但如果任何文件名包含\n(这种情况非常罕见),它会以微妙的方式失败。一个简单的解决方法是重新下载所有匹配的文件,而不是按名称选择:echo "get $remotepattern" | sftp "$remotehost"

    【讨论】:

    • 这里有点不对劲。./1.sh: wc line 12: wc -l <<<"" : arithmetic syntax error我尝试了几种不同的方法,但都没有完全正确。
    • @Leptonator:您使用的是较旧的 bash 版本。我编辑了代码来解决这个问题。另外,请确保您有 wccomm 可用。
    • 这很奇怪.. wc 现在正在工作.. 结果如下:# *********** # Restricted Access. Explicit authorization required. # *********** sftp> get You must specify at least one path after a get command.
    • 脚本答案很有帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-27
    • 1970-01-01
    • 2020-03-07
    • 2017-09-08
    • 1970-01-01
    相关资源
    最近更新 更多