0

I'm creating a tool that makes security searches. My printer detection script is getting strings in another .txt file like "Printer Firmware Version" and using grep tool to check if its exist in target host website. But my script split this strings and uses them as different variables like Printer, Firmware, Version. How can I get that strings without splitting them?

   found=(`cat temp.txt`) # <- This line for target
   httarr=(`cat httpport.txt`)
   printer_array=(`cd ..; cat keywords/printerwords.txt`)
   for lo in ${httarr[*]}
   do
     ifacelook=`curl -sSL http://$found:$lo`
     for pr in ${printer_array[*]}
     do
        echo $pr # <-This line is to see incoming variables
        echo $found | grep -o '${pr}' &>/dev/null
        if [ $? -eq 0 ];then
          echo -en "$cyan>$green|${red}printer$green|$cyan>$default This device is a printer check: http://$found:$lo \n"
        fi
     done
   done

Incoming variables:
Printer
Firmware
Version
.
.
.
Variables I want:
Printer Firmware Version
.
.
.
1
  • mapfile -t found < temp.txt and mapfile -t httarr < httpport.txt. Get rid of found=(cat temp.txt) and httarr=(cat httpport.txt) (or set IFS=$'\n' and then restore the default afterwards). Commented Jun 18, 2019 at 0:01

1 Answer 1

2

Your word splitting occurrs with:

found=(`cat temp.txt`) # <- This line for target
httarr=(`cat httpport.txt`)

(which both include a UUOc (Unnecessary Use Of cat))

Instead use:

mapfile -t found < temp.txt
mapfile -t httarr < httpport.txt

mapfile (synonymous with readarray) will read each line of a file from stdin into the indexed arrays.

Not recommended, but you can also set IFS (Internal Field Separator) to only break on newlines with:

set -o noglob                 ## disable globbing to protect from * in line
oldifs="$IFS"                 ## save current IFS
IFS=$'\n'                     ## set IFS to newline only
found=( $(< temp.txt) )       ## fill arrays
httarr=( $(< httpport.txt) )
IFS="$oldifs"                 ## restore original IFS
set +o noglob                 ## restore globbing  (thanks to Charles)

Look things over and let me know if you have further questions.

Sign up to request clarification or add additional context in comments.

3 Comments

Also, all other variable- and command substitutions should be in double-quotes (e.g. echo "$found" | grep -o "${pr}" instead of echo $found | grep -o '${pr}'). To get the elements of an array, use "${httarr[@]}" instead of ${httarr[*]}`. shellcheck.net is good at pointing out things like this.
In the version where you change IFS, you also need to disable globbing. Otherwise, a line containing only * will still be replaced with a list of filenames.
@CharlesDuffy - thank you Charles, updated. Thanks as well Gordon.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.