[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖
楼主顶楼的竖排文字很难读

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

TOP

回复 10 楼的帖子

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

[ 本帖最后由 neorobin 于 2010-6-24 21:54 编辑 ]

TOP

回 12 楼的帖子

取消 变量 mm 的第一次赋值即可,即 把这行
  1. set /a mm=1!time:~3,2!-100, ms=2 & rem ms 为时间间隔
复制代码
改为
  1. set /a ms=2 & rem ms 为时间间隔
复制代码
另外 11 楼的代码重新编辑过的, 发 11 楼帖时的代码是不支持 连接名称中含有 空格的, 现在修改过的可兼容含 空格 的连接名称, 请重新复制代码

[ 本帖最后由 neorobin 于 2010-6-24 22:10 编辑 ]

TOP

回复 15楼 的帖子

这个我不会, 但有一种隐藏运行的技术
  1. @echo off & setlocal enabledelayedexpansion
  2. if /i "%1" equ "hide" goto begin
  3. if /i "%1" equ "show" goto begin
  4. set /p show=需要显示窗口界面吗^(y/n^)[直接回车=不显示]:)
  5. if /i "%show:~0,1%" equ "y" goto begin
  6. (call :showWindow hide)
  7. :begin
  8. call :foundACName ACName
  9. set "ipa=192.168.1.101" & set "ipb=192.168.1.102" & set "mask=255.255.255.0" & set "gateway=192.168.1.1"
  10. set /a ms=2 & rem ms 为时间间隔
  11. for /l %%k in (0 0 0) do (
  12.   set /a "diff=1!time:~3,2!-100+60-mm, diff%%=60"
  13.   if !diff! geq !ms! ( set /a mm=1!time:~3,2!-100
  14.     for /f "tokens=2 delims=[]" %%i in ('ping -n 1 %computername% ^| find "["') do (
  15.       if "%%i"=="!ipa!" (netsh interface ip set address name="!ACName!" source=static addr=!ipb! mask=!mask! gateway=!gateway! 1
  16.       ) else (
  17.         netsh interface ip set address name="!ACName!" source=static addr=!ipa! mask=!mask! gateway=!gateway! 1
  18.       )
  19.       for /f "tokens=2 delims=[]" %%a in ('ping -n 1 %computername% ^| find "["') do echo %%a
  20.     )
  21.   )
  22. )
  23. exit /b
  24. :foundACName ACName rem 找出当前活动连接的名称
  25. for /f "tokens=2 delims=[]" %%i in ('ping -n 1 %computername% ^| find "["') do (
  26.   for /f "skip=1" %%a in ('wmic nicconfig where "ipenabled='true'" get index') do (
  27.     for /f "delims=}{" %%b in ('wmic nicconfig where "index=%%a" get ipaddress') do (
  28.       if "%%~b"=="%%i" for /f "tokens=1-2 delims==" %%c in ('wmic nic where "index=%%a" get NetConnectionID /value') do (
  29.         for /f "delims=" %%z in ("%%d") do set "_%%c=%%z"
  30.       )
  31.     )
  32.   )
  33. )
  34. set %1=!_NetConnectionID!&echo 当前活动连接为: "!%1!"
  35. exit /b
  36. :showWindow
  37. if /i "%1" equ "hide" (set intWindowStyle=0) else (set intWindowStyle=5)
  38. mshta vbscript:createobject("wscript.shell").run("%~snx0 %1",!intWindowStyle!)(window.close)&&exit
  39. exit /b
复制代码
要想关闭隐藏的 cmd , 可以用一种野蛮的方式关闭所有的 cmd
  1. taskkill /f /t /im cmd.exe
复制代码

[ 本帖最后由 neorobin 于 2010-6-24 23:07 编辑 ]

TOP

  1. @echo off & setlocal enabledelayedexpansion
  2. if /i "%1" equ "hide" goto begin
  3. if /i "%1" equ "show" goto begin
  4. rem 直接隐藏运行的方法: 删除或注解下面的一行
  5. set /p show=需要显示窗口界面吗^(y/n^)[直接回车=不显示]:
  6. if /i "%show:~0,1%" equ "y" goto begin
  7. (call :showWindow hide)
  8. :begin
  9. set "ACName=本地连接"
  10. (call :findACName ACName) & rem 不用搜索活动连接的话, 删除或注解本行.
  11. set /a ipts=2,ipte=9,ipt=3& rem ipts--ipte 指定可用的 ip 地址范围, 例如: 192.168.0.2--192.168.0.9, ipt 取范围中的任一值.
  12. set "iph=192.168.0." & set "mask=255.255.255.0" & set "gateway=!iph!1" & set ms=120 & rem ms: 时间间隔秒数
  13. netsh interface ip set address "!ACName!" static !iph!!ipt! !mask! !gateway! 1
  14. for /l %%k in (0 0 0) do (
  15.   set /a "ipt=(ipt+1-ipts)%%(ipte-ipts+1)+ipts"
  16.   netsh interface ip set address "!ACName!" static !iph!!ipt! !mask!
  17.   for /f "tokens=2 delims=[]" %%a in ('ping -n 1 %computername% ^| find "["') do echo %%a
  18.   ping -n !ms! 127.1>nul
  19. )
  20. exit /b
  21. :findACName ACName rem 找出当前活动连接的名称
  22. for /f "tokens=2 delims=[]" %%i in ('ping -n 1 %computername% ^| find "["') do (
  23.   for /f "skip=1" %%a in ('wmic nicconfig where "ipenabled='true'" get index') do (
  24.     for /f "delims=}{" %%b in ('wmic nicconfig where "index=%%a" get ipaddress') do (
  25.       if "%%~b"=="%%i" for /f "tokens=1-2 delims==" %%c in ('wmic nic where "index=%%a" get NetConnectionID /value') do (
  26.         for /f "delims=" %%z in ("%%d") do set "_%%c=%%z"
  27.       )
  28.     )
  29.   )
  30. )
  31. set %1=!_NetConnectionID!&echo 当前活动连接为: "!%1!"
  32. exit /b
  33. :showWindow
  34. if /i "%1" equ "hide" (set intWindowStyle=0) else (set intWindowStyle=5)
  35. mshta vbscript:createobject("wscript.shell").run("%~snx0 %1",!intWindowStyle!)(window.close)&&exit
  36. exit /b
复制代码
1

评分人数

    • batman: 好热心,论坛就需要兄弟这样的热心人,好!PB + 10

TOP

返回列表