How to obtain public ip address using windows command prompt?
Asked Answered
C

11

13

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!

Chairwoman answered 3/10, 2016 at 17:9 Comment(0)
P
27

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

Invoke-WebRequest ifconfig.me/ip

Go to source

Edit: I misread the question and thought you needed to use Powershell, 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.

Go to source

Prud answered 3/10, 2016 at 17:12 Comment(5)
Any way of doing it on the command prompt and not using powershell?Chairwoman
Keep getting DNS request timed out when using that command. :/Chairwoman
This is a basic nslookup command. As long as you can resolve to external DNS addresses, this will work. It's possible you do not have your dns resolvers configured properly. You could try using Google's DNS resolvers (8.8.8.8 and 8.8.4.4) but you will need to look at that on your local machine.Prud
There is no command that obtains the public IP address in command prompt. This is simply a workaround to the limitation you imposed. A clever "trick" if you will. You're better off using power shell which does actually have a function for doing what you want. You could try using telnet if it is installed. It's not installed/enabled on Windows by default.Prud
The real beef here I guess is that OpenDNS apparently offers this service by allowing you to query their server for the label myip.opendns.com. Probably the answer should spell this out; as it stands, it looks like "here's how to use nslookup".Stereophonic
U
16

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

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

& You Will get public IPV4.

Unsaid answered 24/6, 2020 at 19:5 Comment(1)
curl is also installed, if you install git bash -> C:\Program Files\Git\mingw32\bin\curl.exe or xampp -> C:\xampp\apache\bin\curl.exeKultur
B
9

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.

Blindstory answered 10/7, 2021 at 11:12 Comment(0)
P
3
curl ip-adresim.app

It supports both IPv4 and IPv6.

Persimmon answered 16/7, 2020 at 14:36 Comment(0)
K
2

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.

Knowledge answered 12/2, 2017 at 23:48 Comment(0)
T
2

This command works for me:

nslookup myip.opendns.com. resolver1.opendns.com
Tarboosh answered 3/10, 2018 at 10:23 Comment(1)
nslookup works but sometimes it yields ipv6 instead of 4, which can cause a headache...Kooky
N
2

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
Necessaries answered 18/4, 2022 at 4:30 Comment(0)
I
1

Actually you could use the very same command, both in Linux and Windows:

 curl http://ipinfo.io/ip

I use it as part of a batch file like so:

FOR /F "tokens=* USEBACKQ" %%F IN (`curl http://ipinfo.io/ip`) DO (
SET currentip=%%F
)
ECHO %currentip%
Ic answered 29/11, 2022 at 11:51 Comment(1)
So far, this is the only one that gives me IP v4.Freaky
B
1

another way is call this url in CURL in cmd or other command lines

curl ip.dnslab.link

response will be like this json

84.241.47.110
Brei answered 6/6, 2023 at 14:30 Comment(0)
N
0

Windows/Linux version:

nslookup myip.opendns.com resolver1.opendns.com

Unix version:

dig +short myip.opendns.com @resolver1.opendns.com
Nilson answered 24/4, 2020 at 2:1 Comment(2)
There is nothing particularly "Unix" about dig; there is also host. All three of nslookup, dig, and host are commonly installed on Unix / Linux / Mac, though typically at most one of them will be part of the standard install on any given platform (though on my Mac, I have all three in /usr/bin; but maybe that's part of XCode). nslookup used to be part of the base Windows install but I'm guessing this may no longer be true, either.Stereophonic
Anyway, this basically duplicates the information from the previous answers.Stereophonic
M
0

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%
Morose answered 2/6, 2020 at 14:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.