找回密码
 加入
搜索
查看: 3306|回复: 4

[网络通信] [已解决]如何获取指定名称的网卡的IP地址?

[复制链接]
发表于 2016-2-24 19:17:39 | 显示全部楼层 |阅读模式
本帖最后由 xia_xia 于 2016-2-26 19:41 编辑

比如,在机器中,有一固定网络连接:“无线网络连接”  那么如何获取这个连接的IP地址呢?
发表于 2016-2-24 19:37:58 | 显示全部楼层
本帖最后由 afan 于 2016-2-24 19:39 编辑

活动的外网ip应该只有一个吧,直接在网页查看是不是你要的,比如查看
发表于 2016-2-25 08:46:49 | 显示全部楼层
回复 1# xia_xia
这样试试:

#include <AutoItConstants.au3>
#include <Array.au3>
#include <JY_IPget.au3>
Global Const $__WinINet_sDLL = "wininet.dll"
Global $__WinINet_hDLL = $__WinINet_sDLL
Global $wangka1, $mac, $DNS1
If _WinINet_InternetGetConnectedState() Then
        MsgBox(0, True, "己联网!!!",3)
                _WinINet_InternetGetConnectedStateaa()
Else
        MsgBox(0, False, "断网!!!",5)
EndIf

Exit

Func _WinINet_InternetGetConnectedState()
        Local $tFlags = DllStructCreate("dword")
        Local $avResult = DllCall($__WinINet_hDLL,"bool", "InternetGetConnectedState", "ptr", DllStructGetPtr($tFlags),"dword", 0 )
        If @error Then Return SetError(1, 0, False)
        Return SetError(0, DllStructGetData($tFlags, 1), $avResult[0] <> 0)
EndFunc   ;==>_WinINet_InternetGetConnectedState

Func _WinINet_InternetGetConnectedStateaa()
        WMI()
                For $i = 0 To UBound($wangka1) - 1
                        For $o = 0 To UBound($mac) - 1
                        Local $Chua4 = StringTrimLeft($DNS1[$o], 1)                                       
                        Next
                Next
                $IP = JY_ipGet()
                MsgBox(0, "网络地址:","外网IP地址: "&$IP &@CRLF &"无线网络IP:"&$Chua4)       
EndFunc   ;==>
Func WMI()
        Local $dnsa1
        $sMesStr = ""
        $net = ""
        $oLocator = ObjCreate("WbemScripting.SWbemLocator")
        $oService = $oLocator.ConnectServer
        $oClassSet = $oService.ExecQuery("Select * From Win32_NetworkAdapter Where NetConnectionID IS NOT NULL")       
        For $oClass In $oClassSet
                If @error Then ExitLoop
                $sMesStr = $sMesStr & "连接名:“" & $oClass.NetConnectionID() & "”" & @CRLF & _
                                "网卡名: “" & $oClass.Name & "”" & @CRLF
        Next               
        $oClassSet = ""
        $oClass = ""
        $oService = ""
        $oLocator = ""
        $objWMIService = ObjGet("winmgmts:\\localhost\root\CIMV2")
        $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled != 0", "WQL", 0x10 + 0x20)
        For $objItem In $colItems
                ;"网卡名:" & $objItem.Description & "”" & @CRLF & _
                If @error Then ExitLoop
                $net = $net & "ip地址: “:" & $objItem.IPAddress(0) & "”" & @CRLF & _
                                "mac地址: “" & $objItem.MACAddress & "”" & @CRLF & _
                                "首选dns: “:" & $objItem.DNSServerSearchOrder(0) & "”" & @CRLF ;& _                                               
        Next
        $wangka1 = StringRegExp($sMesStr, "(?<=网卡名: “)[^”]+", 3)
        $DNS1 = StringRegExp($net, "(?<=首选dns: “)[^”]+", 3)
        $mac = StringRegExp($net, "(?<=mac地址: “)[^”]+", 3)
EndFunc   ;==>WMI
发表于 2016-2-25 10:19:00 | 显示全部楼层
试试WMI,似乎可以再简化
$Net_AdapterName = "无线网络连接";网络连接名称
$Net_IPaddress ="";IP
$adapter_index = "";网卡唯一标示索引
$objWMIService = ObjGet("winmgmts:\\localhost\root\CIMV2")
$Net_adapter = $objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapter where Netenabled = true")
If IsObj($Net_adapter) Then
        For $objItem1 In $Net_adapter
                If $objItem1.netconnectionid = $Net_AdapterName Then $adapter_index = $objItem1.index;Win32_NetworkAdapter 获取网络连接名称为XX的唯一标示索引
        Next
EndIf

$Net_adapterconfig = $objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration where IPEnabled = True")
If IsObj($Net_adapterconfig) Then
        For $objItem2 In $Net_adapterconfig
                If $objItem2.index =$adapter_index Then $Net_IPaddress = $objItem2.IPAddress(0)
        Next
EndIf
MsgBox(0,$Net_AdapterName,$Net_IPaddress)
 楼主| 发表于 2016-2-26 19:40:45 | 显示全部楼层
谢谢,虽然看的不是太明白,但是能用了,很好使。谢谢qqgghh1。
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-12-22 17:17 , Processed in 0.080959 second(s), 23 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表