获取外网IP地址的批处理代码
程序员文章站
2023-09-06 16:20:11
复制代码 代码如下:
::==========================================
:: ip.bat
:: v0.1
:: ji...
复制代码 代码如下:
::==========================================
:: ip.bat
:: v0.1
:: jinminghou.cn@2009-12-22
::==========================================
@echo off
title getip
set ip=unknown
echo on error resume next:set odom=wscript.getobject("http://www.jinminghou.cn/?app=ip&flag=client&temp="^&now()):dim i:for i = 1 to 50:if
odom.readystate="complete" then exit for:end if:wscript.sleep 200:next:dim temp:temp=odom.documentelement.outertext:set odom=nothing:temp=mid(temp, instr
(temp, "[")+1, instr(temp, "]")-instr(temp, "[")-1):wscript.echo temp>"%temp%\getip.vbs"
for /f "tokens=1,* delims=:" %%i in ('"cscript //nologo ^"%temp%\getip.vbs^"') do set ip=%%i
if exist "%temp%\getip.vbs" del "%temp%\getip.vbs">2>nul
echo your ip: [%ip%]
pause>2>nul
【方案一】bat + vbs
复制代码 代码如下:
@echo off
set "url=http://www.ip138.com/ip2city.asp"
>%temp%/download.vbs echo set objdom = wscript.getobject(wscript.arguments(0))
>>%temp%/download.vbs echo do until objdom.readystate = "complete"
>>%temp%/download.vbs echo wscript.sleep 100
>>%temp%/download.vbs echo loop
>>%temp%/download.vbs echo wscript.echo objdom.documentelement.outertext
for /f "tokens=2 delims=[]" %%a in ('cscript //nologo //e:vbscript %temp%/download.vbs "%url%"') do (
set "publicip=%%a"
)
echo,%publicip%
pause
【方案二】bat + wget.exe(gnu)
复制代码 代码如下:
@echo off
set "url=http://www.ip138.com/ip2city.asp"
for /f "tokens=2 delims=[]" %%a in ('wget -q "%url%" -o -') do (
set "publicip=%%a"
)
echo,%publicip%
pause
【方案三】bat + curl.exe(gnu)
复制代码 代码如下:
@echo off
set "url=http://www.ip138.com/ip2city.asp"
for /f "tokens=2 delims=[]" %%a in ('curl -s "%url%"') do (
set "publicip=%%a"
)
echo,%publicip%
pause