4 楼是利用的计划任务, 不够完善, 下面给出直接批处理循环, 但批处理程序不能关闭- @echo off & setlocal enabledelayedexpansion
- call :foundACName ACName
- set "ipa=192.168.1.101" & set "ipb=192.168.1.102" & set "mask=255.255.255.0" & set "gateway=192.168.1.1"
- set /a mm=1!time:~3,2!-100, ms=2 & rem ms 为时间间隔
- for /l %%k in (0 0 0) do (
- set /a "diff=1!time:~3,2!-100+60-mm, diff%%=60"
- if !diff! geq !ms! ( set /a mm=1!time:~3,2!-100
- for /f "tokens=2 delims=[]" %%i in ('ping -n 1 %computername% ^| find "["') do (
- if "%%i"=="!ipa!" (netsh interface ip set address name="!ACName!" source=static addr=!ipb! mask=!mask! gateway=!gateway! 1
- ) else (
- netsh interface ip set address name="!ACName!" source=static addr=!ipa! mask=!mask! gateway=!gateway! 1
- )
- for /f "tokens=2 delims=[]" %%a in ('ping -n 1 %computername% ^| find "["') do echo %%a
- )
- )
- )
- exit /b
-
- :foundACName ACName rem 找出当前活动连接的名称
- for /f "tokens=2 delims=[]" %%i in ('ping -n 1 %computername% ^| find "["') do (
- for /f "skip=1" %%a in ('wmic nicconfig where "ipenabled='true'" get index') do (
- for /f "delims=}{" %%b in ('wmic nicconfig where "index=%%a" get ipaddress') do (
- if "%%~b"=="%%i" for /f "tokens=1-2 delims==" %%c in ('wmic nic where "index=%%a" get NetConnectionID /value') do (
- for /f "delims=" %%z in ("%%d") do set "_%%c=%%z"
- )
- )
- )
- )
- set %1=!_NetConnectionID!&echo 当前活动连接为: "!%1!"
- exit /b
复制代码 另给出一个实时监控显示 IP 的代码:- @echo off & setlocal enabledelayedexpansion& set "color=02"& color !color!&mode con lines=1 cols=28
- for /l %%i in (0 0 0) do for /f "tokens=2 delims=[]" %%a in ('ping -a -n 1 %computername% ^| find "["') do (
- if "%%a" neq "!ip!" set ip=%%a&set color=!color:~1!!color:~0,-1!&color !color!
- cls&set /p=!time! %%a<nul
- )
复制代码
[ 本帖最后由 neorobin 于 2010-6-24 21:54 编辑 ] |