Search code examples
linuxbashunixawknslookup

Passing name of server from a list to nslookup with Awk


Backstory: I am making a script to check a blacklist of domains and see which domains are still valid (resolve to an IP) so I can cut out the old non-resolving domains from the list. The list has millions of lines, so I am using awk (instead of a "do while read") to increase speed.

I am trying to write an awk statement that will nslookup a list of domains and print only the list of resolve-able domains to another list.

I am almost finished except I am stuck on one part- how can I specify the server that nslookup uses?

I have -port=54 working, but I am also trying to configure which DNS server nslookup uses.

awk '{print $1}' /etc/pihole/gravity.list | nslookup -port=54| awk '/[Nn]ame/ {print $NF}'  >> /etc/pihole/gravityProcessed.list

If I try to specify -server= this is not a valid parameter If I try to change the nslookup to use 1.1.1.1, instead of using 1.1.1.1 as the server, it tries to nslookup 1.1.1.1 instead.

awk '{print $1}' /etc/pihole/gravity.list | nslookup 1.1.1.1 | awk '/[Nn]ame/ {print $NF}'  >> /etc/pihole/gravityProcessed.list

The issue is that nslookup doesnt have a -server parameter afaik (yes it has a -port parameter) So I need awk to do:

nslookup [INSERT HOST] server -port=

Here is a sample of /etc/pihole/gravity.list

google.com
yahoo.com
skype.com
microsoft.com

The other option I wish to try to incorporate is a regex of a proper domain syntax as the script currently dies if it hits a domain that isnt formatted properly. Like putting this through a grep (?=^.{4,253}$)(^(?:[a-zA-Z0-9](?:(?:[a-zA-Z0-9\-]){0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}$)


Solution

  • When you want to use an alternative nameserver, look at the following instruction for nslookup:

    ARGUMENTS
    Interactive mode is entered in the following cases:
    1. when no arguments are given (the default name server will be used)
    2. when the first argument is a hyphen (-) and the second argument is the host name or Internet address of a name server.

    So try

    nslookup - 1.1.1.1 < /etc/pihole/gravity.list 2>/dev/null | 
       awk '/[Nn]ame/ {print $NF}' >> /etc/pihole/gravityProcessed.list