标题: [系统相关] 如何用wmic命令获取当前正在使用的连网的有线网卡的ip地址 [打印本页]
作者: 小白龙 时间: 2025-1-7 12:44 标题: 如何用wmic命令获取当前正在使用的连网的有线网卡的ip地址
我想使用wmic命令获取当前正在使用的连网的有线网卡的IP地址,
我当前电脑上有一个无线网卡还有一个有线网卡, 有线网卡连的是互联网, 无线网卡连的是一个LED屏, 两个网卡都有IP地址, 我只想获取有线网卡的IP地址, 下面的命令能获取到两个网卡, 但是IP怎样获取呢, wmic太强大, 应该可以实现, 求路过大佬支招- wmic nic where "NetConnectionStatus=2" get NetConnectionID /value
复制代码
作者: 小白龙 时间: 2025-1-7 13:08
本帖最后由 小白龙 于 2025-1-7 13:10 编辑
下面这个是ai写出来的, 但是无效- wmic nic where "NetConnectionStatus=2 AND Name NOT LIKE '%Wireless%'" get Index, NetConnectionID /value
复制代码
获取到Index后,然后再用下面的, 感觉这两行, 应该可以用一行代码实现- wmic nicconfig where "Index=1" get IPAddress
复制代码
作者: 小白龙 时间: 2025-1-7 14:57
怎样把下面这两行并为一行啊, 第二行需要第1行获取的index- wmic nic where "NetConnectionStatus=2 and not Name like '%Wireless%'" get Index /value
- wmic nicconfig where "Index=1" get IPAddress /value
复制代码
作者: flashercs 时间: 2025-1-7 15:41
- wmic nic where "NetConnectionStatus=2 and Not Name like '%Wireless%'" assoc:list /resultclass:Win32_NetworkAdapterConfiguration|findstr /i "IPAddress"
复制代码
作者: 小白龙 时间: 2025-1-7 16:02
回复 4# flashercs
大佬实在是高啊, gpt和gemini问了几十轮都无解,
有个不太完美的地方, 下面是结果, 但我只需要ipv4的值, 在不用for取值的情况下, 还有解吗? 也不想用powershell太慢了
IPAddress={"192.168.1.9","fe80::44be:cc06:966c:a098"}
作者: buyiyang 时间: 2025-1-7 20:58
回复 5# 小白龙 - wmic /namespace:\\root\standardcimv2 path MSFT_NetIPInterface where "Dhcp=1 and ConnectionState=1 and AddressFamily=2" assoc:value /resultclass:MSFT_NetIPAddress | findstr /ib "IPv4Address"
复制代码
作者: 小白龙 时间: 2025-1-8 19:12
本帖最后由 小白龙 于 2025-1-8 19:28 编辑
回复 6# buyiyang
多谢大佬支招, 可以在cmd窗口中执行并获取结果, 但是如果我在命令前面添加上 cmd /c 就不能执行了, 而四楼大佬的就可以执行, 能帮看一下吗?
之所以要加 cmd /c 是因为我用的程序仅支持这种方式来调用cmd命令
感谢~
还有个问题, 命令里的这个 Dhcp=1 条件有限制, 因为有时我的IP没有用DHCP, 是固定的IP
作者: flashercs 时间: 2025-1-8 21:05
本帖最后由 flashercs 于 2025-1-8 21:09 编辑
回复 5# 小白龙
外部程序调用- cmd /c cmd /c for /f "tokens=2 delims=={}" %A in ('wmic nic where "NetConnectionStatus=2 and Not Name like '%Wireless%'" assoc:list /resultclass:Win32_NetworkAdapterConfiguration^^^|findstr /i "IPAddress"') do (for %B in (%A) do @echo %~B)|findstr /v ":"
复制代码
6楼很可能这样- cmd /c wmic /namespace:\\\\root\\standardcimv2 path MSFT_NetIPInterface where " ConnectionState=1 and AddressFamily=2" assoc:value /resultclass:MSFT_NetIPAddress | findstr /ib "IPv4Address"
复制代码
作者: 小白龙 时间: 2025-1-8 21:19
回复 8# flashercs
试了, 都不行
作者: czjt1234 时间: 2025-1-10 09:07
你弄个批处理文件,比如 c:\1.bat
然后
cmd /c c:\1.bat
欢迎光临 批处理之家 (http://bbs.bathome.net/) |
Powered by Discuz! 7.2 |