2017-04-13 88 views
0

我需要檢查某個文件是否可以在FTP服務器上下載。我一直在使用wget的--spider選項來實現這一點,但是,這隻適用於404錯誤,FTP不使用。檢查FTP服務器上是否有文件

目前代碼(不工作):

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 

如何檢查( 「蜘蛛」),看是否有文件可在FTP服務器上?我知道FTP的「404」版本是550(沒有這樣的文件或目錄)。

回答

0

這是我能得到工作:

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 
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 
相關問題