How to get my external IP address (over NAT) from the Windows command-line?

13

2

The Windows "ipconfig" command can only show me the parameters from the Ethernet interfaces from my machine (even with the ipconfig /all argument). It can show detailed information about the interface, but it will never show me my external IP address over a NAT network.

ipconfig/all -- Windows screenshot

However, there are several websites, such as "What is my IP address" that can get and show my external IP address. So I'm wondering, whether it is possible to get this value externally? Should I expect that there is some way to get this information from a command line at my local machine...

I need to get this value to log on an application that I'm doing with VBScript. Is there a way to do this, from a cmd on Windows?

Diogo

Posted 2012-03-26T14:48:36.460

Reputation: 28 202

Question was closed 2015-05-14T14:27:22.083

possible duplicate of Windows command that returns external IP; see also How can I get my public IP address from the command line, if I am behind a router?

– G-Man Says 'Reinstate Monica' – 2015-05-12T15:53:04.123

Note that HTTP based what-is-my-IP services may give you the IP of a proxy server. (And before anyone points out that HTTPS proxies exist, you can't transparently proxy HTTPS. If you've got one, you'll know it.) – billpg – 2012-03-26T20:04:17.563

Answers

11

Now, using the site Oliver provided, this can be done in VBScript.

Dim o
Set o = CreateObject("MSXML2.XMLHTTP")
o.open "GET", "http://ifconfig.me/ip", False
o.send
WScript.StdOut.Write o.responseText

There's a similar method for PowerShell.

Bob

Posted 2012-03-26T14:48:36.460

Reputation: 51 526

Nice, it worked. I will insert it on my program. Its a good solution for my problem. Thank you. – Diogo – 2012-03-26T15:10:53.093

Hmm, it is returning a "Acess denied" on line 4, character 1 for this script. Even running it from a administrator rights command line... Do you know why? – Diogo – 2012-03-26T17:58:49.420

@DiogoRocha I don't know why. Fiasco's one is probably more reliable. It's also possible you have a firewall or something blocking internet access to wscript/cscript. – Bob – 2012-03-27T05:32:31.203

10

The problem is, it's not your IP address. It's the IP address of your router.

So you will never be able to retrieve it without going through your router. And the easiest way is to simply make the router retrieve a website for you (as you've already noticed). Because the router will use its IP address to retrieve that site.

That being said, you can make it easy on yourself by using ifconfig.me/ip. Now, if you have curl, you're already done, if not: Wget/cURL alternative native to Windows?

Der Hochstapler

Posted 2012-03-26T14:48:36.460

Reputation: 77 228

1http://ifconfig.me/ip took a long time to load. http://whatismyip.org/ gives you an identical plain-text IP and was a lot faster. – nhinkle – 2012-03-27T03:29:43.527

1@nhinkle That might depend on your location, though. They both appear to have the same load time for me, but ping response time from ifconfig.me is half (139ms vs 250ms). What's probably more important is that the service stays up for as long as possible, but we really have no way of knowing. – Bob – 2012-03-27T05:27:00.570

There's nothing wrong with the external IP addres of my router considering port forwarding rules and IPv4. – mbx – 2014-05-07T17:09:53.420

5

Create a VB script to run at your leisure.

Type this into a txt file:

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

Close the txt file and rename it to ip.vbs (save it to C: for this example)

In windows open a dos window (run cmd) (ha! I just realised if you swap that it becomes run dmc!!)

Make sure you're in c:/ (if not, type c: & press enter, then cd.. & enter a few times until you see C:>)

At the dos prompt type:

cscript ip.vbs

and you will instantly see your external IP.

If you put the vbs file onto a usb key or something (and remember to run cscript ip.vbs at the dos prompt - but make sure you're in the same directory as the ip.vbs file), you can take this with you anywhere you go and run the .vbs file on any computer to see their external IP.

One other note, the line that has the address for icanhazip.com can be changed to any of the following:

http://myip.dnsomatic.com
http://whatismyip.org
http://icanhazip.com
http://www.whatismyip.com/automation/n09230945.asp
http://externip.com

Edit, you can also run the ip.vbs file in windows without going to a dos window and it will just show up in a little pop up window.

Champ

Posted 2012-03-26T14:48:36.460

Reputation: 51

http://icanhazip.com has a very short response time that is better than http://ipconfig.me/ip here in center of Europe. – Martin Braun – 2015-06-07T18:49:44.223

4

I stumbled on this site when looking for the PowerShell equivalent and I thought I would share this command should anyone else be looking:

Invoke-WebRequest ifconfig.me/ip

More details are available here should you need them - http://jfrmilner.wordpress.com/2012/12/22/powershell-quick-tip-03-whats-my-external-ip-address-windows-command-line/

jfrmilner

Posted 2012-03-26T14:48:36.460

Reputation: 503

3

To get a external IP address, you need to ask something outside of the network and have it report back what your IP address "appears" to be to it. This can be done through scripting by querying a page that is easy to parse the result from and getting the IP address from that, but there is no tool built into Windows that you can just tell you what your external IP address is. You will need to write one yourself or find one and download it.

Scott Chamberlain

Posted 2012-03-26T14:48:36.460

Reputation: 28 923

2

Using something like DynDNS to assign your DHCP IP address a domain name, you could then do an nslookup of, or ping (if your router's configured to respond), your domain name and extract the IP address from that.

The only other Windows utility is traceroute, but it would only show the outbound gateway you connect to, not your router's IP address that's connecting to that gateway.

Edit: As it takes a bit of parsing on the above to extract the IP address string and ifconfig.me has a nice sparse response, here's a little bit of a variant on Bob's script, the function can test for no response and give back a nice clean IP address for use in a script if the server is up.

wscript.echo WAN_IP()

function WAN_IP()
    set obj = createobject("Microsoft.XMLHTTP")
    call obj.open("get", "http://ifconfig.me/ip", false)
    obj.send()

    strresponse = obj.responsetext
    set obj = nothing

    if strresponse <> "" then
        strIP = strresponse
    else
        strIP = "Unavailable"
    end if

    WAN_IP = trim(strIP)

end function

Fiasco Labs

Posted 2012-03-26T14:48:36.460

Reputation: 6 368

1

If your router support Universal plug & Play (UPnP) you can query it's IP address(es).

On Windows you can query such information using WMI and from command-line using the WMIC tool.

Robert

Posted 2012-03-26T14:48:36.460

Reputation: 4 857

2Could you provide an example? I'd be very interested in this. – Der Hochstapler – 2012-03-26T15:21:39.633

1

As others have already said, you need to rely on an external service. I'd recommend http://www.externalip.net

You can use http://api.externalip.net/ip to get the ip in plain text format. It has proven to be fast and reliable, plus it has unlimited use.

user178151

Posted 2012-03-26T14:48:36.460

Reputation: