Nslookup command line with A record IP as sole output
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
orawk
.
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.
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
Related videos on Youtube
BondUniverse
Updated on September 18, 2022Comments
-
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 almost 10 yearsHow do you expect to deal with names that return more than one IP address? Which OS are you using?
-
-
Ƭᴇcʜιᴇ007 almost 10 yearsOP hasn't specified but I'd bet he's on Windows, and there's no
awk
in Windows. -
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 almost 10 yearsYes, on Windows servers.
-
BondUniverse almost 10 yearsI 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 over 8 yearsWhy 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 over 8 yearsNot everybody has
grep
and sed` installed on their Windows PCs. -
Francisco Tapia over 8 yearsand ping syntax is wrong.
-
AlonL over 8 yearsWhat'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 over 7 yearsThis 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 about 7 yearsWelcome to Super User! Please explain why this is a correct answer; not all of us know how to use
dig
. -
yass about 7 yearsAnd it will help other users with the same problem
-
StockB about 6 yearsThis solution is preferable over others using
awk
and/orsed
since piping is often problematic, and it this case, unnecessarily complex. -
Jaro over 5 yearsso 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 over 5 years"Assuming you are using Windows"... I'm not sure that should ever be an assumption here.
-
RCross over 5 yearsThis 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 - Слава Україні about 4 years(1)
awk
andsed
are very powerful commands; you hardly ever need to use them together, or withgrep
. The last three components of your pipeline can be reduced to two:awk '/Address/ {print $2}' | sed -n 2p
orgrep "Address" | awk 'NR==2 {print $2}'
, or even one:awk '/Address/ {if (++count==2) print $2}'
orawk '/Address/ && NR==6 {print $2}'
. (2) Note that JNevill already gave anawk … 'NR==6 { print $2 }'
answer. … (Cont’d) -
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 almost 4 yearsThis actually works very well for me for a >different purpose<.
-
Richard over 2 yearsAdd the egrep to extract the IP: dig +short my.db.host.fqdn | egrep '[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+'