Windows批处理 - 如何将外部IP转换为批处理文件变量

问题描述:

我正在制作一个程序来检查用户的IP是否是某个IP地址。

I am making a program that checks if a user's IP is a certain IP address.

目前,我创建了一个成功的内部IP版本:

Currently, I created a successful internal IP version:

@echo off
set userIp=192.168.90.100
for /f "tokens=4 delims= " %%i in ('route print ^| find " 0.0.0.0"') do set localIp=%%i
for /f "delims=[] tokens=2" %%a in ('ping %computername% -4 -n 1 ^| findstr "["') do set thisip=%%a
goto :Check

:Check
if %localIp%==%userIp% goto :Good
if %thisip%==%userIp% goto :Good
goto :Bad

我正在尝试使用与外部IP协同工作的相同功能。

And I am trying to make the same thing that works with external IPs.

我在网上研究过,这是我到目前为止所做的。

I researched online, and here is what I got so far.

@echo off
for /f "tokens=2 delims=:" %%a IN ('nslookup myip.opendns.com. resolver1.opendns.com ^| findstr /IC:"Address"') do if /i %%a=="10.11.12.13" goto :Good
goto :Bad

我需要一些帮助解决这个问题。

I need a bit of help on how to fix this.

此致,djmrminer。

Sincerely, djmrminer.

纯粹批/已存在的工具:

With pure batch/already present tools:

@Echo off
for /f "tokens=2 delims=: " %%A in (
  'nslookup myip.opendns.com. resolver1.opendns.com 2^>NUL^|find "Address:"'
) Do set ExtIP=%%A
Echo External IP is : %ExtIP%

参考

另一个有powershell的人:

Another one with powershell:

@Echo off
For /f %%A in (
  'powershell -command "(Invoke-Webrequest "http://api.ipify.org").content"'
) Do Set ExtIP=%%A
Echo External IP is : %ExtIP%