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

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

I want it to just output:

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"

  • 2
    How do you expect to deal with names that return more than one IP address? Which OS are you using? Sep 16, 2014 at 19:59

12 Answers 12


Nslookup with A record IP as sole output

If you are using Windows, this can be done using a one line command in CMD.

From the Command Prompt directly:

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

For a batch file, double up the % symbols:

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


  • 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

  • 3
    "Assuming you are using Windows"... I'm not sure that should ever be an assumption here.
    – RCross
    Dec 19, 2018 at 9:29

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
  • 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, 2017 at 14:38
  • And it will help other users with the same problem
    – yass
    Jul 3, 2017 at 15:26
  • 3
    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, 2018 at 13:55
  • 3
    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, 2018 at 9:04
  • 1
    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, 2018 at 9:35

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.

  • 2
    OP hasn't specified but I'd bet he's on Windows, and there's no awk in Windows. Sep 16, 2014 at 20:03
  • 4
    @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. Sep 17, 2014 at 10:11
  • Yes, on Windows servers. Sep 18, 2014 at 22:27
  • 6
    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, 2016 at 17:52

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?


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.


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

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

sergeyi@sergeyi:~$ dig AAAA +short google.ru
  • dig may return multiple lines, not just one. E.g. for me in the US, the last example (AAAA) returns 4 lines. Also sometimes, and for some lookups the first example may return a CNAME and an address. dig +short identitybts.webex.com for example. Mar 2, 2023 at 0:10

Using PowerShell, you can run below:

(((nslookup myip.opendns.com resolver1.opendns.com 2>null| select-string -pattern "Address:") -split ":")[3]).Trim()
  • This actually works very well for me for a >different purpose<.
    – grep65535
    Aug 19, 2020 at 16:35

To Print only the IP resolved,

nslookup google.com | grep "Address" | awk '{print $2}' | sed -n 2p
  • 1
    (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) Jun 10, 2020 at 19:48
  • (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)? Jun 10, 2020 at 19:48

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 !

  • 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. Sep 18, 2014 at 22:29


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.


Probably the most precise on a Unix-like system:

nslookup example.com | awk 'NR==6 {print $2}'

Explanation: This method just prints the second string at line 6.

Credit to @Scott helping me coming up with this


Use host command it is also to the same job

host hostname|awk '{print $NF}'
  • This is a bit more elegant than others, but host HOSTNAME can return multiple lines including "hostxyz.example.com is an alias for HOSTNAME" As given, try it with 'github.com' and you'll see it lists one address and 5 dns names (which are mail), or use superuser.com as the host and you may see 4 or more addresses returned. Mar 2, 2023 at 0:18

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"

  • 1
    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, 2015 at 12:18
  • Not everybody has grep and sed` installed on their Windows PCs.
    – DavidPostill
    Oct 14, 2015 at 15:29
  • and ping syntax is wrong. Oct 15, 2015 at 15:34
  • 1
    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, 2015 at 15:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .