Nslookup command line with A record IP as sole output

71,443

Solution 1

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

Solution 2

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

Solution 3

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.

Solution 4

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:\folder\filename.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:\folder\filename.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?

Solution 5

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:
71,443

Related videos on Youtube

BondUniverse
Author by

BondUniverse

Updated on September 18, 2022

Comments

  • BondUniverse
    BondUniverse almost 2 years

    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"

    • Ƭᴇcʜιᴇ007
      Ƭᴇcʜιᴇ007 almost 10 years
      How do you expect to deal with names that return more than one IP address? Which OS are you using?
  • Ƭᴇcʜιᴇ007
    Ƭᴇcʜιᴇ007 almost 10 years
    OP hasn't specified but I'd bet he's on Windows, and there's no awk in Windows.
  • MariusMatutiae
    MariusMatutiae almost 10 years
    @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.
  • BondUniverse
    BondUniverse almost 10 years
    Yes, on Windows servers.
  • BondUniverse
    BondUniverse almost 10 years
    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.
  • Ramhound
    Ramhound over 8 years
    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.
  • DavidPostill
    DavidPostill over 8 years
    Not everybody has grep and sed` installed on their Windows PCs.
  • Francisco  Tapia
    Francisco Tapia over 8 years
    and ping syntax is wrong.
  • AlonL
    AlonL over 8 years
    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.
  • zeroimpl
    zeroimpl over 7 years
    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' }
  • Glorfindel
    Glorfindel about 7 years
    Welcome to Super User! Please explain why this is a correct answer; not all of us know how to use dig.
  • yass
    yass about 7 years
    And it will help other users with the same problem
  • StockB
    StockB about 6 years
    This solution is preferable over others using awk and/or sed since piping is often problematic, and it this case, unnecessarily complex.
  • Jaro
    Jaro over 5 years
    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
  • RCross
    RCross over 5 years
    "Assuming you are using Windows"... I'm not sure that should ever be an assumption here.
  • RCross
    RCross over 5 years
    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.
  • Scott - Слава Україні
    Scott - Слава Україні about 4 years
    (1) awk and sed are very powerful commands; you hardly ever need to use them together, or with grep.  The last three components of your pipeline can be reduced to two: awk '/Address/ {print $2}' | sed -n 2p or grep "Address" | awk 'NR==2 {print $2}', or even one: awk '/Address/ {if (++count==2) print $2}' or awk '/Address/ && NR==6 {print $2}'.  (2) Note that JNevill already gave an awk … 'NR==6 { print $2 }' answer.  … (Cont’d)
  • Scott - Слава Україні
    Scott - Слава Україні about 4 years
    (Cont’d) … (3) nslookup can output multiple matches, and the OP never specified whether they wanted all or only the first one.  How would you report all matches (i.e., lines 6 and beyond)?
  • Sarita
    Sarita almost 4 years
    This actually works very well for me for a >different purpose<.
  • Richard
    Richard over 2 years
    Add the egrep to extract the IP: dig +short my.db.host.fqdn | egrep '[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+'