Windows command that returns external IP

61

37

Is there a command by which I can find my external IP of my router or my NAT\DSL Router, etc., eliminating the need to visit whatsmyip.net or similar.

Junaid Saeed

Posted 2010-07-21T06:06:13.163

Reputation: 964

Linux: http://askubuntu.com/questions/95910/command-for-determining-my-public-ip/712144#712144

– Ciro Santilli 新疆改造中心法轮功六四事件 – 2015-12-21T22:06:14.030

Answers

16

There is no built-in command to do this. Part of the problem is that when you are connected to the internet through a router, your network hardware is not directly connected to the internet, so your system isn't specifically assigned an IP. It's possible you might even have multiple external IPs in some cases if you are behind a reverse proxy, as many corporate networks are set up. Your best bet might be to create a script which queries whatismyip.org, or trying to find if one already exists.

(As a tip, whatismyip.org is preferable to most other solutions, since it just returns your IP as plain text - no superfluous text, links, images or other garbage. It would be much easier to use in a custom script than most of the other IP-detection sites.)

nhinkle

Posted 2010-07-21T06:06:13.163

Reputation: 35 057

Working native command from below https://superuser.com/a/722397/216374

– PsychoData – 2019-01-03T19:54:04.757

6They changed whatismyip.org, unfortunately; the IP address is now returned as an image, making it pretty much useless for scripting. The website "icanhazip.com" mentioned below does work. – onnodb – 2012-11-29T13:53:19.360

Yandex is working, check the script posted below. – Filipe YaBa Polido – 2013-12-05T15:42:19.417

109

You could use a DNS request instead of HTTP request to find out your public IP:

C:\> nslookup myip.opendns.com. resolver1.opendns.com

It uses resolver1.opendns.com dns server to resolve the magical myip.opendns.com. hostname to your ip address. (Note: the trailing . on the lookup prevents search domains from being appended, which can yield incorrect results.)

Unix version:

$ dig +short myip.opendns.com @resolver1.opendns.com

jfs

Posted 2010-07-21T06:06:13.163

Reputation: 1 231

1Worked like a charm. – B.K. – 2014-06-29T04:15:28.367

brilliant, works without installing anything too. – ericosg – 2015-09-07T11:54:46.123

Didn't work for me anymore in November, 2017, on Windows 10. PowerShell answer from Bradley Forney works. – Jason – 2017-11-08T23:58:33.830

Worked for me still (Windows 10, Dec 2017). Very clever @jfs, do OpenDNS have this documented somewhere how did you discover it? – Alex KeySmith – 2017-12-21T13:48:44.313

@AlexKeySmith: I do not remember. – jfs – 2017-12-22T08:49:41.447

4This should be the accepted answer, since it provides an actual command, and it works. The accepted answer tells there is no command (which is proved false), and suggests using a script, without really giving further direction. – CyberClaw – 2018-03-23T12:41:40.800

works for me it returns correct external ip adress – Santropedro – 2018-11-29T22:52:43.763

33

grab your own copy of curlfrom http://curl.haxx.se/download.html and then just

curl "http://myexternalip.com/raw"

or use powershell:

$wc = new-object System.Net.WebClient
$wc.DownloadString("http://myexternalip.com/raw")

(disclaimer: http://myexternalip.com was created by me)

akira

Posted 2010-07-21T06:06:13.163

Reputation: 52 754

That only provides an IPv6 address. Is there a way to get the IPv4 address? – tim11g – 2018-12-01T19:35:40.973

I kept reading and found the IPv4 / IPv6 examples below for Powershell. It is possible to get IPv4 with CURL like this "C:>curl "http://myip.dnsomatic.com/"

– tim11g – 2018-12-01T19:39:29.743

7

This works nicely, I use it mostly with psexec when inspecting client computer connections.

nslookup myip.opendns.com resolver1.opendns.com

Antti Lagus

Posted 2010-07-21T06:06:13.163

Reputation: 71

6

Create a file named ip.vbs and copy the following into it:

Option Explicit
Dim http : Set http = CreateObject( "MSXML2.ServerXmlHttp" )
http.Open "GET", "http://icanhazip.com", False
http.Send
Wscript.Echo http.responseText   'or do whatever you want with it
Set http = Nothing

Execute using

C:\>cscript ip.vbs

As nhinkle noted, it's best to choose a site that only returns the IP and not HTML + ads, etc. like:

(source: formerly http://externip.com/about)

hyperslug

Posted 2010-07-21T06:06:13.163

Reputation: 12 882

5

I made this batch script to do that a few months ago:

@echo off

:: WhatIsMyIP.cmd - returns public IP address
:: requires: wget.exe

if [%1]==[-h] goto :HELP
if [%1]==[--help] goto :HELP
if [%1]==[/?] goto :HELP

wget -q -O %temp%\MyIP http://www.whatismyip.com/automation/n09230945.asp
for /f "delims= " %%G in (%temp%\myip) do set PublicIP=%%G & del %temp%\MyIP
echo. & echo Your public IP address is %PublicIP% & echo.
if [%1]==[--clip] echo %PublicIP% | clip
goto :EOF

:HELP
echo. & echo Usage: whatismyip [--clip] & echo.
goto :EOF

:EOF

It gives you the option to put the IP address in the clipboard and it sets an environmental variable - %PublicIP%.


SIMPLER METHOD:

Now, I just do this instead:

curl icanhazip.com

or...

curl icanhazip.com | clip

...to get the current public IP address into the clipboard.

You need cURL.

paradroid

Posted 2010-07-21T06:06:13.163

Reputation: 20 970

1

The script from paradroid worked fine but whatsmyip moved the web page to http://automation.whatismyip.com/n09230945.asp If you change to this, it works perfect.

– None – 2012-04-19T06:52:57.977

5

With Powershell 3.0 (Windows 7 default is 2.0) you can use Invoke-WebRequest

For IPv4:

$tmp =Invoke-WebRequest -URI http://myip.dnsomatic.com/
$tmp.Content

For IPv6:

$tmp =Invoke-WebRequest -URI http://myexternalip.com/raw
$tmp.Content

This will give you a variable to work with if you have something specific you want to do with it. I'm actually using this to build a script to upload my router's dynamic IP periodically, have another machine test communication to it at regular intervals, and then update DNS with the latest IP if it has changed so I can access my gear from anywhere by using a name instead of having to constantly chase down the IP.

And of course - as the sites change or their outputs change you'll want to update this accordingly. :)

Bradley Forney

Posted 2010-07-21T06:06:13.163

Reputation: 651

This approach worked great for me on windows 10. I needed a one-line command, so ran the above in quotes after powershell.exe -noprofile -command "". Separate the line-break with a semicolon... – Jason – 2017-11-08T23:57:27.253

1Oneliner? Just run (Invoke-WebRequest -URI http://myip.dnsomatic.com).Content – merosss – 2018-04-11T21:18:56.803

3

Try this:
Doesn't need any kind of external software installed.

@set @script=0 /*
  @echo off
    set @script=
    cscript //nologo //e:jscript "%~dpnx0"
  exit /b
*/

with (new ActiveXObject('Microsoft.XMLHTTP')) {
   open('GET', 'http://internet.yandex.ru/', false);
   send();

   WScript.echo(responseText.match(/IPv4:\s(\d+\.){3}\d+/g));
}

Filipe YaBa Polido

Posted 2010-07-21T06:06:13.163

Reputation: 149

3

Without third party programs is hard on Windows as Telnet isn't supplied by default, but, if it is there (XP) or turned on (Windows Vista and above), simply type:

telnet curlmyip.com 80

the screen will flash, and you will just get a cursor... Next type:

GET

In capital letters... you will then see the headers, followed by your ip (and sorry I blurred it, just showing where it would be!):

enter image description here

Other answers here obviously work, but, I'm trying to keep to the question on something that can be used on any windows, without third party programs!

For Windows Vista and above, you can install telnet easily (and safely) through Programs and features, or use the following command:

pkgmgr /iu:”TelnetClient”

William Hilsum

Posted 2010-07-21T06:06:13.163

Reputation: 111 572

Not working with curlmyip.com, but tested working with checkip.dyndns.org. – Sopalajo de Arrierez – 2014-12-07T13:04:35.597

That (and most of the answers!) does assume that the source website picks up your ip address accurately - some types of proxies mess this up. I was scraping my ip address off my router cause of this. Interestingly, in these cases, a website that uses a non standard port may work fine. – Journeyman Geek – 2014-01-15T01:21:42.497

2

You can use ifcfg.me. it supports some internal commands that come with windows by default.

curl ifcfg.me
nslookup . ifcfg.me
telnet ifcfg.me
ftp ifcfg.me
finger @ifcfg.me

Eun

Posted 2010-07-21T06:06:13.163

Reputation: 1 723

1

Here is what I figured out, it's possible to use in-built command to get public IP address but you do have access to internet

telnet icanhazip.com

or

telnet myip.gelma.net

Sam

Posted 2010-07-21T06:06:13.163

Reputation: 21

Good catch! Although icanhazip.com won’t work for me and the other one doesn’t do IPv6 properly. :) – Daniel B – 2015-03-22T15:32:27.467

1

I wanted to have a solution which works for Windows without installing third party tools. So I took the powershell option, which should run a script every hour.

You create this script with Notepad

$wc = new-object System.Net.WebClient
$ip = $wc.DownloadString("http://myexternalip.com/raw")
$LogTime = Get-Date -Format "MM-dd-yyyy_hh-mm-ss"
Add-Content C:\Users\Administrator\Desktop\logs\IPLog.txt $LogTime
Add-Content C:\Users\Administrator\Desktop\logs\IPLog.txt "`n"
Add-Content C:\Users\Administrator\Desktop\logs\IPLog.txt $ip
Add-Content C:\Users\Administrator\Desktop\logs\IPLog.txt "`n"

and save it as getIP.ps1 somewhere. You can call this script either directly in powershell or use the task planer as I did. This is the command for executing:

powershell -executionpolicy bypass -File C:\Users\Administrator\Desktop\getIP.ps1

In the task scheduler you create a new task. At Trigger you use the setting daily for every 1 day and under advanced option you choose every hour for the duration of immediately. Also check Run task as soon as possible after a scheduled start is missed in the Settings. Under Action you paste the above execution code.

The powershell code can be optimized, because I'm no powershell guru (my first time with it). Now I'm testing if the script is called every hour.

testing

Posted 2010-07-21T06:06:13.163

Reputation: 641

0

ipchicken.com will return your apparent IP address in text format if you can parse the returning HTML you extract your router's external IP. Alternately you can install one of those dynamic DNS agents that updates your apparent external IP to an FQDN and then just do nslookup on your FQDN to see your current IP number. There used to be free ones like DynDNS but most of them now require a paid subscription account.

anthonymaw

Posted 2010-07-21T06:06:13.163

Reputation: 67