I've found the following command to get your current public IP that works well from command line:
nslookup myip.opendns.com resolver1.opendns.comI 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.123I 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"
112 Answers
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.txtFrom a batch file:
for /f "skip=4 usebackq tokens=2" %%a in (`nslookup myip.opendns.com resolver1.opendns.com`) do echo %%a > ip.txtNotes:
- The public IP address is stored in a file (
ip.txt). - The above does not require non standard windows commands like
PowerShell,.Netorawk.
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.
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 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.
4If 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("")[0] | Select IPAddressToString -ExpandProperty IPAddressToString | Out-File c:\folder\filename.txt
This will lookup 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 )[0] | Select IPAddressToString -ExpandProperty IPAddressToString | Out-File c:\folder\filename.txt
Simply change 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.mynetworkon 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 To Print only the IP resolved,
nslookup google.com | grep "Address" | awk '{print $2}' | sed -n 2p 2 Using PowerShell, you can run below:
(((nslookup myip.opendns.com resolver1.opendns.com 2>null| select-string -pattern "Address:") -split ":")[3]).Trim() 1 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 ).Content
}Running this function will return your external IP, and no other useless information.
Source and examples :
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 :
Hope this helps !
1Simple.
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.
Use host command it is also to the same job host hostname|awk '{print $NF}'
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
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"