1

I ran into a little problem trying to check if a file exists on a website that is htaccess protected.

This works great when the file is not htaccess protected with a username and password authentication process.

#!/bin/bash

url="some file on a website"

function validate_url(){
   if [[ `wget -S --spider $1  2>&1 | grep 'HTTP/1.1 200 OK'` ]]; then
      echo "true";
   fi
}  

if `validate_url $url >/dev/null`; then
   echo "BINGO - File $url exists ...!";
   wget $url;
fi

I have tried to do the same process on a website that is htaccess protected and I run into a false positive. Here's my script:

#!/bin/bash

sourcefile="some file on a htaccess website"
usr="somedude"
pswd="someword"

function validate_url(){
  if [[ `wget -S --spider --user="${usr}" --password="${pswd}" $1  2>&1 | grep 'HTTP/1.1 200 OK'` ]]; then echo "true"; fi
}


if `validate_url $sourcefile >/dev/null`; then
   echo "BINGO - File $sourcefile exists ...!";
   wget --user="$usr" --password="$pswd" $sourcefile;
fi

When I run my script with bash -v script.sh, I see this which is very confusing because the file does NOT exist on the htaccess protected website.

validate_url $sourcefile >/dev/null
wget -S --spider --user="${usr}" --password="${pswd}" $1  2>&1 | grep 'HTTP/1.1 200 OK'
File $sourcefile exists ...!
--2017-09-30 01:06:29--  $sourcefILE
Resolving $sourcefile $website
Connecting to $website ... connected.
HTTP request sent, awaiting response... 401 Unauthorized

Where did I go wrong?

Vituvo
  • 1,008
  • 1
  • 9
  • 29

0 Answers0