楼主顶楼的竖排文字很难读
以下代码中 /mo 后面的数字 即为 时间间隔, 单位 分钟
ipa, ipb, mask, gateway 各变量请楼主根据自己的要求自行改动, 我在自己机器上已测试可正常运行.- @echo off & setlocal enabledelayedexpansion
- set "taskName=IP_Switch"
- if not exist "%windir%\tasks\!taskName!.job" (
- rem 确保 Task Scheduler 服务启动
- sc config Schedule start= Auto>nul
- (sc query Schedule | find "RUNNING">nul) || (
- ((sc query Schedule | find "PAUSED">nul) && sc continue Schedule) || sc start Schedule
- )
- rem 建立计划任务运行本程序
- schtasks /create /tn "!taskName!" /sc minute /mo 2 /tr %~s0 /ru system
- )
- rem 要运行的任务的实体
- set "ipa=192.168.1.101" & set "ipb=192.168.1.102" & set "mask=255.255.255.0" & set "gateway=192.168.1.1"
- for /f "tokens=2 delims=[]" %%i in ('ping -n 1 %computername% ^| find "["') do (
- for /f %%a in ('wmic nicconfig where "ipenabled='true'" get index') do (
- if /i "%%a" neq "index" (
- for /f "delims=}{" %%b in ('wmic nicconfig where "index=%%a" get ipaddress') do (
- if "%%~b"=="%%i" for /f "skip=1" %%c in ('wmic nic where "index=%%a" get NetConnectionID') do (
- if "%%~b"=="!ipa!" (netsh interface ip set address name="%%c" source=static addr=!ipb! mask=!mask! gateway=!gateway! 1
- ) else (
- netsh interface ip set address name="%%c" source=static addr=!ipa! mask=!mask! gateway=!gateway! 1
- )
- )
- )
- )
- )
- )
复制代码
|