Nslookup command line with A record IP as sole output












7















I've found the following command to get your current public IP that works well from command line:



nslookup myip.opendns.com resolver1.opendns.com


I want to be able to run a command though that JUST prints the resulant IP. (Right now it shows the specified DNS server and it's IP along with all the other info IE:



Server:  resolver1.opendns.com
Address: 208.67.222.222

Non-authoritative answer:
Name: myip.opendns.com
Address: 123.123.123.123


I want it to just output:
123.123.123.123



Not sure if the is a command line flag to get what I want or if I can use some command line trickery to get just the output I want (ultimately, I want to redirect the output to a file, "> filename.txt"










share|improve this question























  • How do you expect to deal with names that return more than one IP address? Which OS are you using?

    – Ƭᴇcʜιᴇ007
    Sep 16 '14 at 19:59


















7















I've found the following command to get your current public IP that works well from command line:



nslookup myip.opendns.com resolver1.opendns.com


I want to be able to run a command though that JUST prints the resulant IP. (Right now it shows the specified DNS server and it's IP along with all the other info IE:



Server:  resolver1.opendns.com
Address: 208.67.222.222

Non-authoritative answer:
Name: myip.opendns.com
Address: 123.123.123.123


I want it to just output:
123.123.123.123



Not sure if the is a command line flag to get what I want or if I can use some command line trickery to get just the output I want (ultimately, I want to redirect the output to a file, "> filename.txt"










share|improve this question























  • How do you expect to deal with names that return more than one IP address? Which OS are you using?

    – Ƭᴇcʜιᴇ007
    Sep 16 '14 at 19:59
















7












7








7


2






I've found the following command to get your current public IP that works well from command line:



nslookup myip.opendns.com resolver1.opendns.com


I want to be able to run a command though that JUST prints the resulant IP. (Right now it shows the specified DNS server and it's IP along with all the other info IE:



Server:  resolver1.opendns.com
Address: 208.67.222.222

Non-authoritative answer:
Name: myip.opendns.com
Address: 123.123.123.123


I want it to just output:
123.123.123.123



Not sure if the is a command line flag to get what I want or if I can use some command line trickery to get just the output I want (ultimately, I want to redirect the output to a file, "> filename.txt"










share|improve this question














I've found the following command to get your current public IP that works well from command line:



nslookup myip.opendns.com resolver1.opendns.com


I want to be able to run a command though that JUST prints the resulant IP. (Right now it shows the specified DNS server and it's IP along with all the other info IE:



Server:  resolver1.opendns.com
Address: 208.67.222.222

Non-authoritative answer:
Name: myip.opendns.com
Address: 123.123.123.123


I want it to just output:
123.123.123.123



Not sure if the is a command line flag to get what I want or if I can use some command line trickery to get just the output I want (ultimately, I want to redirect the output to a file, "> filename.txt"







networking command-line dns command-line-arguments






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Sep 16 '14 at 19:51









BondUniverseBondUniverse

1774620




1774620













  • How do you expect to deal with names that return more than one IP address? Which OS are you using?

    – Ƭᴇcʜιᴇ007
    Sep 16 '14 at 19:59





















  • How do you expect to deal with names that return more than one IP address? Which OS are you using?

    – Ƭᴇcʜιᴇ007
    Sep 16 '14 at 19:59



















How do you expect to deal with names that return more than one IP address? Which OS are you using?

– Ƭᴇcʜιᴇ007
Sep 16 '14 at 19:59







How do you expect to deal with names that return more than one IP address? Which OS are you using?

– Ƭᴇcʜιᴇ007
Sep 16 '14 at 19:59












9 Answers
9






active

oldest

votes


















6














Nslookup with A record IP as sole output



Assuming you are using Windows, this can be done using a simple one line command.



From the command line:



for /f "skip=4 usebackq tokens=2" %a in (`nslookup myip.opendns.com resolver1.opendns.com`) do echo %a > ip.txt


From a batch file:



for /f "skip=4 usebackq tokens=2" %%a in (`nslookup myip.opendns.com resolver1.opendns.com`) do echo %%a > ip.txt


Notes:




  • The public IP address is stored in a file (ip.txt).

  • The above does not require non standard windows commands like PowerShell, .Net or awk.




Further Reading





  • An A-Z Index of the Windows CMD command line - An excellent reference for all things Windows cmd line related.


  • for /f - Loop command against the results of another command.


  • nslookup - Lookup IP addresses on a NameServer.






share|improve this answer


























  • "Assuming you are using Windows"... I'm not sure that should ever be an assumption here.

    – RCross
    Dec 19 '18 at 9:29



















10














nslookup was never really intended for scripted use. You really want to use dig instead, which with the +short option produces machine-readable output according to the query parameters.



dig +short myip.opendns.com @resolver1.opendns.com





share|improve this answer





















  • 1





    Welcome to Super User! Please explain why this is a correct answer; not all of us know how to use dig.

    – Glorfindel
    Jul 3 '17 at 14:38













  • And it will help other users with the same problem

    – yass
    Jul 3 '17 at 15:26






  • 1





    This solution is preferable over others using awk and/or sed since piping is often problematic, and it this case, unnecessarily complex.

    – StockB
    May 9 '18 at 13:55






  • 1





    so should I use the last ouput line? In case CNAME there are two or more output lines. dig +short www.getready.cz | tail -1

    – Jaro
    Oct 15 '18 at 9:04













  • This is by far the best answer, as it's a cross-platform solution (ok Windows users will have to download dig) that avoids messing around with OS-specific string manipulation.

    – RCross
    Dec 19 '18 at 9:35



















6














This is a good usecase for awk.



nslookup myip.opendns.com resolver1.opendns.com | awk -F': ' 'NR==6 { print $2 } '



Here we are piping to awk, delimiting by ": " and then only outputting the second delimited field of line 6.






share|improve this answer



















  • 2





    OP hasn't specified but I'd bet he's on Windows, and there's no awk in Windows.

    – Ƭᴇcʜιᴇ007
    Sep 16 '14 at 20:03






  • 1





    @Techie007 But the OP has not specified he is on windows either, so we may as well provide complete information. The above answer works for all Nixes, which nicely complements other replies, including yours.

    – MariusMatutiae
    Sep 17 '14 at 10:11











  • Yes, on Windows servers.

    – BondUniverse
    Sep 18 '14 at 22:27






  • 1





    This doesn't work in some cases (the hard-coded line number can be wrong). I use this instead: nslookup-ip() { nslookup "$@" | tail -n +3 | sed -n 's/Address:s*//p' }

    – zeroimpl
    Dec 6 '16 at 17:52





















3














If you're on Windows, and have PowerShell installed (v1 or better) (and a .Net version) you could use a (long) one-liner like this:



[System.Net.Dns]::GetHostAddresses("www.google.com")[0] | Select IPAddressToString -ExpandProperty IPAddressToString | Out-File c:folderfilename.txt



This will lookup www.google.com and put the first returned IPv4 address into a file.



If you're using PowerShell v3+ on Windows 8+ (or Server 2012+) you can user the use the Resolve-DnsName cmdlet instead of the .Net GetHostAddress call. ie:



(Resolve-DnsName www.google.com)[0] | Select IPAddressToString -ExpandProperty IPAddressToString | Out-File c:folderfilename.txt



Simply change www.google.com to your preferred domain name. Or put it in a PowerShell script and set it up to accept an argument (of the domain name you want to look up).



More info on that: How to pass an argument to a PowerShell script?






share|improve this answer

































    1














    Works good for me on my Linux machine. I've never tried it on other systems though but Google has a lot of articles on how to install dig for example on Windows



    The only thing to note, for local hostnames search domain should be added explicitly.
    So if you have myhost host in your local network with search domain mynetwork put



    dig +short myhost.mynetwork


    on command line.



    Examples:



    sergeyi@sergeyi:~$ dig +short google.ru
    173.194.222.94

    sergeyi@sergeyi:~$ dig A +short google.ru
    173.194.222.94

    sergeyi@sergeyi:~$ dig AAAA +short google.ru
    2a00:1450:4010:c0b::5e





    share|improve this answer

































      0














      If your goal is to retrieve your external IP with a script, a possible would be the use of a very simple PowerShell function :



      function Get-ExternalIP {
      (Invoke-WebRequest ifconfig.me/ip).Content
      }


      Running this function will return your external IP, and no other useless information.



      Source and examples : http://jfrmilner.wordpress.com/2012/12/22/powershell-quick-tip-03-whats-my-external-ip-address-windows-command-line/



      Alternatively, if you want to be able to get the IP resolution for other hosts, you should have a look at the Resolve-DnsName cmdlet. Unfortunatelly, your computer must be running Windows 8.1 or Windows Server 2012 R2 in order to use this cmdlet. Here is an example I just performed :



      enter image description here



      Hope this helps !






      share|improve this answer


























      • I need the command to work reliably on any Windows server 2003+. I agree though that if Power shell was viable, I would prefer your solution.

        – BondUniverse
        Sep 18 '14 at 22:29



















      0














      Simple.



      In nslookup use:



      Set type=A



      Then lookup. The set command will show only outputs for A records. You can use this for MX, CNAME, AAA etc.






      share|improve this answer































        -1














        Not ideal, but I sometimes use this method:



        ping -c 1 myip.opendns.com | grep -ohE "(([0-9]+.[0-9]+.[0-9]+.[0-9]+))" | head -1 | sed "s/[()]//g"






        share|improve this answer
























        • Why exactly is this solution not ideal? I know the answer but I normally provide an attempt to provide clarity to unclear answers before I vote on them.

          – Ramhound
          Oct 14 '15 at 12:18











        • Not everybody has grep and sed` installed on their Windows PCs.

          – DavidPostill
          Oct 14 '15 at 15:29











        • and ping syntax is wrong.

          – Francisco Tapia
          Oct 15 '15 at 15:34











        • What's wrong with the syntax? I just wanted to give another method that I use and I thought that someone may find that useful as well. It works for me. You don't have to use it.

          – AlonL
          Oct 16 '15 at 15:54



















        -1














        Using PowerShell, you can run below:



        (((nslookup myip.opendns.com resolver1.opendns.com 2>null| select-string -pattern "Address:") -split ":")[3]).Trim()





        share|improve this answer























          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "3"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f812664%2fnslookup-command-line-with-a-record-ip-as-sole-output%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          9 Answers
          9






          active

          oldest

          votes








          9 Answers
          9






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          6














          Nslookup with A record IP as sole output



          Assuming you are using Windows, this can be done using a simple one line command.



          From the command line:



          for /f "skip=4 usebackq tokens=2" %a in (`nslookup myip.opendns.com resolver1.opendns.com`) do echo %a > ip.txt


          From a batch file:



          for /f "skip=4 usebackq tokens=2" %%a in (`nslookup myip.opendns.com resolver1.opendns.com`) do echo %%a > ip.txt


          Notes:




          • The public IP address is stored in a file (ip.txt).

          • The above does not require non standard windows commands like PowerShell, .Net or awk.




          Further Reading





          • An A-Z Index of the Windows CMD command line - An excellent reference for all things Windows cmd line related.


          • for /f - Loop command against the results of another command.


          • nslookup - Lookup IP addresses on a NameServer.






          share|improve this answer


























          • "Assuming you are using Windows"... I'm not sure that should ever be an assumption here.

            – RCross
            Dec 19 '18 at 9:29
















          6














          Nslookup with A record IP as sole output



          Assuming you are using Windows, this can be done using a simple one line command.



          From the command line:



          for /f "skip=4 usebackq tokens=2" %a in (`nslookup myip.opendns.com resolver1.opendns.com`) do echo %a > ip.txt


          From a batch file:



          for /f "skip=4 usebackq tokens=2" %%a in (`nslookup myip.opendns.com resolver1.opendns.com`) do echo %%a > ip.txt


          Notes:




          • The public IP address is stored in a file (ip.txt).

          • The above does not require non standard windows commands like PowerShell, .Net or awk.




          Further Reading





          • An A-Z Index of the Windows CMD command line - An excellent reference for all things Windows cmd line related.


          • for /f - Loop command against the results of another command.


          • nslookup - Lookup IP addresses on a NameServer.






          share|improve this answer


























          • "Assuming you are using Windows"... I'm not sure that should ever be an assumption here.

            – RCross
            Dec 19 '18 at 9:29














          6












          6








          6







          Nslookup with A record IP as sole output



          Assuming you are using Windows, this can be done using a simple one line command.



          From the command line:



          for /f "skip=4 usebackq tokens=2" %a in (`nslookup myip.opendns.com resolver1.opendns.com`) do echo %a > ip.txt


          From a batch file:



          for /f "skip=4 usebackq tokens=2" %%a in (`nslookup myip.opendns.com resolver1.opendns.com`) do echo %%a > ip.txt


          Notes:




          • The public IP address is stored in a file (ip.txt).

          • The above does not require non standard windows commands like PowerShell, .Net or awk.




          Further Reading





          • An A-Z Index of the Windows CMD command line - An excellent reference for all things Windows cmd line related.


          • for /f - Loop command against the results of another command.


          • nslookup - Lookup IP addresses on a NameServer.






          share|improve this answer















          Nslookup with A record IP as sole output



          Assuming you are using Windows, this can be done using a simple one line command.



          From the command line:



          for /f "skip=4 usebackq tokens=2" %a in (`nslookup myip.opendns.com resolver1.opendns.com`) do echo %a > ip.txt


          From a batch file:



          for /f "skip=4 usebackq tokens=2" %%a in (`nslookup myip.opendns.com resolver1.opendns.com`) do echo %%a > ip.txt


          Notes:




          • The public IP address is stored in a file (ip.txt).

          • The above does not require non standard windows commands like PowerShell, .Net or awk.




          Further Reading





          • An A-Z Index of the Windows CMD command line - An excellent reference for all things Windows cmd line related.


          • for /f - Loop command against the results of another command.


          • nslookup - Lookup IP addresses on a NameServer.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Apr 16 '17 at 8:09

























          answered Sep 16 '14 at 20:45









          DavidPostillDavidPostill

          104k25225259




          104k25225259













          • "Assuming you are using Windows"... I'm not sure that should ever be an assumption here.

            – RCross
            Dec 19 '18 at 9:29



















          • "Assuming you are using Windows"... I'm not sure that should ever be an assumption here.

            – RCross
            Dec 19 '18 at 9:29

















          "Assuming you are using Windows"... I'm not sure that should ever be an assumption here.

          – RCross
          Dec 19 '18 at 9:29





          "Assuming you are using Windows"... I'm not sure that should ever be an assumption here.

          – RCross
          Dec 19 '18 at 9:29













          10














          nslookup was never really intended for scripted use. You really want to use dig instead, which with the +short option produces machine-readable output according to the query parameters.



          dig +short myip.opendns.com @resolver1.opendns.com





          share|improve this answer





















          • 1





            Welcome to Super User! Please explain why this is a correct answer; not all of us know how to use dig.

            – Glorfindel
            Jul 3 '17 at 14:38













          • And it will help other users with the same problem

            – yass
            Jul 3 '17 at 15:26






          • 1





            This solution is preferable over others using awk and/or sed since piping is often problematic, and it this case, unnecessarily complex.

            – StockB
            May 9 '18 at 13:55






          • 1





            so should I use the last ouput line? In case CNAME there are two or more output lines. dig +short www.getready.cz | tail -1

            – Jaro
            Oct 15 '18 at 9:04













          • This is by far the best answer, as it's a cross-platform solution (ok Windows users will have to download dig) that avoids messing around with OS-specific string manipulation.

            – RCross
            Dec 19 '18 at 9:35
















          10














          nslookup was never really intended for scripted use. You really want to use dig instead, which with the +short option produces machine-readable output according to the query parameters.



          dig +short myip.opendns.com @resolver1.opendns.com





          share|improve this answer





















          • 1





            Welcome to Super User! Please explain why this is a correct answer; not all of us know how to use dig.

            – Glorfindel
            Jul 3 '17 at 14:38













          • And it will help other users with the same problem

            – yass
            Jul 3 '17 at 15:26






          • 1





            This solution is preferable over others using awk and/or sed since piping is often problematic, and it this case, unnecessarily complex.

            – StockB
            May 9 '18 at 13:55






          • 1





            so should I use the last ouput line? In case CNAME there are two or more output lines. dig +short www.getready.cz | tail -1

            – Jaro
            Oct 15 '18 at 9:04













          • This is by far the best answer, as it's a cross-platform solution (ok Windows users will have to download dig) that avoids messing around with OS-specific string manipulation.

            – RCross
            Dec 19 '18 at 9:35














          10












          10








          10







          nslookup was never really intended for scripted use. You really want to use dig instead, which with the +short option produces machine-readable output according to the query parameters.



          dig +short myip.opendns.com @resolver1.opendns.com





          share|improve this answer















          nslookup was never really intended for scripted use. You really want to use dig instead, which with the +short option produces machine-readable output according to the query parameters.



          dig +short myip.opendns.com @resolver1.opendns.com






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jul 3 '17 at 15:44









          tripleee

          1,73032130




          1,73032130










          answered Jul 3 '17 at 14:04









          brmbrm

          10912




          10912








          • 1





            Welcome to Super User! Please explain why this is a correct answer; not all of us know how to use dig.

            – Glorfindel
            Jul 3 '17 at 14:38













          • And it will help other users with the same problem

            – yass
            Jul 3 '17 at 15:26






          • 1





            This solution is preferable over others using awk and/or sed since piping is often problematic, and it this case, unnecessarily complex.

            – StockB
            May 9 '18 at 13:55






          • 1





            so should I use the last ouput line? In case CNAME there are two or more output lines. dig +short www.getready.cz | tail -1

            – Jaro
            Oct 15 '18 at 9:04













          • This is by far the best answer, as it's a cross-platform solution (ok Windows users will have to download dig) that avoids messing around with OS-specific string manipulation.

            – RCross
            Dec 19 '18 at 9:35














          • 1





            Welcome to Super User! Please explain why this is a correct answer; not all of us know how to use dig.

            – Glorfindel
            Jul 3 '17 at 14:38













          • And it will help other users with the same problem

            – yass
            Jul 3 '17 at 15:26






          • 1





            This solution is preferable over others using awk and/or sed since piping is often problematic, and it this case, unnecessarily complex.

            – StockB
            May 9 '18 at 13:55






          • 1





            so should I use the last ouput line? In case CNAME there are two or more output lines. dig +short www.getready.cz | tail -1

            – Jaro
            Oct 15 '18 at 9:04













          • This is by far the best answer, as it's a cross-platform solution (ok Windows users will have to download dig) that avoids messing around with OS-specific string manipulation.

            – RCross
            Dec 19 '18 at 9:35








          1




          1





          Welcome to Super User! Please explain why this is a correct answer; not all of us know how to use dig.

          – Glorfindel
          Jul 3 '17 at 14:38







          Welcome to Super User! Please explain why this is a correct answer; not all of us know how to use dig.

          – Glorfindel
          Jul 3 '17 at 14:38















          And it will help other users with the same problem

          – yass
          Jul 3 '17 at 15:26





          And it will help other users with the same problem

          – yass
          Jul 3 '17 at 15:26




          1




          1





          This solution is preferable over others using awk and/or sed since piping is often problematic, and it this case, unnecessarily complex.

          – StockB
          May 9 '18 at 13:55





          This solution is preferable over others using awk and/or sed since piping is often problematic, and it this case, unnecessarily complex.

          – StockB
          May 9 '18 at 13:55




          1




          1





          so should I use the last ouput line? In case CNAME there are two or more output lines. dig +short www.getready.cz | tail -1

          – Jaro
          Oct 15 '18 at 9:04







          so should I use the last ouput line? In case CNAME there are two or more output lines. dig +short www.getready.cz | tail -1

          – Jaro
          Oct 15 '18 at 9:04















          This is by far the best answer, as it's a cross-platform solution (ok Windows users will have to download dig) that avoids messing around with OS-specific string manipulation.

          – RCross
          Dec 19 '18 at 9:35





          This is by far the best answer, as it's a cross-platform solution (ok Windows users will have to download dig) that avoids messing around with OS-specific string manipulation.

          – RCross
          Dec 19 '18 at 9:35











          6














          This is a good usecase for awk.



          nslookup myip.opendns.com resolver1.opendns.com | awk -F': ' 'NR==6 { print $2 } '



          Here we are piping to awk, delimiting by ": " and then only outputting the second delimited field of line 6.






          share|improve this answer



















          • 2





            OP hasn't specified but I'd bet he's on Windows, and there's no awk in Windows.

            – Ƭᴇcʜιᴇ007
            Sep 16 '14 at 20:03






          • 1





            @Techie007 But the OP has not specified he is on windows either, so we may as well provide complete information. The above answer works for all Nixes, which nicely complements other replies, including yours.

            – MariusMatutiae
            Sep 17 '14 at 10:11











          • Yes, on Windows servers.

            – BondUniverse
            Sep 18 '14 at 22:27






          • 1





            This doesn't work in some cases (the hard-coded line number can be wrong). I use this instead: nslookup-ip() { nslookup "$@" | tail -n +3 | sed -n 's/Address:s*//p' }

            – zeroimpl
            Dec 6 '16 at 17:52


















          6














          This is a good usecase for awk.



          nslookup myip.opendns.com resolver1.opendns.com | awk -F': ' 'NR==6 { print $2 } '



          Here we are piping to awk, delimiting by ": " and then only outputting the second delimited field of line 6.






          share|improve this answer



















          • 2





            OP hasn't specified but I'd bet he's on Windows, and there's no awk in Windows.

            – Ƭᴇcʜιᴇ007
            Sep 16 '14 at 20:03






          • 1





            @Techie007 But the OP has not specified he is on windows either, so we may as well provide complete information. The above answer works for all Nixes, which nicely complements other replies, including yours.

            – MariusMatutiae
            Sep 17 '14 at 10:11











          • Yes, on Windows servers.

            – BondUniverse
            Sep 18 '14 at 22:27






          • 1





            This doesn't work in some cases (the hard-coded line number can be wrong). I use this instead: nslookup-ip() { nslookup "$@" | tail -n +3 | sed -n 's/Address:s*//p' }

            – zeroimpl
            Dec 6 '16 at 17:52
















          6












          6








          6







          This is a good usecase for awk.



          nslookup myip.opendns.com resolver1.opendns.com | awk -F': ' 'NR==6 { print $2 } '



          Here we are piping to awk, delimiting by ": " and then only outputting the second delimited field of line 6.






          share|improve this answer













          This is a good usecase for awk.



          nslookup myip.opendns.com resolver1.opendns.com | awk -F': ' 'NR==6 { print $2 } '



          Here we are piping to awk, delimiting by ": " and then only outputting the second delimited field of line 6.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Sep 16 '14 at 20:02









          JNevillJNevill

          1,169512




          1,169512








          • 2





            OP hasn't specified but I'd bet he's on Windows, and there's no awk in Windows.

            – Ƭᴇcʜιᴇ007
            Sep 16 '14 at 20:03






          • 1





            @Techie007 But the OP has not specified he is on windows either, so we may as well provide complete information. The above answer works for all Nixes, which nicely complements other replies, including yours.

            – MariusMatutiae
            Sep 17 '14 at 10:11











          • Yes, on Windows servers.

            – BondUniverse
            Sep 18 '14 at 22:27






          • 1





            This doesn't work in some cases (the hard-coded line number can be wrong). I use this instead: nslookup-ip() { nslookup "$@" | tail -n +3 | sed -n 's/Address:s*//p' }

            – zeroimpl
            Dec 6 '16 at 17:52
















          • 2





            OP hasn't specified but I'd bet he's on Windows, and there's no awk in Windows.

            – Ƭᴇcʜιᴇ007
            Sep 16 '14 at 20:03






          • 1





            @Techie007 But the OP has not specified he is on windows either, so we may as well provide complete information. The above answer works for all Nixes, which nicely complements other replies, including yours.

            – MariusMatutiae
            Sep 17 '14 at 10:11











          • Yes, on Windows servers.

            – BondUniverse
            Sep 18 '14 at 22:27






          • 1





            This doesn't work in some cases (the hard-coded line number can be wrong). I use this instead: nslookup-ip() { nslookup "$@" | tail -n +3 | sed -n 's/Address:s*//p' }

            – zeroimpl
            Dec 6 '16 at 17:52










          2




          2





          OP hasn't specified but I'd bet he's on Windows, and there's no awk in Windows.

          – Ƭᴇcʜιᴇ007
          Sep 16 '14 at 20:03





          OP hasn't specified but I'd bet he's on Windows, and there's no awk in Windows.

          – Ƭᴇcʜιᴇ007
          Sep 16 '14 at 20:03




          1




          1





          @Techie007 But the OP has not specified he is on windows either, so we may as well provide complete information. The above answer works for all Nixes, which nicely complements other replies, including yours.

          – MariusMatutiae
          Sep 17 '14 at 10:11





          @Techie007 But the OP has not specified he is on windows either, so we may as well provide complete information. The above answer works for all Nixes, which nicely complements other replies, including yours.

          – MariusMatutiae
          Sep 17 '14 at 10:11













          Yes, on Windows servers.

          – BondUniverse
          Sep 18 '14 at 22:27





          Yes, on Windows servers.

          – BondUniverse
          Sep 18 '14 at 22:27




          1




          1





          This doesn't work in some cases (the hard-coded line number can be wrong). I use this instead: nslookup-ip() { nslookup "$@" | tail -n +3 | sed -n 's/Address:s*//p' }

          – zeroimpl
          Dec 6 '16 at 17:52







          This doesn't work in some cases (the hard-coded line number can be wrong). I use this instead: nslookup-ip() { nslookup "$@" | tail -n +3 | sed -n 's/Address:s*//p' }

          – zeroimpl
          Dec 6 '16 at 17:52













          3














          If you're on Windows, and have PowerShell installed (v1 or better) (and a .Net version) you could use a (long) one-liner like this:



          [System.Net.Dns]::GetHostAddresses("www.google.com")[0] | Select IPAddressToString -ExpandProperty IPAddressToString | Out-File c:folderfilename.txt



          This will lookup www.google.com and put the first returned IPv4 address into a file.



          If you're using PowerShell v3+ on Windows 8+ (or Server 2012+) you can user the use the Resolve-DnsName cmdlet instead of the .Net GetHostAddress call. ie:



          (Resolve-DnsName www.google.com)[0] | Select IPAddressToString -ExpandProperty IPAddressToString | Out-File c:folderfilename.txt



          Simply change www.google.com to your preferred domain name. Or put it in a PowerShell script and set it up to accept an argument (of the domain name you want to look up).



          More info on that: How to pass an argument to a PowerShell script?






          share|improve this answer






























            3














            If you're on Windows, and have PowerShell installed (v1 or better) (and a .Net version) you could use a (long) one-liner like this:



            [System.Net.Dns]::GetHostAddresses("www.google.com")[0] | Select IPAddressToString -ExpandProperty IPAddressToString | Out-File c:folderfilename.txt



            This will lookup www.google.com and put the first returned IPv4 address into a file.



            If you're using PowerShell v3+ on Windows 8+ (or Server 2012+) you can user the use the Resolve-DnsName cmdlet instead of the .Net GetHostAddress call. ie:



            (Resolve-DnsName www.google.com)[0] | Select IPAddressToString -ExpandProperty IPAddressToString | Out-File c:folderfilename.txt



            Simply change www.google.com to your preferred domain name. Or put it in a PowerShell script and set it up to accept an argument (of the domain name you want to look up).



            More info on that: How to pass an argument to a PowerShell script?






            share|improve this answer




























              3












              3








              3







              If you're on Windows, and have PowerShell installed (v1 or better) (and a .Net version) you could use a (long) one-liner like this:



              [System.Net.Dns]::GetHostAddresses("www.google.com")[0] | Select IPAddressToString -ExpandProperty IPAddressToString | Out-File c:folderfilename.txt



              This will lookup www.google.com and put the first returned IPv4 address into a file.



              If you're using PowerShell v3+ on Windows 8+ (or Server 2012+) you can user the use the Resolve-DnsName cmdlet instead of the .Net GetHostAddress call. ie:



              (Resolve-DnsName www.google.com)[0] | Select IPAddressToString -ExpandProperty IPAddressToString | Out-File c:folderfilename.txt



              Simply change www.google.com to your preferred domain name. Or put it in a PowerShell script and set it up to accept an argument (of the domain name you want to look up).



              More info on that: How to pass an argument to a PowerShell script?






              share|improve this answer















              If you're on Windows, and have PowerShell installed (v1 or better) (and a .Net version) you could use a (long) one-liner like this:



              [System.Net.Dns]::GetHostAddresses("www.google.com")[0] | Select IPAddressToString -ExpandProperty IPAddressToString | Out-File c:folderfilename.txt



              This will lookup www.google.com and put the first returned IPv4 address into a file.



              If you're using PowerShell v3+ on Windows 8+ (or Server 2012+) you can user the use the Resolve-DnsName cmdlet instead of the .Net GetHostAddress call. ie:



              (Resolve-DnsName www.google.com)[0] | Select IPAddressToString -ExpandProperty IPAddressToString | Out-File c:folderfilename.txt



              Simply change www.google.com to your preferred domain name. Or put it in a PowerShell script and set it up to accept an argument (of the domain name you want to look up).



              More info on that: How to pass an argument to a PowerShell script?







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited May 23 '17 at 12:41









              Community

              1




              1










              answered Sep 16 '14 at 20:11









              Ƭᴇcʜιᴇ007Ƭᴇcʜιᴇ007

              98.9k14156212




              98.9k14156212























                  1














                  Works good for me on my Linux machine. I've never tried it on other systems though but Google has a lot of articles on how to install dig for example on Windows



                  The only thing to note, for local hostnames search domain should be added explicitly.
                  So if you have myhost host in your local network with search domain mynetwork put



                  dig +short myhost.mynetwork


                  on command line.



                  Examples:



                  sergeyi@sergeyi:~$ dig +short google.ru
                  173.194.222.94

                  sergeyi@sergeyi:~$ dig A +short google.ru
                  173.194.222.94

                  sergeyi@sergeyi:~$ dig AAAA +short google.ru
                  2a00:1450:4010:c0b::5e





                  share|improve this answer






























                    1














                    Works good for me on my Linux machine. I've never tried it on other systems though but Google has a lot of articles on how to install dig for example on Windows



                    The only thing to note, for local hostnames search domain should be added explicitly.
                    So if you have myhost host in your local network with search domain mynetwork put



                    dig +short myhost.mynetwork


                    on command line.



                    Examples:



                    sergeyi@sergeyi:~$ dig +short google.ru
                    173.194.222.94

                    sergeyi@sergeyi:~$ dig A +short google.ru
                    173.194.222.94

                    sergeyi@sergeyi:~$ dig AAAA +short google.ru
                    2a00:1450:4010:c0b::5e





                    share|improve this answer




























                      1












                      1








                      1







                      Works good for me on my Linux machine. I've never tried it on other systems though but Google has a lot of articles on how to install dig for example on Windows



                      The only thing to note, for local hostnames search domain should be added explicitly.
                      So if you have myhost host in your local network with search domain mynetwork put



                      dig +short myhost.mynetwork


                      on command line.



                      Examples:



                      sergeyi@sergeyi:~$ dig +short google.ru
                      173.194.222.94

                      sergeyi@sergeyi:~$ dig A +short google.ru
                      173.194.222.94

                      sergeyi@sergeyi:~$ dig AAAA +short google.ru
                      2a00:1450:4010:c0b::5e





                      share|improve this answer















                      Works good for me on my Linux machine. I've never tried it on other systems though but Google has a lot of articles on how to install dig for example on Windows



                      The only thing to note, for local hostnames search domain should be added explicitly.
                      So if you have myhost host in your local network with search domain mynetwork put



                      dig +short myhost.mynetwork


                      on command line.



                      Examples:



                      sergeyi@sergeyi:~$ dig +short google.ru
                      173.194.222.94

                      sergeyi@sergeyi:~$ dig A +short google.ru
                      173.194.222.94

                      sergeyi@sergeyi:~$ dig AAAA +short google.ru
                      2a00:1450:4010:c0b::5e






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Sep 7 '17 at 11:58









                      mtak

                      11k23253




                      11k23253










                      answered Sep 7 '17 at 8:42









                      SergeySergey

                      112




                      112























                          0














                          If your goal is to retrieve your external IP with a script, a possible would be the use of a very simple PowerShell function :



                          function Get-ExternalIP {
                          (Invoke-WebRequest ifconfig.me/ip).Content
                          }


                          Running this function will return your external IP, and no other useless information.



                          Source and examples : http://jfrmilner.wordpress.com/2012/12/22/powershell-quick-tip-03-whats-my-external-ip-address-windows-command-line/



                          Alternatively, if you want to be able to get the IP resolution for other hosts, you should have a look at the Resolve-DnsName cmdlet. Unfortunatelly, your computer must be running Windows 8.1 or Windows Server 2012 R2 in order to use this cmdlet. Here is an example I just performed :



                          enter image description here



                          Hope this helps !






                          share|improve this answer


























                          • I need the command to work reliably on any Windows server 2003+. I agree though that if Power shell was viable, I would prefer your solution.

                            – BondUniverse
                            Sep 18 '14 at 22:29
















                          0














                          If your goal is to retrieve your external IP with a script, a possible would be the use of a very simple PowerShell function :



                          function Get-ExternalIP {
                          (Invoke-WebRequest ifconfig.me/ip).Content
                          }


                          Running this function will return your external IP, and no other useless information.



                          Source and examples : http://jfrmilner.wordpress.com/2012/12/22/powershell-quick-tip-03-whats-my-external-ip-address-windows-command-line/



                          Alternatively, if you want to be able to get the IP resolution for other hosts, you should have a look at the Resolve-DnsName cmdlet. Unfortunatelly, your computer must be running Windows 8.1 or Windows Server 2012 R2 in order to use this cmdlet. Here is an example I just performed :



                          enter image description here



                          Hope this helps !






                          share|improve this answer


























                          • I need the command to work reliably on any Windows server 2003+. I agree though that if Power shell was viable, I would prefer your solution.

                            – BondUniverse
                            Sep 18 '14 at 22:29














                          0












                          0








                          0







                          If your goal is to retrieve your external IP with a script, a possible would be the use of a very simple PowerShell function :



                          function Get-ExternalIP {
                          (Invoke-WebRequest ifconfig.me/ip).Content
                          }


                          Running this function will return your external IP, and no other useless information.



                          Source and examples : http://jfrmilner.wordpress.com/2012/12/22/powershell-quick-tip-03-whats-my-external-ip-address-windows-command-line/



                          Alternatively, if you want to be able to get the IP resolution for other hosts, you should have a look at the Resolve-DnsName cmdlet. Unfortunatelly, your computer must be running Windows 8.1 or Windows Server 2012 R2 in order to use this cmdlet. Here is an example I just performed :



                          enter image description here



                          Hope this helps !






                          share|improve this answer















                          If your goal is to retrieve your external IP with a script, a possible would be the use of a very simple PowerShell function :



                          function Get-ExternalIP {
                          (Invoke-WebRequest ifconfig.me/ip).Content
                          }


                          Running this function will return your external IP, and no other useless information.



                          Source and examples : http://jfrmilner.wordpress.com/2012/12/22/powershell-quick-tip-03-whats-my-external-ip-address-windows-command-line/



                          Alternatively, if you want to be able to get the IP resolution for other hosts, you should have a look at the Resolve-DnsName cmdlet. Unfortunatelly, your computer must be running Windows 8.1 or Windows Server 2012 R2 in order to use this cmdlet. Here is an example I just performed :



                          enter image description here



                          Hope this helps !







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Sep 16 '14 at 20:12

























                          answered Sep 16 '14 at 20:06









                          Ob1lanOb1lan

                          1,43131230




                          1,43131230













                          • I need the command to work reliably on any Windows server 2003+. I agree though that if Power shell was viable, I would prefer your solution.

                            – BondUniverse
                            Sep 18 '14 at 22:29



















                          • I need the command to work reliably on any Windows server 2003+. I agree though that if Power shell was viable, I would prefer your solution.

                            – BondUniverse
                            Sep 18 '14 at 22:29

















                          I need the command to work reliably on any Windows server 2003+. I agree though that if Power shell was viable, I would prefer your solution.

                          – BondUniverse
                          Sep 18 '14 at 22:29





                          I need the command to work reliably on any Windows server 2003+. I agree though that if Power shell was viable, I would prefer your solution.

                          – BondUniverse
                          Sep 18 '14 at 22:29











                          0














                          Simple.



                          In nslookup use:



                          Set type=A



                          Then lookup. The set command will show only outputs for A records. You can use this for MX, CNAME, AAA etc.






                          share|improve this answer




























                            0














                            Simple.



                            In nslookup use:



                            Set type=A



                            Then lookup. The set command will show only outputs for A records. You can use this for MX, CNAME, AAA etc.






                            share|improve this answer


























                              0












                              0








                              0







                              Simple.



                              In nslookup use:



                              Set type=A



                              Then lookup. The set command will show only outputs for A records. You can use this for MX, CNAME, AAA etc.






                              share|improve this answer













                              Simple.



                              In nslookup use:



                              Set type=A



                              Then lookup. The set command will show only outputs for A records. You can use this for MX, CNAME, AAA etc.







                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Sep 16 '14 at 21:31









                              ProxyProxy

                              16614




                              16614























                                  -1














                                  Not ideal, but I sometimes use this method:



                                  ping -c 1 myip.opendns.com | grep -ohE "(([0-9]+.[0-9]+.[0-9]+.[0-9]+))" | head -1 | sed "s/[()]//g"






                                  share|improve this answer
























                                  • Why exactly is this solution not ideal? I know the answer but I normally provide an attempt to provide clarity to unclear answers before I vote on them.

                                    – Ramhound
                                    Oct 14 '15 at 12:18











                                  • Not everybody has grep and sed` installed on their Windows PCs.

                                    – DavidPostill
                                    Oct 14 '15 at 15:29











                                  • and ping syntax is wrong.

                                    – Francisco Tapia
                                    Oct 15 '15 at 15:34











                                  • What's wrong with the syntax? I just wanted to give another method that I use and I thought that someone may find that useful as well. It works for me. You don't have to use it.

                                    – AlonL
                                    Oct 16 '15 at 15:54
















                                  -1














                                  Not ideal, but I sometimes use this method:



                                  ping -c 1 myip.opendns.com | grep -ohE "(([0-9]+.[0-9]+.[0-9]+.[0-9]+))" | head -1 | sed "s/[()]//g"






                                  share|improve this answer
























                                  • Why exactly is this solution not ideal? I know the answer but I normally provide an attempt to provide clarity to unclear answers before I vote on them.

                                    – Ramhound
                                    Oct 14 '15 at 12:18











                                  • Not everybody has grep and sed` installed on their Windows PCs.

                                    – DavidPostill
                                    Oct 14 '15 at 15:29











                                  • and ping syntax is wrong.

                                    – Francisco Tapia
                                    Oct 15 '15 at 15:34











                                  • What's wrong with the syntax? I just wanted to give another method that I use and I thought that someone may find that useful as well. It works for me. You don't have to use it.

                                    – AlonL
                                    Oct 16 '15 at 15:54














                                  -1












                                  -1








                                  -1







                                  Not ideal, but I sometimes use this method:



                                  ping -c 1 myip.opendns.com | grep -ohE "(([0-9]+.[0-9]+.[0-9]+.[0-9]+))" | head -1 | sed "s/[()]//g"






                                  share|improve this answer













                                  Not ideal, but I sometimes use this method:



                                  ping -c 1 myip.opendns.com | grep -ohE "(([0-9]+.[0-9]+.[0-9]+.[0-9]+))" | head -1 | sed "s/[()]//g"







                                  share|improve this answer












                                  share|improve this answer



                                  share|improve this answer










                                  answered Oct 14 '15 at 7:04









                                  AlonLAlonL

                                  1071




                                  1071













                                  • Why exactly is this solution not ideal? I know the answer but I normally provide an attempt to provide clarity to unclear answers before I vote on them.

                                    – Ramhound
                                    Oct 14 '15 at 12:18











                                  • Not everybody has grep and sed` installed on their Windows PCs.

                                    – DavidPostill
                                    Oct 14 '15 at 15:29











                                  • and ping syntax is wrong.

                                    – Francisco Tapia
                                    Oct 15 '15 at 15:34











                                  • What's wrong with the syntax? I just wanted to give another method that I use and I thought that someone may find that useful as well. It works for me. You don't have to use it.

                                    – AlonL
                                    Oct 16 '15 at 15:54



















                                  • Why exactly is this solution not ideal? I know the answer but I normally provide an attempt to provide clarity to unclear answers before I vote on them.

                                    – Ramhound
                                    Oct 14 '15 at 12:18











                                  • Not everybody has grep and sed` installed on their Windows PCs.

                                    – DavidPostill
                                    Oct 14 '15 at 15:29











                                  • and ping syntax is wrong.

                                    – Francisco Tapia
                                    Oct 15 '15 at 15:34











                                  • What's wrong with the syntax? I just wanted to give another method that I use and I thought that someone may find that useful as well. It works for me. You don't have to use it.

                                    – AlonL
                                    Oct 16 '15 at 15:54

















                                  Why exactly is this solution not ideal? I know the answer but I normally provide an attempt to provide clarity to unclear answers before I vote on them.

                                  – Ramhound
                                  Oct 14 '15 at 12:18





                                  Why exactly is this solution not ideal? I know the answer but I normally provide an attempt to provide clarity to unclear answers before I vote on them.

                                  – Ramhound
                                  Oct 14 '15 at 12:18













                                  Not everybody has grep and sed` installed on their Windows PCs.

                                  – DavidPostill
                                  Oct 14 '15 at 15:29





                                  Not everybody has grep and sed` installed on their Windows PCs.

                                  – DavidPostill
                                  Oct 14 '15 at 15:29













                                  and ping syntax is wrong.

                                  – Francisco Tapia
                                  Oct 15 '15 at 15:34





                                  and ping syntax is wrong.

                                  – Francisco Tapia
                                  Oct 15 '15 at 15:34













                                  What's wrong with the syntax? I just wanted to give another method that I use and I thought that someone may find that useful as well. It works for me. You don't have to use it.

                                  – AlonL
                                  Oct 16 '15 at 15:54





                                  What's wrong with the syntax? I just wanted to give another method that I use and I thought that someone may find that useful as well. It works for me. You don't have to use it.

                                  – AlonL
                                  Oct 16 '15 at 15:54











                                  -1














                                  Using PowerShell, you can run below:



                                  (((nslookup myip.opendns.com resolver1.opendns.com 2>null| select-string -pattern "Address:") -split ":")[3]).Trim()





                                  share|improve this answer




























                                    -1














                                    Using PowerShell, you can run below:



                                    (((nslookup myip.opendns.com resolver1.opendns.com 2>null| select-string -pattern "Address:") -split ":")[3]).Trim()





                                    share|improve this answer


























                                      -1












                                      -1








                                      -1







                                      Using PowerShell, you can run below:



                                      (((nslookup myip.opendns.com resolver1.opendns.com 2>null| select-string -pattern "Address:") -split ":")[3]).Trim()





                                      share|improve this answer













                                      Using PowerShell, you can run below:



                                      (((nslookup myip.opendns.com resolver1.opendns.com 2>null| select-string -pattern "Address:") -split ":")[3]).Trim()






                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Dec 31 '18 at 13:29









                                      Ajaz AhmedAjaz Ahmed

                                      1




                                      1






























                                          draft saved

                                          draft discarded




















































                                          Thanks for contributing an answer to Super User!


                                          • Please be sure to answer the question. Provide details and share your research!

                                          But avoid



                                          • Asking for help, clarification, or responding to other answers.

                                          • Making statements based on opinion; back them up with references or personal experience.


                                          To learn more, see our tips on writing great answers.




                                          draft saved


                                          draft discarded














                                          StackExchange.ready(
                                          function () {
                                          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f812664%2fnslookup-command-line-with-a-record-ip-as-sole-output%23new-answer', 'question_page');
                                          }
                                          );

                                          Post as a guest















                                          Required, but never shown





















































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown

































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown







                                          Popular posts from this blog

                                          How do I know what Microsoft account the skydrive app is syncing to?

                                          When does type information flow backwards in C++?

                                          Grease: Live!