LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-20-2019, 01:28 PM   #1
riahc3
Member
 
Registered: Dec 2002
Posts: 176

Rep: Reputation: 1
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?
 
Old 08-20-2019, 02:17 PM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,157
Blog Entries: 13

Rep: Reputation: 4221Reputation: 4221Reputation: 4221Reputation: 4221Reputation: 4221Reputation: 4221Reputation: 4221Reputation: 4221Reputation: 4221Reputation: 4221Reputation: 4221
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 View Post
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 View Post
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.
 
Old 08-20-2019, 04:07 PM   #3
riahc3
Member
 
Registered: Dec 2002
Posts: 176

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by rtmistler View Post
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?
 
Old 08-20-2019, 04:22 PM   #4
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,157
Blog Entries: 13

Rep: Reputation: 4221Reputation: 4221Reputation: 4221Reputation: 4221Reputation: 4221Reputation: 4221Reputation: 4221Reputation: 4221Reputation: 4221Reputation: 4221Reputation: 4221
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.

Last edited by rtmistler; 08-20-2019 at 04:26 PM.
 
Old 08-21-2019, 02:03 AM   #5
Sefyir
Member
 
Registered: Mar 2015
Distribution: Linux Mint
Posts: 623

Rep: Reputation: 309Reputation: 309Reputation: 309Reputation: 309
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
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash script that acts as a DNS proxy, except it queries several DNS servers Ulysses_ Linux - Networking 11 03-06-2014 01:31 PM
am not getting any result when am using 'if', without getting result for below script alavala53 Linux - General 3 10-25-2012 07:00 PM
Any tool to compare DNS results from several DNS servers? Ulysses_ Linux - Networking 13 11-04-2010 04:36 PM
[bash] how to put nslookup result next to ip in file kriezo Programming 5 02-25-2008 01:48 AM
compare the hostnames in /etc/hosts with the result of nslookup on a ip from /etc/hos pete83 AIX 3 12-18-2007 04:34 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:03 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration