LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Bash script to get result of nslookup against several DNS servers? (https://www.linuxquestions.org/questions/linux-newbie-8/bash-script-to-get-result-of-nslookup-against-several-dns-servers-4175659473/)

riahc3 08-20-2019 12:28 PM

Bash script to get result of nslookup against several DNS servers?
 
Hello

I want to do a nslookup of a FQDN.

First Ill try it against my local DNS server.

If that doesnt work, Ill have a text file with several DNS servers (Google, Cloudflare, etc) and try to nslookup against those.

If one DNS server doesnt give a reply the next, and etc....The first one that gives a reply, I want to save the IP in a variable.

How can I do this?

rtmistler 08-20-2019 01:17 PM

Is this a Linux Newbie question, a Networking question, or a General question?

It seems as if you have a process:
Quote:

Originally Posted by riahc3 (Post 6027375)
First Ill try it against my local DNS server.

If that doesnt work, Ill have a text file with several DNS servers (Google, Cloudflare, etc) and try to nslookup against those.

If one DNS server doesnt give a reply the next, and etc....The first one that gives a reply, I want to save the IP in a variable.

Can you achieve this result using some series of commands, and if so, what are those commands and what parts of the output do you need to proceed?
Quote:

Originally Posted by riahc3 (Post 6027375)
I want to do a nslookup of a FQDN.

What I'd say to this is that anything you can write on the command line, you can code into a bash script, or another type of script.

If you have a series of commands which work for you, please share those, and would be happy to offer ways to adjust the output so that you can use parts of it as variables within your script, as well as help you code some series of commands into a script. I'm just no expert at exactly the content which you want, therefore do not wish to provide the commands in place of your expertise for that.

riahc3 08-20-2019 03:07 PM

Quote:

Originally Posted by rtmistler (Post 6027414)
Is this a Linux Newbie question, a Networking question, or a General question?

It seems as if you have a process:Can you achieve this result using some series of commands, and if so, what are those commands and what parts of the output do you need to proceed?

What I'd say to this is that anything you can write on the command line, you can code into a bash script, or another type of script.

If you have a series of commands which work for you, please share those, and would be happy to offer ways to adjust the output so that you can use parts of it as variables within your script, as well as help you code some series of commands into a script. I'm just no expert at exactly the content which you want, therefore do not wish to provide the commands in place of your expertise for that.

I can Powershell it:

Code:

$servertrying="gosdfsdfogle.com"
Try
{
    $ip=Resolve-DnsName -Name $servertrying -Type A | Select -ExpandProperty IPAddress -ErrorAction Stop
  }
Catch
{
    Try
    {
    $ip=Resolve-DnsName -Name $servertrying -Server 1.1.1.1 -Type A | Select -ExpandProperty IPAddress -ErrorAction Stop
    }
    Catch
    {
   
                Try
                {
                $ip=Resolve-DnsName -Name $servertrying -Server 8.8.8.8 -Type A | Select -ExpandProperty IPAddress -ErrorAction Stop
                }
                Catch
                {
                    Write-Host "Cannot resolve $servertrying !"
                }

    }
   
}

Does this make it clearer what I want to do? :)

rtmistler 08-20-2019 03:22 PM

Well I don't know powershell. That stuff looks like Java or CPP with the try-catch statements.

Is Resolve-DnsName an available command on your system?

Appears as if you can just issue that statement (along with the variable set):
Code:

servertrying="gosdfsdfogle.com"
ip=Resolve-DnsName -Name $servertrying -Server 1.1.1.1 -Type A | Select -ExpandProperty IPAddress -ErrorAction Stop

to a command prompt and you'll assign the variable $ip. From that point, you can do it in a bash script. Does that statement or those statements yield what you need?

What I'm saying is that you can take the $ off of those at the start of the line, copy and past those lines:
servertrying="gosdfsdfogle.com"
ip=Resolve-DnsName -Name $servertrying -Server 1.1.1.1 -Type A | Select -ExpandProperty IPAddress -ErrorAction Stop
<or any of the ip= commands)

into your command prompt and you will be able to inspect the results:
Code:

$ echo $servertrying
gosdfsdfogle.com
$ echo $ip
<whatever the results may be I don't know>

And from there, start writing a bash script.

Or is there a problem writing bash scripts? If so, the link in my signature citing My Bash Blog is for debugging bash scripts, but also contains links to the Bash Script guide to get you started with bash scripting in general. Another starting point is:
Code:

#!/bin/bash
First line of your script. From there, it's just command line statements, along with some extra things you can do like if tests and loops. You can do those directly in the command line by the way, just typing is sometimes difficult, this is why we write it in a script. But a script is nothing more than an execute permissions text file with the .sh extension, the #!/bin/bash line as the first line, and a bunch of command line calls, that you'd normally type.

Sefyir 08-21-2019 01:03 AM

You might try using dig

Code:

dig +short @1.1.1.1 @8.8.8.8 gosdfsdfogle.com A
Code:

dig
+timeout=1        # DNS Query times out after 1s
+short            # less verbose output
@1.1.1.1          # @ = server, check this server first
@8.8.8.8          # @ = server, check this server second
gosdfsdfogle.com  # domain to check
A                # check A records

Code:

man dig


All times are GMT -5. The time now is 03:17 AM.