I am making a program that checks if a user's IP is a certain IP address.
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
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.
Sincerely, djmrminer.
With pure batch/already present tools:
EDIT: changed the batch to properly handle also IPv6 addresses
Reference
Another one with powershell:
First it seems there is a
.
too much after the first.com
.Second when using your command with simply
google.com
andecho %a
I get the following:" xx.xx.xx.x"
without the quotes and with two leading spaces!So your if will never be true!
Change it to something like this:
if "%%a"==" xx.xx.xx.x" Goto:good
and you should be fine.To get your public IP without additional parsing do this:
curl "http://api.ipify.org"
EDIT:
This version is more reliable across windows language versions: