如何使用 windows 命令提示符获取 public ip 地址?

How to obtain public ip address using windows command prompt?

有什么方法可以在不使用任何第三方软件或不依赖 powershell 的情况下通过命令提示符执行此操作?

像 Linux/Mac 上的简单命令会很棒,我用 curl http://ipinfo.io/ip 来做。

谢谢!

在 powershell 中使用 Invoke-WebRequest 模块。例如:

Invoke-WebRequest ifconfig.me/ip

Source

编辑:我误读了问题并认为您需要使用 Powrshell。

cmd.exe 中没有内置命令到 return public IP 地址。但是你可以用nslookup来解决,像这样;

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

OP 的另一个选项:

telnet curlmyip.com 80

连接后键入 "GET"。

注意:默认情况下,telnet 不是 installed/enabled Windows。

Source

对正常的 dos 批处理试试这个

@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%

现在,%MyIP% 可以回显或用作其他批处理使用的变量。

这个命令对我有用:

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

Windows/Linux版本:

nslookup myip.opendns.com resolver1.opendns.com

Unix 版本:

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

这非常适合我:

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%

最简单的方法和跨平台解决方案(Mac、Windows 和 Linux)

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

& 您将获得 public IPV4.

curl ip-adresim.app

同时支持 IPv4 和 IPv6。

您可以在 Windows 和 Linux

中使用这些命令

curl ifconfig.me

Curl 是升级到最新版本后Windows内置的东西,所以不叫第三方软件。

curl ifconfig.co

在Linux中,几乎所有的发行版都有这个命令。

非常感谢 ArthurG 的命令。它工作完美,可以以多种方式用于批处理脚本。

Oneliner,没有 curl 或 powershell,只是裸 CMD,IP4 的原始 ip

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