Guest User

deep-query.sh

a guest
May 14th, 2020
331
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.18 KB | None | 0 0
  1. #!/usr/bin/env bash
  2.  
  3. # Query pi-hole's domain lists, but follow CNAMEs. Useful for debugging why
  4. # a site is being blocked if CNAME_DEEP_INSPECT=true is set (which it is by
  5. # default on version 5.0)
  6.  
  7. # Nameserver for looking for CNAMES. Needs to be different from pihole itself;
  8. # or, pihole's group management needs to exclude localhost from blocking. (If
  9. # we use pihole to look for CNAMES but the domain is blocked, we won't find
  10. # any even if they exist.)
  11. NS="1.1.1.1"
  12.  
  13. # For a given domain name, return a list containing itself and all cnames
  14. # down the "chain" from it.
  15. # Usage: get_all_cnames example.com some.name.server
  16. get_all_cnames()
  17. {
  18.         local query
  19.         local ns
  20.         local cnames
  21.         query="$1"
  22.         ns="$2"
  23.        
  24.         # Drop trailing period from query, if any
  25.         query=${query%.}
  26.  
  27.         # All cnames for this query returned by dig
  28.         cnames=$(dig @$ns CNAME $query +short)
  29.        
  30.         # Echo this query, then recursively call this function for
  31.         # all cnames. Traverses depth-first through CNAME chains. Supports
  32.         # cases when multiple cnames are returned by dig; even though that
  33.         # shouldn't happen it does sometimes.
  34.         echo -n $query
  35.         for cn in $cnames; do
  36.                 echo -n " "
  37.                 echo -n $(get_all_cnames $cn $ns)
  38.         done
  39. }
  40.  
  41. # Echo usage to command line
  42. usage()
  43. {
  44.         echo 'Usage:'
  45.         echo '$0 [options] domain'
  46.         echo ''
  47.         echo 'Behaves the same way as pihole -q except it will also search'
  48.         echo 'for any domains which are CNAMES of the supplied domain. (i.e.'
  49.         echo 'it does "deep CNAME inspection")'
  50.         echo ''
  51.         echo 'For available options, see pihole -q --help'
  52. }
  53.  
  54. # Parse arguments
  55. if [ $# -lt 1 ]; then
  56.         usage
  57.         exit 1
  58. fi
  59. QUERY=${!#}
  60. PHQARGS=${@:1:$#-1} # All but the last argument
  61.  
  62. # Call 'pihole -q' on the original domain and all cnames
  63. for name in $(get_all_cnames $QUERY $NS); do
  64.         if [ $name = $QUERY ]; then
  65.                 echo "Checking $name ..."
  66.         else
  67.                 echo "Checking CNAME $name ..."
  68.         fi
  69.         pihole -q $PHQARGS $name
  70. done
Advertisement
Add Comment
Please, Sign In to add comment