簡體   English   中英

使用CURL檢查重定向的Bash腳本

[英]Bash Script For Checking Redirects With CURL

我寫了以下腳本:

#!/bin/bash

host="www.myhost.com"

IFS=$" " ;

for x in $(cat foo.list) ; do
        srcURI=$(echo $x | awk '{print $1}') ;
        destURI=$(echo $x | awk '{print $2}') ;
        srcCURL=$(curl -s -H "Host: ${host}" -I "qualified.domain.local${srcURI}" |grep "Location:" | tr -d [[:cntrl:]]) ;
        destCURL=$(curl -s -H "Host: ${host}" -I "qualified.domain.local${srcURI}" |grep "301" | tr -d [[:cntrl:]]) ;

        echo " "
        echo " "        
        echo -e "srcURI = ${srcURI}"
        echo -e "destURI = ${destURI}"
        echo -e "srcCURL = ${srcCURL}"
        echo -e "destCURL = ${destCURL}"
        echo -e "Host:DestURI = Location: http://${host}${destURI}"
        echo -e "Host:srcCURL = ${srcCURL}"
        echo " "
        echo " "
#       if [[ ${srcCURL} == "Location: http://${host}${destURI}" ]] ; then
#               echo -e "Good";
#       else
#               echo -e "Bad";
#       fi
done

我有一個名為foo.list的文件,其中包含我想要使用for-loop檢查的URI列表; 它看起來像這樣:

/ source / uri / here / dest / uri / here

/ source2 / uri / here / dest2 / uri / here

/ source3 / uri / here / dest3 / uri here

我正在嘗試使用awk將源分配給$ 1,將目標分配給$ 2,如果我有1 uri,則可以正常工作,但如果我有多個,則可能會選擇部分內容。

代碼看起來很亂,因為我有隨機回聲用於調試目的。

在腳本結束時(已注釋掉)我試圖將源URL重定向的目標與可疑目標進行比較。

我哪里錯了?

我不確定我的所有細節都是正確的,但我會嘗試這樣的事情:

while read -r src dst; do
    redir="$( curl -q -s -S -o /dev/null -w '%{redirect_url}' qualified.domain.local"${src}" )"
    if [ $? -eq 0 -a x"$redir" = qualified.domain.local"${dst}" ]; then echo Good; else echo Bad; fi
done <foo.list

謝謝lcd047!

使用你放的東西,我有以下幾點:

#!/bin/bash

read -p "Enter the host: " host

while read -r src dst; do
        srcCURL=$(curl -s -H "Host: ${host}" -I "some.domain.local${src}" |grep "Location:" | tr -d [[:cntrl:]]) ;
        dstURL="Location: http://${host}${dst}"

        #echo ${srcCURL}
        #echo ${dstURL}

        if [ $? -eq 0 -a "${srcCURL}" == "${dstURL}" ]; then
                echo -e "[\033[0;32mOK\033[0m]: ${src} \033[0;1mredirects to\033[0m ${dst}";
        else
                echo -e "[\033[0;31mERROR\033[0m]: ${src} does not redirect to ${dst}";
        fi
done <foo.list

它完美無瑕!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM