Bash Script To Test Redirection With CURL

I wrote 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 called foo.list that has a list of URIs that 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 source $ 1 and destination to $ 2, which works if I have 1 uri, but if I have multiple, it seems to split parts of it.

The code looks messy because I have random echoes for debugging purposes.

At the end of the script (which is commented out), I'm trying to compare the redirect destination of the original url to the intended recipient.

Where am I going wrong?

+3


source to share


2 answers


Not sure if I understood everything correctly, but I would 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

      

+3


source


Thanks lcd047!

Using what you put I got this:



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

+2


source







All Articles