【问题标题】:Check If File Is Available on FTP Server检查 FTP 服务器上的文件是否可用
【发布时间】:2017-04-13 09:55:01
【问题描述】:

我需要检查某个文件是否可以在 FTP 服务器上下载。我一直在使用wget 的 --spider 选项通过 HTTP 实现此目的,但这仅适用于 FTP 不使用的 404 错误。

当前代码(不工作):

file1="12345.tar"
abc1="http://some.website/data/${file1}"

if wget --no-cache --spider --user=username --password='password' ${abc1} >/dev/null 2>&1; then
echo "File ${abc1} exists. Let's get it!"
bash run.sh
else
echo "File ${abc1} doesn't exist. Exiting script..."
exit 0
fi

如何检查(“spider”)以查看 FTP 服务器上的文件是否可用?我知道 FTP 的“404”版本是 550(没有这样的文件或目录)。

【问题讨论】:

    标签: linux bash unix ftp wget


    【解决方案1】:

    对于 FTP

    响应代码:为新用户准备好服务 (220)

    响应码:150 这里是目录列表/打开二进制数据连接

    对于 HTTP

    将 220 或 150 替换为 HTTP/1.1 200 OK

    #!/bin/bash
    
    url="ftp://path/to/some/file.something"
    
    function validate_url(){
    
        if [[ `wget -S --spider $url 2>&1 | grep '150'` ]]; then exit_status=$?; fi
        if [[ $exit_status == 0 ]]; then
                echo "FTP location exists"
        elif [[ $exist_status == 1 ]]; then
                echo "FTP location not available"
        fi
    }
    
    validate_url
    

    【讨论】:

      【解决方案2】:

      这是我能够得到的工作:

      file1="12345.tar"
      abc1="http://some.website/data/${file1}"
      
      check=`wget --no-cache --spider --user=username --password='password' ${abc1} 2>&1 | awk '{print $1}' | head -n 24 | tail -1`
      
      if [ ${check} -eq "No" ]; then
      echo "File ${abc1} doesn't exist. Exiting script..."
      exit 0
      else
      echo "File exists!"
      fi
      

      【讨论】:

        猜你喜欢
        • 2012-05-15
        • 2011-11-16
        • 1970-01-01
        • 2018-09-24
        • 1970-01-01
        • 2013-01-16
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多