'How to obtain public ip address using windows command prompt?

Any way of doing this through the command prompt without using any third party software or relying on powershell?

A simple command would be great like on Linux/Mac which I use curl http://ipinfo.io/ip for.

Thanks!



Solution 1:[1]

Use the Invoke-WebRequest module in powershell. For example:

Invoke-WebRequest ifconfig.me/ip

Source

Edit: I misread the question and thought you needed to use Powrshell.

There is no built in command in cmd.exe to return a public IP address. But you can use nslookup to resolve it, like so;

nslookup myip.opendns.com. resolver1.opendns.com

Another option for the OP:

telnet curlmyip.com 80

Type "GET" after you are connected.

Note: telnet is not installed/enabled by default on Windows.

Source

Solution 2:[2]

Simplest way & Cross platform solution (Mac, Windows, and Linux)

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

& You Will get public IPV4.

Solution 3:[3]

you can use these commands in both Windows and Linux

curl ifconfig.me

Curl is something that is built-in Windows when you upgrade to the latest version, so it's not called a third-party software.

or

curl ifconfig.co

In Linux, almost Distros have this command already.

Thanks so much ArthurG for your commands. It works perfectly and can be used for Batch script in many ways.

Solution 4:[4]

Try this for normal dos batch

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

Now, %MyIP% will can be echo or used as a variable for additional batch use.

Solution 5:[5]

This command works for me:

nslookup myip.opendns.com. resolver1.opendns.com

Solution 6:[6]

curl ip-adresim.app

It supports both IPv4 and IPv6.

Solution 7:[7]

Windows/Linux version:

nslookup myip.opendns.com resolver1.opendns.com

Unix version:

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

Solution 8:[8]

This works perfectly for me:

curl http://ip.jsontest.com/ > ip.json
for /f "tokens=2 delims=:" %%a in ('type ip.json') do (set publicip=%%a)
set publicip=%publicip:{=%
set publicip=%publicip:}=%
set publicip=%publicip:"=%
echo %publicip:~1%

Solution 9:[9]

Oneliner, no curl or powershell, just bare CMD, raw ip for IP4

FOR /f "skip=1 tokens=2 delims=:\ " %G IN ('nslookup myip.opendns.com resolver1.opendns.com 2^>^&1 ^|find "dress"') DO @echo %G

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Community
Solution 2
Solution 3
Solution 4 ArthurG
Solution 5 Yannis
Solution 6 ilhan
Solution 7 tripleee
Solution 8 Anic17
Solution 9