[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程批处理在线视频分享
返回列表 发帖

[网络连接] 【已解决】请教,批处理获取多网卡系统中指定网络连接对应的网关子网掩码DNS

本帖最后由 godkong 于 2018-3-8 22:59 编辑
  1. @echo off
  2. REM 设置网络适配器的名称
  3. REM 英文系统一般是:Local Area Connection
  4. REM set "AdapterName=Local Area Connection"
  5. REM 中文系统一般是:本地连接
  6. set "AdapterName=本地连接"
  7. set "FileTmp=%temp%\ipList.txt"
  8. ipconfig /all >"%FileTmp%"
  9. for /f "delims=:" %%i in ('findstr /n /c:"%AdapterName%" "%FileTmp%"') do (
  10.     set "SkipRow=%%i"
  11.     goto :DoSkip
  12. )
  13. :DoSkip
  14. for /f "tokens=2 delims=:(" %%i in ('more +%SkipRow% "%FileTmp%" ^| findstr /v "IPv6" ^|findstr "IP"') do (
  15.     set "IP=%%i"
  16.     goto :ShowResult
  17. )
  18. :ShowResult
  19. set "IP=%IP: =%"
  20. echo,你的IP是:%IP%
  21. pause
复制代码
此为论坛大神Batcher 的杰作,只提取显示了IP地址,我现在要实现同时显示子网掩码、网关、主备DNS地址,5个信息,请教各位大神批处理改怎么写?
[quote]终于成功了,感谢Batcher大神的批处理。
godkong 发表于 2018-3-8 22:46

[/quote]
1

评分人数

  1. @echo off
  2. REM 设置网络适配器的名称
  3. REM 英文系统一般是:Local Area Connection
  4. REM set "AdapterName=Local Area Connection"
  5. REM 中文系统一般是:本地连接
  6. set "AdapterName=本地连接"
  7. set "FileTmp=%temp%\ipList.txt"
  8. ipconfig /all >"%FileTmp%"
  9. for /f "delims=:" %%i in ('findstr /n /c:"%AdapterName%" "%FileTmp%"') do (
  10.     set "SkipRow=%%i"
  11.     goto :DoSkip
  12. )
  13. :DoSkip
  14. for /f "tokens=2 delims=:" %%i in ('more +%SkipRow% "%FileTmp%" ^| findstr "默认网关"') do (
  15.     set "gateway=%%i"
  16.     goto :ShowResult
  17. )
  18. :ShowResult
  19. set "gateway=%gateway: =%"
  20. echo,%gateway%
  21. pause
复制代码
这样能显示网关,但同时显示IP和网关要怎么修改代码?

TOP

  1. @echo off
  2. REM 设置网络适配器的名称
  3. REM 英文系统一般是:Local Area Connection
  4. REM set "AdapterName=Local Area Connection"
  5. REM 中文系统一般是:无线网络连接
  6. set "AdapterName=无线网络连接"
  7. set "FileTmp=%temp%\ipList.txt"
  8. ipconfig /all >"%FileTmp%"
  9. for /f "delims=:" %%i in ('findstr /n /c:"%AdapterName%" "%FileTmp%"') do (
  10.     set "SkipRow=%%i"
  11.     goto :DoSkip
  12. )
  13. :DoSkip
  14. for /f "tokens=2 delims=:(" %%i in ('more +%SkipRow% "%FileTmp%" ^| findstr
  15. /v "IPv6" ^|findstr "IP"') do (
  16.     set "IP=%%i"
  17.     goto :DoSkip1
  18. )
  19. :DoSkip1
  20. for /f "tokens=2 delims=:" %%i in ('more +%SkipRow% "%FileTmp%" ^| findstr
  21. "..-..-..-..-..-.."') do (
  22.     set "MAC=%%i"  
  23.     goto :ShowResult
  24. )
  25. :ShowResult
  26. set "IP=%IP: =%"
  27. set "MAC=%MAC: =%"
  28. echo,%IP%,%MAC%
  29. pause
复制代码
这样可以同时显示IP和MAC地址,但是死活不会修改,按照理解去修改,死活搞不出DNS服务器的显示

TOP

  1. @echo off
  2. REM 设置网络适配器的名称
  3. REM 英文系统一般是:Local Area Connection
  4. REM set "AdapterName=Local Area Connection"
  5. REM 中文系统一般是:无线网络连接
  6. set "AdapterName=无线网络连接"
  7. set "FileTmp=%temp%\ipList.txt"
  8. ipconfig /all >"%FileTmp%"
  9. for /f "delims=:" %%i in ('findstr /n /c:"%AdapterName%" "%FileTmp%"') do (
  10.     set "SkipRow=%%i"
  11.     goto :DoSkip
  12. )
  13. :DoSkip
  14. for /f "tokens=2 delims=:(" %%i in ('more +%SkipRow% "%FileTmp%" ^| findstr /v "IPv6" ^|findstr "IP"') do (
  15.     set "IP=%%i"
  16.     goto :DoSkip1
  17. )
  18. :DoSkip1
  19. for /f "tokens=2 delims=:" %%i in ('more +%SkipRow% "%FileTmp%" ^| findstr "..-..-..-..-..-.."') do (
  20.     set "MAC=%%i"  
  21.     goto :DoSkip2
  22. )
  23. :DoSkip2
  24. for /f "tokens=2 delims=:" %%i in ('more +%SkipRow% "%FileTmp%" ^| findstr "子网掩码"') do (
  25.     set "mask=%%i"  
  26.     goto :DoSkip3
  27. )
  28. :DoSkip3
  29. for /f "tokens=2 delims=:" %%i in ('more +%SkipRow% "%FileTmp%" ^| findstr "默认网关"') do (
  30.     set "gateway=%%i"  
  31.     goto :DoSkip4
  32. )
  33. :DoSkip4
  34. for /f "tokens=2 delims=:" %%i in ('more +%SkipRow% "%FileTmp%" ^| findstr "DNS服务器"') do (
  35.     set "dns=%%i"  
  36.     goto :ShowResult
  37. )
  38. :ShowResult
  39. set "IP=%IP: =%"
  40. set "MAC=%MAC: =%"
  41. set "mask=%mask: =%"
  42. set "gateway=%gateway: =%"
  43. set "dns=%dns: =%"
  44. echo,你的IP是:%IP%
  45. echo,你的物理地址是:%MAC%
  46. echo,你的子网掩码是:%mask%
  47. echo,你的网关是:%gateway%
  48. echo,你的DNS服务器地址是:%dns%
  49. pause
复制代码
显示五个信息的代码,为什么DNS服务器地址不能显示,代码怎么修改?

TOP

  1. @echo off
  2. REM 设置网络适配器的名称
  3. REM 英文系统一般是:Local Area Connection
  4. REM set "AdapterName=Local Area Connection"
  5. REM 中文系统一般是:无线网络连接
  6. set "AdapterName=无线网络连接"
  7. set "FileTmp=%temp%\ipList.txt"
  8. ipconfig /all >"%FileTmp%"
  9. for /f "delims=:" %%i in ('findstr /n /c:"%AdapterName%" "%FileTmp%"') do (
  10.     set "SkipRow=%%i"
  11.     goto :DoSkip
  12. )
  13. :DoSkip
  14. for /f "tokens=2 delims=:(" %%i in ('more +%SkipRow% "%FileTmp%" ^| findstr /v "IPv6" ^|findstr "IP"') do (
  15.     set "IP=%%i"
  16.     goto :DoSkip1
  17. )
  18. :DoSkip1
  19. for /f "tokens=2 delims=:" %%i in ('more +%SkipRow% "%FileTmp%" ^| findstr "..-..-..-..-..-.."') do (
  20.     set "MAC=%%i"  
  21.     goto :DoSkip2
  22. )
  23. :DoSkip2
  24. for /f "tokens=2 delims=:" %%i in ('more +%SkipRow% "%FileTmp%" ^| findstr "子网掩码"') do (
  25.     set "mask=%%i"  
  26.     goto :DoSkip3
  27. )
  28. :DoSkip3
  29. for /f "tokens=2 delims=:" %%i in ('more +%SkipRow% "%FileTmp%" ^| findstr "默认网关"') do (
  30.     set "gateway=%%i"  
  31.     goto :DoSkip4
  32. )
  33. :DoSkip4
  34. for /f "tokens=2 delims=:" %%i in ('more +%SkipRow% "%FileTmp%" ^| findstr /v "router" ^| findstr "DNS 服务器"') do (
  35.     set "dns=%%i"  
  36.     goto :ShowResult
  37. )
  38. :ShowResult
  39. set "IP=%IP: =%"
  40. set "MAC=%MAC: =%"
  41. set "mask=%mask: =%"
  42. set "gateway=%gateway: =%"
  43. set "dns=%dns: =%"
  44. echo,你的IP是:%IP%
  45. echo,你的物理地址是:%MAC%
  46. echo,你的子网掩码是:%mask%
  47. echo,你的网关是:%gateway%
  48. echo,你的DNS服务器地址是:%dns%
  49. pause
复制代码
终于成功了,感谢Batcher大神的批处理。

TOP

备用DNS好像无法t提取并显示出来,大神们有办法吗?

TOP

返回列表