【已解决】求教列出系统启用中的网卡并排除虚拟网卡的方法
本帖最后由 lon91ong 于 2012-8-5 10:56 编辑如题,请教查询系统启用状态的非虚拟的网卡名称的AU3代码
望大侠不吝赐教! 获取所有网卡 论坛上有方法了 至于是不是虚拟网卡 这个可以试图从驱动文件和mac地址来判断
一定要现成代码就…… 本帖最后由 fpquenya 于 2012-8-4 22:38 编辑
回复 1# lon91ong
以前写的一个代码,供你参考.
;-------------------------
;WMI方法查询非系统隐藏网卡信息(包含HyperV/WMWare服务器虚拟化的网卡)
;关键是NetConnectionID名来判断;
;网上通过PhysicalAdapter来判断的话有系统限制,xp或win server 2003不支持.
;-------------------------
MsgBox(0,"网卡信息",get_network_info())
Func get_network_info()
Local $sNetInfo = ""
;查询本机网卡信息
Local $oWMI = ObjGet("winmgmts:\\.\root\cimv2")
Local $colNet= $oWMI.ExecQuery("SELECT * FROM Win32_NetworkAdapter")
Local $oNet
Local $colConfig
For $oNet In $colNet
If $oNet.NetConnectionID <> "" Then ;关键的一句,有NetConnectionID名的就不是系统虚拟网卡! (物理网卡/HyperV虚拟网卡,非系统隐藏网卡)
$sNetInfo &= @CRLF & "网卡名称:" & $oNet.Name & @CRLF & _
"网卡索引号:" & $oNet.Index & @CRLF & _
"PNP设备ID:" & $oNet.PnPDeviceID & @CRLF
;得到网卡状态
Local $iStatus = $oNet.NetConnectionStatus
Local $sStatus = ""
Switch $iStatus
Case 0,7
$sStatus = "断开"
Case 2
$sStatus = "连通"
Case 4,5
$sStatus = "硬件不存在或禁用"
Case 1
$sStatus = "正在连接"
Case 3
$sStatus = "正在断开"
Case 6
$sStatus = "硬件故障"
Case 8
$sStatus = "正在验证"
Case 9
$sStatus = "验证成功"
Case 10
$sStatus = "验证失败"
Case 11
$sStatus = "非法地址"
Case 12
$sStatus = "要求凭证"
EndSwitch
$sNetInfo &= "网卡状态:" & $sStatus & @CRLF
If _bIsWin7Win2008() = True Then ;2008/Win7/2008R2系统才支持PhysicalAdapter属性
If $oNet.PhysicalAdapter = True Then
$sNetInfo &= "物理网卡:是" & @CRLF
Else
$sNetInfo &= "物理网卡:否" & @CRLF
EndIf
EndIf
$sNetInfo &= "MAC和IP信息:" & @CRLF
$colConfig = $oWMI.ExecQuery _
("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE Index='" & $oNet.DeviceID & "'") ;属性太多,用*
Local $oConfig
Local $sIP,$sSubnet,$sGateway,$sDNSServer
Local $sIP_Sum = ""
Local $sDNSServer_sum = ""
For $oConfig In $colConfig ;网络配置也是集合
If $oConfig.IPAddress <> "" Then ;防止IP为Null报错
For $sIP In $oConfig.IPAddress ;一块网卡IP地址可能有多个,是集合
$sNetInfo &= @TAB & "IP地址:" & $sIP & @CRLF
Next
EndIf
If $oConfig.IPSubnet <> "" Then ;掩码也有多个,防止Null报错(同一块网卡即使多IP,但掩码都相同)
For $sSubnet In $oConfig.IPSubnet
$sNetInfo &= @TAB & "子网掩码:" & $sSubnet & @CRLF
Next
EndIf
If $oConfig.DefaultIPGateway <> "" Then ;默认网关也有多个,防止Null报错(同一块网卡即使多IP,但默认网关都相同)
For $sGateway In $oConfig.DefaultIPGateway
$sNetInfo &= @TAB & "默认网关:" & $sGateway & @CRLF
Next
EndIf
If $oConfig.DHCPEnabled = True And $oConfig.IPEnabled = True Then ;一块网卡上的DHCP服务器是唯一的
$sNetInfo &= @TAB & "DHCP服务器:" & $oConfig.DHCPServer & @CRLF & _
@TAB & "DHCP租约获取时间:" & _FormatDateTime($oConfig.DHCPLeaseObtained) & @CRLF & _
@TAB & "DHCP租约过期时间:" & _FormatDateTime($oConfig.DHCPLeaseExpires) & @CRLF
EndIf
If $oConfig.DNSServerSearchOrder <> "" Then ;DNS服务器也有多个,防止Null报错
For $sDNSServer In $oConfig.DNSServerSearchOrder
$sNetInfo &= @TAB & "DNS服务器:" & $sDNSServer & @CRLF
Next
EndIf
If $oConfig.WINSPrimaryServer <> "" Then ;唯一
$sNetInfo &= @TAB & "主WINS服务器:" & $oConfig.WINSPrimaryServer & @CRLF
EndIf
If $oConfig.WINSSecondaryServer <> "" Then ;唯一
$sNetInfo &= @TAB & "备WINS服务器:" & $oConfig.WINSSecondaryServer & @CRLF
EndIf
Next
EndIf
Next
Return $sNetInfo
EndFunc
Func _bIsWin7Win2008()
Local $oWMI = ObjGet("winmgmts:\\.\root\cimv2")
Local $bIsWin7Win2008 = False
Local $colOS = $oWMI.InstancesOf("Win32_OperatingSystem")
Local $sOSVersion
For $oFoundItem In $colOS
$sOSVersion = $oFoundItem.Version
ExitLoop
Next
If StringLeft($sOSVersion,1) = "6" Then $bIsWin7Win2008 = True
Return $bIsWin7Win2008
EndFunc
Func _FormatDateTime($sDateTime)
Local $sFormatedDateTime = ""
If StringLeft($sDateTime,8) <> 0 And StringLeft($sDateTime,8) <> "" Then
$sFormatedDateTime = StringLeft($sDateTime,4) & "/" & StringMid($sDateTime,5,2) & "/" & StringMid($sDateTime,7,2) & " "
EndIf
If StringMid($sDateTime,9,6) <> 0 And StringMid($sDateTime,9,6) <> ""Then
$sFormatedDateTime = $sFormatedDateTime & StringMid($sDateTime,9,2) & ":" & StringMid($sDateTime,11,2) & ":" & StringMid($sDateTime,13,2)
EndIf
Return $sFormatedDateTime
EndFunc ;<==格式化输出日期和时间
本帖最后由 lon91ong 于 2012-8-4 22:50 编辑
回复 3# fpquenya
多谢大侠的代码,还得好好消化一下!
还有就是我有一点说错了, 我想要的是网络连接名称, 不是网卡名!
应该修改哪块呢?? 回复 4# lon91ong
象'本地连接'或'无线连接'这种名称的话,就是NetConnectionID 回复 5# fpquenya
明白了, 多谢指点! 回复 5# fpquenya
昨天没有测试,直接把贴子在改为[已解决]
刚刚测试了一下,结果如下:
红框部分的这两个虚拟网卡不能排除 回复 7# lon91ong
前面我说过,不包含系统隐藏网卡,可能包含虚拟化的网卡,可以将上面代码改一下:
If $oNet.NetConnectionID <> "" And StringInStr($oNet.Name,"virtual") = 0 Then
回复 8# fpquenya
多谢热心的大侠解答!
还有点疑惑的是,我用的是64位Win7旗舰系统,怎么显示的那两个虚拟网卡的"物理网卡"属性为"是"呢? 回复 9# lon91ong
对的,PhysicalAdapter我也发现经常判断不准哈. 回复 1# lon91ong
贴出解决方法以供他人参考
最后采用的解决方法
代码如下:Local $sNetInfo = ""
;查询本机网卡信息
Local $oWMI = ObjGet("winmgmts:\\.\root\cimv2")
Local $colNet= $oWMI.ExecQuery("Select * From Win32_NetworkAdapter Where NetConnectionID IS NOT NULL")
Local $oNet,$colConfig
For $oNet In $colNet
If $oNet.NetConnectionStatus == 4 Or $oNet.NetConnectionStatus == 5 Then ContinueLoop ;排除非活动状态的网卡
If $oNet.NetConnectionID == "" Or StringInStr($oNet.name,"virtual") Then ContinueLoop ;排除虚拟网卡
$sNetInfo &= $oNet.NetConnectionID & @CRLF
Next
MsgBox(0,"全部",$sNetInfo)
页:
[1]