34

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"

BondUniverse
  • 883
  • 5
  • 18
  • 27

12 Answers12

57

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
tripleee
  • 3,308
  • 5
  • 36
  • 35
brm
  • 581
10

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

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

mwfearnley
  • 7,889
DavidPostill
  • 162,382
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.

JNevill
  • 1,241
  • 6
  • 12
6

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?

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
mtak
  • 17,262
Sergey
  • 151
1

Using PowerShell, you can run below:

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

To Print only the IP resolved,

nslookup google.com | grep "Address" | awk '{print $2}' | sed -n 2p
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 !

Ob1lan
  • 1,936
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.

Proxy
  • 187
-1

Use host command it is also to the same job

host hostname|awk '{print $NF}'
mwfearnley
  • 7,889
-1

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

tbrodbeck
  • 111
-2

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"

AlonL
  • 105