0

Trying to build a shell script which determines if the site is using PulseVPN or not. The script takes a input file which contains a list of URLs, iterate each URL line by line, perform cURL operation and grep for specific string. And if string found, display a echo message "Website using PulseVPN"

Here is my code -

Keyword="dana-na" # keyword to find for PulseSecure SSL VPN
while read -r line; 
do
if curl -s -L "$line" | grep "$keyword" > /dev/null
then
    # if the keyword is in the conent
    echo " The website $line is using Pulse Secure VPN" 
else
    echo "Pulse Secure VPN Not Found on $line"
fi
done < urls.txt

Problem The code is working but The problem here is when i include only one URL in input file. It gives correct result. But if i include more than one URL in input file, then it gives incorrect results. Can anyone explains why is this happening. Is anything wrong in code ?

Akshay Sharma
  • 65
  • 1
  • 9
  • 3
    Your code can't work because grep uses $keyword variable while you define Keyword (case is different). In general your loop should work ok but it will print **The website $line is using Pulse Secure VPN** for all urls due to keyword is empty – Maxim Sagaydachny Dec 18 '19 at 20:37
  • 1
    What is the incorrect result you're seeing? – that other guy Dec 18 '19 at 20:43
  • 2
    Note that `grep -q "$keyword"` is more efficient than `grep "$keyword" >/dev/null`, as the former (but not the latter) can stop as soon as the first match is found, instead of reading to the end of the file and writing content to `/dev/null`. – Charles Duffy Dec 18 '19 at 20:48
  • 4
    BTW, it's often helpful to run `bash -x yourscript` to trace the individual commands that get run and compare them against what you expect. For example, if you see your line being displayed with `$'\r'` characters at the end, then you know there's a newline type problem; if you see `''` used instead of your keyword, you know to look at that (and can figure out the `Keyword`/`keyword` name mismatch issue), etc. – Charles Duffy Dec 18 '19 at 20:49
  • 2
    I'm not sure if curl consumes any data from its stdin, but you might want to try `curl <&- ...` to make sure it's not reading data intended for `read`. – William Pursell Dec 18 '19 at 21:12
  • @WilliamPursell, an attempt to read from a closed file descriptor triggers an error on its own, so ` – Charles Duffy Dec 21 '19 at 16:18

1 Answers1

-1

It might be skipping over the last line due to how read works. Try

while read -r line || [[ -n $line ]];

Look at this post for more information.

Peymon
  • 41
  • 1
  • 7
  • If there isn't enough information in the question to allow a firm answer as opposed to a speculative one, comment instead of answering (and perhaps vote-to-close as lacking sufficient information). Moreover, if this were *truly* the problem, we'd want to close the question as duplicate of the one you're linking, instead of adding an answer that just points to a preexisting knowledge base entry. – Charles Duffy Dec 18 '19 at 20:45
  • Gotta love the unending spiral of losing points because I can't comment and losing points for answering. – Peymon Dec 18 '19 at 21:03