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

[文本处理] 使用批处理脚本获取本机真实IP地址

因本机安装了虚拟机,所以多了2个虚拟网卡
[attach]11737[/attach]
所以编写以下代码过滤虚拟网卡,获取真实IP,但是有BUG,如果本地IP为固定IP地址,则会显示"默认"。

以下是动态IP地址获取状态,正常状态:

以下是固态IP地址获取,不正常

请问改如何修改
  1. @echo on
  2. for /f "tokens=4" %%a in ('route print^|findstr 0.0.0.0.*0.0.0.0') do (
  3. set IP=%%a
  4. )
  5. echo 你的局域网IP是:
  6. echo %IP%
  7. pause>nul
复制代码

把 route print 的完整结果发出来,不要截图。

怎样把CMD窗口里的结果复制出来?
http://bbs.bathome.net/thread-3473-1-1.html
我帮忙写的代码不需要付钱。如果一定要给,请在微信群或QQ群发给大家吧。
【微信公众号、微信群、QQ群】http://bbs.bathome.net/thread-3473-1-1.html
【支持批处理之家,加入VIP会员!】http://bbs.bathome.net/thread-67716-1-1.html

TOP

回复 2# Batcher
  1. C:\Users\king\Desktop>for /F "tokens=4" %a in ('route print|findstr 0.0.0.0.*0.0.0.0') do (set IP=%a )
  2. C:\Users\king\Desktop>(set IP=192.168.0.127 )
  3. C:\Users\king\Desktop>(set IP=默认 )
  4. C:\Users\king\Desktop>echo 你的局域网IP是:
  5. 你的局域网IP是:
  6. C:\Users\king\Desktop>echo 默认
  7. 默认
  8. C:\Users\king\Desktop>pause1>nul
复制代码

TOP

  1. @echo off
  2. for /f "tokens=1,2,3 delims=={,}" %%a in ('wmic NICCONFIG where "IPEnabled='TRUE'" get DefaultIPGateway^,DNSServerSearchOrder^,IPAddress^,IPSubnet /value^|findstr "={"') do (
  3. if "%%a"=="DefaultIPGateway" (set "Gate=%%~b"
  4. ) else if "%%a"=="DNSServerSearchOrder" (set "DSN1=%%~b"&set "DSN2=%%~c"
  5. ) else if "%%a"=="IPAddress" (set "IP=%%~b"
  6. ) else if "%%a"=="IPSubnet" (set "Mask=%%~b")
  7. if defined Gate if defined Mask goto :show
  8. )
  9. :show
  10. echo;I P 地址: %IP%
  11. echo;子网掩码: %Mask%
  12. echo;默认网关: %Gate%
  13. echo;首选 DNS: %DSN1%
  14. echo;备用 DNS: %DSN2%
  15. pause
复制代码
QQ 33892006

TOP

回复 3# cidsxwf


    可能是我表达不清楚,打开一个CMD窗口,单独执行命令 route print 的完整结果发出来
我帮忙写的代码不需要付钱。如果一定要给,请在微信群或QQ群发给大家吧。
【微信公众号、微信群、QQ群】http://bbs.bathome.net/thread-3473-1-1.html
【支持批处理之家,加入VIP会员!】http://bbs.bathome.net/thread-67716-1-1.html

TOP

回复 5# Batcher
  1. Microsoft Windows [版本 10.0.17763.253]
  2. (c) 2018 Microsoft Corporation。保留所有权利。
  3. C:\Users\king>route print
  4. ===========================================================================
  5. 接口列表
  6. 15...ac d1 b8 2e 44 47 ......Realtek RTL8723BE Wireless LAN 802.11n PCI-E NIC
  7. 20...ae d1 b8 2e 44 47 ......Microsoft Wi-Fi Direct Virtual Adapter
  8. 14...ac d1 b8 2e 44 47 ......Microsoft Wi-Fi Direct Virtual Adapter #2
  9.   9...68 f7 28 ab 12 0a ......Intel(R) Ethernet Connection (3) I218-V
  10. 17...00 50 56 c0 00 01 ......VMware Virtual Ethernet Adapter for VMnet1
  11.   3...00 50 56 c0 00 08 ......VMware Virtual Ethernet Adapter for VMnet8
  12. 18...ac d1 b8 2e 44 48 ......Bluetooth Device (Personal Area Network)
  13.   1...........................Software Loopback Interface 1
  14. ===========================================================================
  15. IPv4 路由表
  16. ===========================================================================
  17. 活动路由:
  18. 网络目标        网络掩码          网关       接口   跃点数
  19.           0.0.0.0          0.0.0.0      192.168.0.1    192.168.0.112     35
  20.         127.0.0.0        255.0.0.0            在链路上         127.0.0.1    331
  21.         127.0.0.1  255.255.255.255            在链路上         127.0.0.1    331
  22.   127.255.255.255  255.255.255.255            在链路上         127.0.0.1    331
  23.       192.168.0.0    255.255.255.0            在链路上     192.168.0.112    291
  24.     192.168.0.112  255.255.255.255            在链路上     192.168.0.112    291
  25.     192.168.0.255  255.255.255.255            在链路上     192.168.0.112    291
  26.     192.168.237.0    255.255.255.0            在链路上     192.168.237.1    291
  27.     192.168.237.1  255.255.255.255            在链路上     192.168.237.1    291
  28.   192.168.237.255  255.255.255.255            在链路上     192.168.237.1    291
  29.     192.168.247.0    255.255.255.0            在链路上     192.168.247.1    291
  30.     192.168.247.1  255.255.255.255            在链路上     192.168.247.1    291
  31.   192.168.247.255  255.255.255.255            在链路上     192.168.247.1    291
  32.         224.0.0.0        240.0.0.0            在链路上         127.0.0.1    331
  33.         224.0.0.0        240.0.0.0            在链路上     192.168.0.112    291
  34.         224.0.0.0        240.0.0.0            在链路上     192.168.237.1    291
  35.         224.0.0.0        240.0.0.0            在链路上     192.168.247.1    291
  36.   255.255.255.255  255.255.255.255            在链路上         127.0.0.1    331
  37.   255.255.255.255  255.255.255.255            在链路上     192.168.0.112    291
  38.   255.255.255.255  255.255.255.255            在链路上     192.168.237.1    291
  39.   255.255.255.255  255.255.255.255            在链路上     192.168.247.1    291
  40. ===========================================================================
  41. 永久路由:
  42.   无
  43. IPv6 路由表
  44. ===========================================================================
  45. 活动路由:
  46. 接口跃点数网络目标                网关
  47.   1    331 ::1/128                  在链路上
  48.   9    291 fe80::/64                在链路上
  49. 17    291 fe80::/64                在链路上
  50.   3    291 fe80::/64                在链路上
  51.   3    291 fe80::b5d2:633f:e766:7765/128
  52.                                     在链路上
  53. 17    291 fe80::d927:d221:67cd:7621/128
  54.                                     在链路上
  55.   9    291 fe80::e46b:4050:4f36:7a14/128
  56.                                     在链路上
  57.   1    331 ff00::/8                 在链路上
  58.   9    291 ff00::/8                 在链路上
  59. 17    291 ff00::/8                 在链路上
  60.   3    291 ff00::/8                 在链路上
  61. ===========================================================================
  62. 永久路由:
  63.   无
  64. C:\Users\king>
复制代码

TOP

回复 4# yhcfsr
感谢大佬

TOP

回复 6# cidsxwf


没有看到“默认”这两个字,执行这个命令的结果呢:
  1. route print|findstr 0.0.0.0.*0.0.0.0
复制代码
我帮忙写的代码不需要付钱。如果一定要给,请在微信群或QQ群发给大家吧。
【微信公众号、微信群、QQ群】http://bbs.bathome.net/thread-3473-1-1.html
【支持批处理之家,加入VIP会员!】http://bbs.bathome.net/thread-67716-1-1.html

TOP

  1. for /f "tokens=4" %a in ('route print ^| find " 0.0.0.0 "') do echo %a
复制代码
你的 route print|findstr 0.0.0.0.*0.0.0.0 已经是错的了。而且还多了一个点

TOP

本帖最后由 ivor 于 2019-1-18 15:40 编辑

powershell
  1. (Test-Connection -ComputerName $env:ComputerName -Count 1).IPV4Address.IPAddressToString
复制代码
cmd
  1. powershell -c "(Test-Connection -ComputerName $env:ComputerName -Count 1).IPV4Address.IPAddressToString"
复制代码

TOP

本帖最后由 窄口牛 于 2019-1-18 16:56 编辑

回复 4# yhcfsr


     这个加上mac地址 计算机名 网卡名称呢?
  1. @echo off
  2. for /f "tokens=1,2,3,4,5,6 delims=={,}" %%a in ('wmic NICCONFIG where "IPEnabled='TRUE'" get Description^,MacAddress^,ServiceName^,DefaultIPGateway^,DNSServerSearchOrder^,IPAddress^,IPSubnet /value^|findstr "={"') do (
  3. if "%%a"=="Description" (set "Name=%%~b"
  4. ) else if "%%a"=="MacAddress" (set "MAC=%%~b"
  5. ) else if "%%a"=="ServiceName" (set "Chip=%%~b"
  6. ) else if "%%a"=="DefaultIPGateway" (set "Gate=%%~b"
  7. ) else if "%%a"=="DNSServerSearchOrder" (set "DSN1=%%~b"&set "DSN2=%%~c"
  8. ) else if "%%a"=="IPAddress" (set "IP=%%~b"
  9. ) else if "%%a"=="IPSubnet" (set "Mask=%%~b")
  10. if defined Gate if defined Mask goto :show
  11. )
  12. :show
  13. echo;网卡名称: %Name%
  14. echo;MAC 地址: %MAC%
  15. echo;网卡芯片: %Chip%
  16. echo;I P 地址: %IP%
  17. echo;子网掩码: %Mask%
  18. echo;默认网关: %Gate%
  19. echo;首选 DNS: %DSN1%
  20. echo;备用 DNS: %DSN2%
  21. pause
复制代码
照猫画虎,哪里有问题?

TOP

回复 11# 窄口牛
  1. @echo off
  2. for /f "tokens=1,2,3 delims=={,}" %%a in ('wmic NICCONFIG where "IPEnabled='TRUE'" get Description^,MacAddress^,ServiceName^,DefaultIPGateway^,DNSServerSearchOrder^,IPAddress^,IPSubnet /value^|findstr "="') do (
  3. if "%%a"=="Description" (set "Name=%%~b"
  4. ) else if "%%a"=="DefaultIPGateway" (set "Gate=%%~b"
  5. ) else if "%%a"=="DNSServerSearchOrder" (set "DSN1=%%~b"&set "DSN2=%%~c"
  6. ) else if "%%a"=="IPAddress" (set "IP=%%~b"
  7. ) else if "%%a"=="IPSubnet" (set "Mask=%%~b"
  8. ) else if "%%a"=="MACAddress" (set "MAC=%%~b"
  9. ) else if "%%a"=="ServiceName" (set "Chip=%%~b")
  10. if defined Gate if defined Chip goto :show
  11. )
  12. :show
  13. echo;网卡名称: %Name%
  14. echo;MAC 地址: %MAC%
  15. echo;网卡芯片: %Chip%
  16. echo;I P 地址: %IP%
  17. echo;子网掩码: %Mask%
  18. echo;默认网关: %Gate%
  19. echo;首选 DNS: %DSN1%
  20. echo;备用 DNS: %DSN2%
  21. pause
复制代码
QQ 33892006

TOP

明白了,谢谢!

TOP

回复  窄口牛
yhcfsr 发表于 2019-1-18 18:35



    大佬,您好,你的批处理如果是只连接了单有线网卡可以获取到,但wifi的网卡一起连接的时候,只能获取到WIFI的信息,能否把2个网卡的信息一起获取到?

TOP

返回列表