Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env bash
- # Query pi-hole's domain lists, but follow CNAMEs. Useful for debugging why
- # a site is being blocked if CNAME_DEEP_INSPECT=true is set (which it is by
- # default on version 5.0)
- # Nameserver for looking for CNAMES. Needs to be different from pihole itself;
- # or, pihole's group management needs to exclude localhost from blocking. (If
- # we use pihole to look for CNAMES but the domain is blocked, we won't find
- # any even if they exist.)
- NS="1.1.1.1"
- # For a given domain name, return a list containing itself and all cnames
- # down the "chain" from it.
- # Usage: get_all_cnames example.com some.name.server
- get_all_cnames()
- {
- local query
- local ns
- local cnames
- query="$1"
- ns="$2"
- # Drop trailing period from query, if any
- query=${query%.}
- # All cnames for this query returned by dig
- cnames=$(dig @$ns CNAME $query +short)
- # Echo this query, then recursively call this function for
- # all cnames. Traverses depth-first through CNAME chains. Supports
- # cases when multiple cnames are returned by dig; even though that
- # shouldn't happen it does sometimes.
- echo -n $query
- for cn in $cnames; do
- echo -n " "
- echo -n $(get_all_cnames $cn $ns)
- done
- }
- # Echo usage to command line
- usage()
- {
- echo 'Usage:'
- echo '$0 [options] domain'
- echo ''
- echo 'Behaves the same way as pihole -q except it will also search'
- echo 'for any domains which are CNAMES of the supplied domain. (i.e.'
- echo 'it does "deep CNAME inspection")'
- echo ''
- echo 'For available options, see pihole -q --help'
- }
- # Parse arguments
- if [ $# -lt 1 ]; then
- usage
- exit 1
- fi
- QUERY=${!#}
- PHQARGS=${@:1:$#-1} # All but the last argument
- # Call 'pihole -q' on the original domain and all cnames
- for name in $(get_all_cnames $QUERY $NS); do
- if [ $name = $QUERY ]; then
- echo "Checking $name ..."
- else
- echo "Checking CNAME $name ..."
- fi
- pihole -q $PHQARGS $name
- done
Advertisement
Add Comment
Please, Sign In to add comment