Guest User

Untitled

a guest
Oct 18th, 2019
318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.20 KB | None | 0 0
  1. # parse a hosts file with awk and perform a query for each line,
  2. # cycling through a list of DNS servers to avoid overloading them
  3.  
  4. # define multiple DNS servers to avoid overloading a single one
  5. DNS_SERVERS_POOL="1.1.1.1 8.8.8.8 1.0.0.1 8.8.4.4"
  6.  
  7.  
  8. ### Main
  9. awk -v DNS_SERVERS_POOL="$DNS_SERVERS_POOL" '
  10. function dns_exists(host, dns_server) {
  11.    cmd = sprintf("nslookup -port=53 -retry=5 -timeout=5 %s %s | grep -q -F Name", host, dns_server)
  12.    return !system(cmd)
  13. }
  14.  
  15. BEGIN {
  16.    # Store the DNS servers in array and keep num_of_servers variable to use in modulo operation
  17.    num_of_servers = split(DNS_SERVERS_POOL, Dns_servers);
  18. }
  19.  
  20. # Main loop
  21. # Perform a query for each line using a different server each time
  22. # also skip lines (domains) starting with "-"
  23. !/^-/ && /^([a-zA-Z0-9-]*[a-zA-Z0-9]\.)+[a-zA-Z][a-zA-Z]+$/ {
  24.    # choose the server for this line
  25.    selected_server = Dns_servers[NR % num_of_servers + 1]
  26.    
  27.    # if the query is successful print the domain
  28.    if( dns_exists($1, selected_server) ) {
  29.        print $1 > "/etc/pihole/gravity.resolved"
  30.    }
  31. }
  32. END { close("/etc/pihole/gravity.resolved") }'
  33.  
  34. cat /etc/pihole/gravity.resolved > /etc/pihole/gravity.list
Advertisement
Add Comment
Please, Sign In to add comment