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

ᔕᖺᘎᕊ
  • 6,393

7 Answers7

13

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
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
  • 133,878
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

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
  • 21
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
  • 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
  • 162,382
0

Here is a solution using Powershell:

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