29

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
  • 823
  • 5
  • 14
  • 27

12 Answers12

54

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,121
  • 5
  • 32
  • 35
brm
  • 549
  • 4
  • 2
  • 1
    Welcome to Super User! Please *explain* why this is a correct answer; not all of us know how to use `dig`. – Glorfindel Jul 03 '17 at 14:38
  • And it will help other users with the same problem – yass Jul 03 '17 at 15:26
  • 2
    This solution is preferable over others using `awk` and/or `sed` since piping is often problematic, and it this case, unnecessarily complex. – StockB May 09 '18 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 '18 at 09:04
  • 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 '18 at 09:35
  • Add the egrep to extract the IP: dig +short my.db.host.fqdn | egrep '[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+' – Richard Jan 05 '22 at 17:50
  • This is exactly what I needed! I was searching for "NSLOOKUP", but DIG does it so much better! Thanks! – OutOfThisPlanet Jan 02 '23 at 23:20
8

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

DavidPostill
  • 153,128
  • 77
  • 353
  • 394
7

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,214
  • 6
  • 12
  • 2
    OP hasn't specified but I'd bet he's on Windows, and there's no `awk` in Windows. – Ƭᴇcʜιᴇ007 Sep 16 '14 at 20:03
  • 3
    @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. – MariusMatutiae Sep 17 '14 at 10:11
  • Yes, on Windows servers. – BondUniverse Sep 18 '14 at 22:27
  • 4
    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 06 '16 at 17:52
5

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?

Ƭᴇcʜιᴇ007
  • 111,883
  • 19
  • 201
  • 268
4

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
  • 16,513
  • 2
  • 52
  • 64
Sergey
  • 141
  • 2
  • 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 8.8.8.8` for example. – Steven the Easily Amused Mar 02 '23 at 00:10
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
  • (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](https://superuser.com/q/812664/150988#812667).  … (Cont’d) – Scott - Слава Україні Jun 10 '20 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)? – Scott - Слава Україні Jun 10 '20 at 19:48
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,906
  • 3
  • 18
  • 35
  • 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. – BondUniverse Sep 18 '14 at 22:29
-1

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. – Steven the Easily Amused Mar 02 '23 at 00:18
-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
  • 5
-1

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
  • 179
  • 1
  • 4
-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
  • 1