How do you get your public IP address through the Windows command line?

7

5

Is it possible to get your public IP address through cmd without using sites such as http://whatismyipaddress.com/? I am behind a router.

Please feel free to go as detailed as possible. I'm studying for my Net+ Exam

MIchael S

Posted 2015-10-22T16:59:58.077

Reputation: 71

Not unless you have a script or client on your computer that communicates with an external server that can find your IP from a request - such as how many websites can identify your public IP, or a dynamic DNS client such as DynDNS. P.s. We're not here to do your homework for you. – Kinnectus – 2015-10-22T17:06:14.817

Hey now. That wasn't necessary. This wasn't on my "homework" assignment. I'm just trying to learn. – MIchael S – 2015-10-22T17:11:30.287

1It depends on precisely what you mean by "your public IP address". For example, suppose you're on a machine that has a private IP address and uses a variety of nameservers and proxies to reach the Internet, each of which has their own public IP address. What is "your public IP address" in this case? – David Schwartz – 2015-10-22T17:59:15.723

Answers

11

You can use PowerShell Invoke-RestMethod:

PS > Invoke-RestMethod ipinfo.io/ip
99.109.97.210

Or the alias:

PS > irm ipinfo.io/ip
99.109.97.210

Rachel Duncan

Posted 2015-10-22T16:59:58.077

Reputation: 1

3OP requested a solution without using external sites. – ChrisInEdmonton – 2015-10-22T23:58:34.377

@ChrisInEdmonton fair point though maybe the OP had a web browser in mmind when he said that, but fair point – barlop – 2015-10-23T00:03:30.307

Yeap. If the OP is willing to use an external site so long as he doesn't have to use a web browser, this solution would work (as would others posted here). – ChrisInEdmonton – 2015-10-23T00:04:20.727

9

You can't. Why? There's no clear definition of a 'public ip address'. I'd note the answer's a massive abstraction, good enough to explain my point, but riddled with minor holes. Feel free to comment on big ones (so I can plug them).

Essentially, all a public ip address is is an address on a subnet that's not been reserved for private use. There's no distinction between a public and private ip address as far as the network stack's concerned. Its just routing between subnets. In theory, in a closed network, while its terrible practice, you could use a public IP address range, and doing the reverse - using a RFC 1918 address on the public internet, would likely break things.

If I run pathping (and while I'm not willing to stick my ip address out on the internet, but its not cgn, and starts with 101)

enter image description here

My ip address isn't anywhere on the route (so... your pc might actually not be aware of its public ip address).

The concept of a public ip address is nebulous too. What would it be behind double nat (eww!), or even triple nat (maybe with overlapping ip address ranges!). What would it be behind CGN or even a closed network?

In essence each router or system simply needs to know the next hop and there's no need or way for your PC to know what its public IP address is.

The reason that an external service can find your 'public' ip address is that's the hop that the message was delivered to as far as the server's concerned. It too will/does not need to know whether that's a end user's computer, a proxy behind which you have a end user's computer, a router/switch or even a toaster.

Journeyman Geek

Posted 2015-10-22T16:59:58.077

Reputation: 119 122

4

There's nothing automatically built into Windows that will do this. You could, as other users have suggested in the past, try something like:

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

and use a DNS request to return your public facing IP. Answer was originally found here.

Sam Waldie

Posted 2015-10-22T16:59:58.077

Reputation: 41

1OP requested a solution without using external sites. – ChrisInEdmonton – 2015-10-22T23:58:39.487

1

Try this. You can use %MyIP% as a variable or just to display.

@echo off
nslookup myip.opendns.com resolver1.opendns.com | find "Address" >"%temp%\test1.txt"
more +1 "%temp%\test1.txt" >"%temp%\test2.txt"
del %temp%\test1.txt
set /p MyIP=<%temp%\test2.txt
del %temp%\test2.txt
set MyIP=%MyIP:~10%
cls
echo .
echo %MyIP%
echo .

ArthurG

Posted 2015-10-22T16:59:58.077

Reputation: 21

2(1) The question requested a solution without using external sites.  (2) A solution based on these sites has already been posted. – Scott – 2017-02-13T00:31:26.443

1

nslookup myip.opendns.com resolver1.opendns.com | find "Address:" >"%temp%\test1.txt"
more +1 "%temp%\test1.txt" >"%temp%\test2.txt"
del %temp%\test1.txt
set /p MyIP=<%temp%\test2.txt
del %temp%\test2.txt
set MyIP=%MyIP:~10%
echo My IP is %MyIP%

This self-cleans, but it pulls two IP Addresses (the one from resolver1.opendns.com and your current IP Address, crops off the resolver1 address, to just set the variable MyIP as your current IP Address. It works well in batch files.

ArthurG

Posted 2015-10-22T16:59:58.077

Reputation: 21

1

Is it possible to get your public IP address through cmd?

Your question is impossible to answer as it stands, because you are asking the wrong question.

You should have asked:

How do I get the WAN address of my internet connection from the commmand line?

The WAN address can be a public or a private IP address:

  • Your ISP provides a WAN address.
  • It can be either static/dynamic
  • It can be public/private

It all depends on how the ISP has configured his network to connect to yours in order to provide you with an internet service.

DavidPostill

Posted 2015-10-22T16:59:58.077

Reputation: 118 938

0

Here is a solution using Powershell:

@powershell -NoProfile -Command "(new-object net.webclient).DownloadString('http://www.trackip.net/ip')"

heavyd

Posted 2015-10-22T16:59:58.077

Reputation: 54 755

2OP requested a solution without using external sites. – ChrisInEdmonton – 2015-10-22T23:59:44.120