#StackBounty: #bash #grep grep with PTR records and domain+TLD match

Bounty: 200

I’m trying to determine if the domain+TLD are present in a list, after running the host command for an IP.

My script looks like this:

while read ip; do
  PTR=$(host $ip | rev | cut -d" " -f1 | rev | sed 's/.$//')
  if grep -q "$PTR" list.txt
  then
    echo "Match in list"
  else
    echo "No match in list"
  fi
done <ips.txt

The list.txt will contain:

dns.google
shodan.io

If I run my script for 8.8.8.8, which returns dns.google the script works as expected. If I run it for 198.20.99.130 it will fail (not match) as the result is census4.shodan.io.

Is there a way I can have grep match only if the domain+TLD (in this case shodan.io) is in the list?

While census4.shodan.io should match the list.txt, a domain like shodan.io.example.net shouldn’t.


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.