7

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

3
  • 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
    Oct 22, 2015 at 17:06
  • Hey now. That wasn't necessary. This wasn't on my "homework" assignment. I'm just trying to learn.
    – MIchael S
    Oct 22, 2015 at 17:11
  • 1
    It 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? Oct 22, 2015 at 17:59

7 Answers 7

12

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
3
  • 3
    OP requested a solution without using external sites. Oct 22, 2015 at 23:58
  • @ChrisInEdmonton fair point though maybe the OP had a web browser in mmind when he said that, but fair point
    – barlop
    Oct 23, 2015 at 0:03
  • 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). Oct 23, 2015 at 0:04
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.

0
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.

1
  • 1
    OP requested a solution without using external sites. Oct 22, 2015 at 23:58
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.

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 .
1
  • 2
    (1) The question requested a solution without using external sites.  (2) A solution based on these sites has already been posted. Feb 13, 2017 at 0:31
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.

0

Here is a solution using Powershell:

@powershell -NoProfile -Command "(new-object net.webclient).DownloadString('http://www.trackip.net/ip')"
1
  • 2
    OP requested a solution without using external sites. Oct 22, 2015 at 23:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .