Board logo

标题: [网络连接] (已解决)我这个批处理是改网关的,但是碰到网卡名字改变的情况下改不了 [打印本页]

作者: conan2095    时间: 2023-12-29 22:04     标题: (已解决)我这个批处理是改网关的,但是碰到网卡名字改变的情况下改不了

本帖最后由 conan2095 于 2023-12-29 22:45 编辑
  1. @echo off
  2. setlocal enabledelayedexpansion
  3. REM 读取本机网关
  4. for /f "tokens=3" %%a in ('"route print 0.0.0.0|findstr 0.0.0.0"') do (
  5.     set gateway=%%a
  6. )
  7. echo 当前网关为:%gateway%
  8. echo 输入数字1和2来选择网关,选1网关改成1,选2网关改成200 输入Q 退出
  9. set /p "n=请输入选项: "
  10. if /i "%n%"=="q" exit
  11. if %n% equ 1 goto 1
  12. if %n% equ 2 goto 2
  13. echo 无效的选项,请重新输入。
  14. pause
  15. exit /b
  16. :1
  17. netsh interface IP Set Addr "网络    2" Static 192.168.33.33 255.255.255.0 192.168.33.1 gwmetric=1
  18. echo 网关已设置为 192.168.33.1
  19. exit /b
  20. :2
  21. netsh interface IP Set Addr "网络    2" Static 192.168.33.33 255.255.255.0 192.168.33.200 gwmetric=1
  22. echo 网关已设置为 192.168.33.200
  23. exit /b
复制代码
但是如果网卡名字是其他名字就改不了,能不能帮我写一个获取网卡名字的代码插进去,然后网卡名字用变量代替,要考虑到网卡名字有空格的情况
作者: conan2095    时间: 2023-12-29 22:44

我在AI 上找到答案了 谢谢大家
  1. @echo off
  2. setlocal enabledelayedexpansion
  3. REM 读取本机网关
  4. for /f "tokens=3" %%a in ('"route print 0.0.0.0|findstr 0.0.0.0"') do (
  5.     set gateway=%%a
  6. )
  7. echo 当前网关为:%gateway%
  8. REM 获取网卡名称
  9. for /f "delims=" %%a in ('netsh interface show interface^|more +3') do (
  10.     set "str=%%a"
  11.     for /f "tokens=*" %%b in ("!str:~46!") do set interfaceName=%%b
  12. )
  13. echo 网卡名称为: %interfaceName%
  14. echo 输入数字1和2来选择网关,选1网关改成1,选2网关改成200 输入Q 退出
  15. set /p "n=请输入选项: "
  16. if /i "%n%"=="q" exit
  17. if %n% equ 1 goto 1
  18. if %n% equ 2 goto 2
  19. echo 无效的选项,请重新输入。
  20. pause
  21. exit /b
  22. :1
  23. netsh interface IP Set Addr "%interfaceName%" Static 192.168.33.33 255.255.255.0 192.168.33.1 gwmetric=1
  24. echo 网关已设置为 192.168.33.1
  25. exit /b
  26. :2
  27. netsh interface IP Set Addr "%interfaceName%" Static 192.168.33.33 255.255.255.0 192.168.33.200 gwmetric=1
  28. echo 网关已设置为 192.168.33.200
  29. exit /b
复制代码





欢迎光临 批处理之家 (http://www.bathome.net/) Powered by Discuz! 7.2