简体   繁体   中英

Bash Script For Checking Redirects With CURL

I have written the following script:

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

I have a file named foo.list which has a list of URIs I want to check using my for-loop; it looks like this:

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

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

/source3/uri/here /dest3/uri here

I'm trying to use awk to assign the source to $1 and the destination to $2, which works if I have 1 uri, but if I have multiple it appears to chomp parts of it.

The code looks a mess because I've got the random echos for debugging purposes.

At the end of script (which is commented out) I am trying to compare the destination of the source URL's redirect, with the suspected destination.

Where am I going wrong?

Not sure I got all the details right, but I'd try something like this:

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

Thanks lcd047!

Using what you put I've got the following:

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

And it works flawlessly!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM