我最近升级了我的宽带连接,所以新的有 ipv6 和 ipv4。我以前的只有 ipv4。我想将 ipv4 放在批处理文件中的变量中,而不是 ipv6。我使用的当前命令给出了默认 ip,所以之前它用于将 ipv4 存储在变量中,但现在它正在将 ipv6 存储在其中。我不知道如何获得 ipv4,因为我使用的 nslookup 只提供 ipv6。这是我当前的批处理文件。
@echo off
setlocal EnableExtensions Disabledelayedexpansion
cd /d "%~dp0"
for /f "tokens=1* delims=: " %%A in (
'nslookup myip.opendns.com. resolver1.opendns.com 2^>NUL^|find "Address:"'
) Do set ExtIP=%%B
<save.ip set /p SavIP=
if "%SavIP%" neq "%ExtIP%" (
echo %date% %time~0,5% : IP changed from %SavIP% to %ExtIP%.
>save.ip echo %ExtIP%
start http://%ExtIP% #this is where I put the url
) else (
echo ExtIP didn't change. Nothing to do.
)
我刚刚测试了这个批处理文件,它在我这边工作 5/5 !
@echo off
Title Get Public IP by Powershell and Batch
@for /f "delims=" %%# in (
'PowerShell -C "Resolve-DnsName -Name "'myip.opendns.com'" -Server "'resolver1.opendns.com'" | select IPAddress" ^| findstr "\."'
) do Set "MyPublicIP=%%#"
echo My Public IP Address = %MyPublicIP%
pause
这是另一个调用子程序的程序 :GetPublic_IP <Target> <IP>
@echo off
Title Get Public IP by Powershell and Batch
Set "MyPublicIP=myip.opendns.com"
Set "Google=www.google.com"
Set "Yahoo=www.yahoo.com"
Set "Stack=www.stackoverflow.com"
Call :GetPublic_IP "%MyPublicIP%" MyPublicIP
echo My Public IP Address = %MyPublicIP%
Call :GetPublic_IP "%Google%" Google_IP
echo Google IP Address = %Google_IP%
Call :GetPublic_IP "%Yahoo%" Yahoo_IP
echo Yahoo IP Address = %Yahoo_IP%
Call :GetPublic_IP "%Stack%" Stack_IP
echo Stackoverflow IP Address = %Stack_IP%
pause
EXIT
::------------------------------------------------------------------------------------
:GetPublic_IP <Target> <IP>
@for /f "delims=" %%# in (
'PowerShell -C "Resolve-DnsName -Name '"%~1"' -Server "'resolver1.opendns.com'" | select IPAddress" ^| findstr "\."'
) do Set "%2=%%#
Exit /B
::------------------------------------------------------------------------------------
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句