zhenglei 发表于 2008-10-17 00:33:59

如何获取本机所有网卡IP信息及网卡名字

#include <GUIConstants.au3>
#include <Array.au3>
#Region ### START Koda GUI section ### Form=c:\documents and settings\cface\桌面\222.kxf
;Opt("TrayIconHide", 1)
Opt("TrayMenuMode",1)

#include <GUIConstantsEx.au3>

Opt('MustDeclareVars', 1)

dim $ret="" , $bt11,$nMsg,$input,$s,$s1 ,$ttt=22 , $i=0,$wk1, $card1,$card2

getipinfo()


        Local $msg
        GUICreate("My GUI combo"); will create a dialog box that when displayed is centered
        GUICtrlCreateLabel("请选择网克使用网卡:",10,30)
        $input=GUICtrlCreateCombo("", 10, 50,260,20) ; create first item
        GUICtrlSetData($input, $card1&"|"&$card2) ; add other item snd set a new default
    $bt11=GUICtrlCreateButton("测试",100,160)
        GUISetState()
        $s=GUICtrlRead($input)
   ; MsgBox(64,"asd",$s)
        ; Run the GUI until the dialog is closed
        ;While 1
        ;        $msg = GUIGetMsg()
        ;        If $msg = $GUI_EVENT_CLOSE Then ExitLoop
;Case $bt11
;$rd1 = GUICtrlRead($xuni1)

;        WEnd

While 1
        $nMsg = GUIGetMsg()
        Select
                Case $nMsg = $GUI_EVENT_CLOSE
                        Exit
                Case $nMsg = $bt11
$s1=GUICtrlRead($input)
;MsgBox(64,"网卡检测:",$s1)
                        EndSelect
                        WEnd







Func getipinfo()   ;获取本机网卡全部信息
Local $wbemFlagReturnImmediately,$wbemFlagForwardOnly,$deip,$demask,$dedns,$dedns2,$degete,$dename
Local $network,$strComputer,$objWMIService,$colItems,$Output,$strIPAddress,$strDNSServerSearchOrder,$strIPSubnet
$wbemFlagReturnImmediately = 0x10
$wbemFlagForwardOnly = 0x20
$colItems = ""
$strComputer = "localhost"

$Output=""
$Output = $Output & "计 算 机:" & $strComputer& @CRLF
$Output = $Output & "==========================================" & @CRLF
$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled != 0", "WQL", _
                                          $wbemFlagReturnImmediately + $wbemFlagForwardOnly)
$i=0
If IsObj($colItems) then
   For $objItem In $colItems
      $Output = $Output & "网    卡: " & $objItem.Description & @CRLF
      ;$strDefaultIPGateway = $objItem.DefaultIPGateway(0)
      $Output = $Output & "默认网关: " & $objItem.DefaultIPGateway(0) & @CRLF
   

      $Output = $Output & "计算机名: " & $objItem.DNSHostName & @CRLF
      $strIPAddress = $objItem.IPAddress(0)
      $Output = $Output & "IP地址: " & $strIPAddress & @CRLF
   $strDNSServerSearchOrder = $objItem.DNSServerSearchOrder(0)
      $Output = $Output & "默认 DNS: " & $strDNSServerSearchOrder & @CRLF
          $strDNSServerSearchOrder = $objItem.DNSServerSearchOrder(1)
      $Output = $Output & "备用 DNS: "& $strDNSServerSearchOrder & @CRLF
         
      $strIPSubnet = $objItem.IPSubnet(0)
      $Output = $Output & "子网掩码: " & $strIPSubnet & @CRLF
      $Output = $Output & "M AC地址: " & $objItem.MACAddress & @CRLF
      ;if Msgbox(1,"WMI获取的信息:",$Output) = 2 then ExitLoop
      $Output=""

$deip=$strIPAddress
$demask=$strIPSubnet
$dedns=$objItem.DNSServerSearchOrder(0)
$dedns2=$objItem.DNSServerSearchOrder(1)
$degete=$objItem.DefaultIPGateway(0)
$dename=$objItem.DNSHostName

          if $i =1 then
             ;MsgBox(64,"测试",$network)
               $card2=$objItem.Description
                          ;MsgBox(64,"card2",$card2)
                       Else
               $card1=$objItem.Description&"1"
         $i =1               
         $card2='无网卡信息'                              
       EndIf
   MsgBox(64,"netcardname",$strIPAddress)

       
Next
          MsgBox(64,"card1",$card1)
       MsgBox(64,"card2",$card2)       
Endif
EndFunc

以上代码可以进行多网卡IP信息 及 多网卡名称获取,,,但存在一个问题就是如果你的机器是一个网卡的话。以上代码会把你的机器网卡认为2快。。 而二快网卡却又是一摸一样的。。 也就把同一网卡认了俩遍。。。

代码错误处:


if $i =1 then
             ;MsgBox(64,"测试",$network)
               $card2=$objItem.Description
                          ;MsgBox(64,"card2",$card2)
                       Else
               $card1=$objItem.Description&"1"
         $i =1               
         $card2='无网卡信息'                              
       EndIf
   MsgBox(64,"netcardname",$strIPAddress)
       
Next

如果将 以下代码放于 NEXT后 则单网卡机器检测一切正常。。 缺点就是不能应用于多网卡的机器了。。达不到我们的要求...
if $i =1 then
             ;MsgBox(64,"测试",$network)
               $card2=$objItem.Description
                          ;MsgBox(64,"card2",$card2)
                       Else
               $card1=$objItem.Description&"1"
         $i =1               
         $card2='无网卡信息'                              
       EndIf
   MsgBox(64,"netcardname",$strIPAddress)


               请高手们出来指点一下吧。 谢谢。。。 另外就以上代码。。 如果要给多网卡的 某个网卡修改IP如果修改啊。。

[ 本帖最后由 zhenglei 于 2008-10-23 12:39 编辑 ]

zhenglei 发表于 2008-10-17 00:39:25

问题已解决

问题已解决

itljl 发表于 2008-10-17 00:49:10

如何解决的?

zhenglei 发表于 2008-10-17 01:14:18

在顶一下

在顶一下....顶到高手出现!!~~

liongodmien 发表于 2008-10-17 11:34:46

我是单网卡的,用LZ的代码测试了下,没有出现有两块一样网卡的情形!

netegg 发表于 2008-10-17 12:16:20

走api枚举一下应给就行了吧

liongodmien 发表于 2008-10-17 12:34:52

我没有多网卡试,改了下,不知道行不行:

#include <GUIConstants.au3>
#include <Array.au3>
#Region ### START Koda GUI section ### Form=c:\documents and settings\cface\桌面\222.kxf
;Opt("TrayIconHide", 1)
Opt("TrayMenuMode", 1)

#include <GUIConstantsEx.au3>

Opt('MustDeclareVars', 1)

Dim $ret = "", $bt11, $nMsg, $input, $s, $s1, $ttt = 22, $i = 0, $wk1, $card, $setcard = ''

getipinfo()


Local $msg
GUICreate("My GUI combo") ; will create a dialog box that when displayed is centered
GUICtrlCreateLabel("请选择网克使用网卡:", 10, 30)
For $i = 1 To $card
        $setcard &= $card[$i] & '|'
Next
$input = GUICtrlCreateCombo('请选择一块网卡', 10, 50, 380, 20) ; create first item
GUICtrlSetData($input, $setcard) ; add other item snd set a new default
$bt11 = GUICtrlCreateButton("测试", 100, 160, 50, 30)
GUISetState()
$s = GUICtrlRead($input)

While 1
        $nMsg = GUIGetMsg()
        Select
                Case $nMsg = $GUI_EVENT_CLOSE
                        Exit
                Case $nMsg = $bt11
                        $s1 = GUICtrlRead($input)
                        ;MsgBox(64,"网卡检测:",$s1)
        EndSelect
WEnd

Func getipinfo() ;获取本机网卡全部信息
        Local $wbemFlagReturnImmediately, $wbemFlagForwardOnly, $deip, $demask, $dedns, $dedns2, $degete, $dename
        Local $network, $strComputer, $objWMIService, $colItems, $Output, $strIPAddress, $strDNSServerSearchOrder, $strIPSubnet
        $wbemFlagReturnImmediately = 0x10
        $wbemFlagForwardOnly = 0x20
        $colItems = ""
        $strComputer = "localhost"

        $Output = ""
        $Output = $Output & "计 算 机:" & $strComputer & @CRLF
        $Output = $Output & "==========================================" & @CRLF
        $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
        $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled != 0", "WQL", _
                        $wbemFlagReturnImmediately + $wbemFlagForwardOnly)
       
        Local $check2 = '', $check1 = ''
        $card = 0
        If IsObj($colItems) Then
                For $objItem In $colItems
                        $Output = $Output & "网    卡: " & $objItem.Description & @CRLF
                        ;$strDefaultIPGateway = $objItem.DefaultIPGateway(0)
                        $Output = $Output & "默认网关: " & $objItem.DefaultIPGateway(0) & @CRLF

                        $Output = $Output & "计算机名: " & $objItem.DNSHostName & @CRLF
                        $strIPAddress = $objItem.IPAddress(0)
                        $Output = $Output & "IP地址: " & $strIPAddress & @CRLF
                        $strDNSServerSearchOrder = $objItem.DNSServerSearchOrder(0)
                        $Output = $Output & "默认 DNS: " & $strDNSServerSearchOrder & @CRLF
                        $strDNSServerSearchOrder = $objItem.DNSServerSearchOrder(1)
                        $Output = $Output & "备用 DNS: " & $strDNSServerSearchOrder & @CRLF

                        $strIPSubnet = $objItem.IPSubnet(0)
                        $Output = $Output & "子网掩码: " & $strIPSubnet & @CRLF
                        $Output = $Output & "M AC地址: " & $objItem.MACAddress & @CRLF
;~                         if Msgbox(1,"WMI获取的信息:",$Output) = 2 then ExitLoop
                        $Output = ""
                        $check1 = $objItem.MACAddress
                        If $check1 = $check2 Then
                                If $check1 = '' Then Return MsgBox(64, '', '无网卡')
                                ExitLoop
                        EndIf
                        $check2 = $check1
                        $card += 1
                       
                        $deip = $strIPAddress
                        $demask = $strIPSubnet
                        $dedns = $objItem.DNSServerSearchOrder(0)
                        $dedns2 = $objItem.DNSServerSearchOrder(1)
                        $degete = $objItem.DefaultIPGateway(0)
                        $dename = $objItem.DNSHostName

                        $card[$card] = $objItem.Description & $card
                        MsgBox(64, "netcardname", $strIPAddress)


                Next
                For $i = 1 To $card
                        MsgBox(64, "card"&$i, $card[$i])
                Next
        EndIf
EndFunc   ;==>getipinfo


[ 本帖最后由 liongodmien 于 2008-10-17 12:46 编辑 ]

番茄 发表于 2008-10-17 12:36:35


#include <GUIConstants.au3>
#include <Array.au3>
#Region ### START Koda GUI section ### Form=c:\documents and settings\cface\桌面\222.kxf
;Opt("TrayIconHide", 1)
Opt("TrayMenuMode", 1)

#include <GUIConstantsEx.au3>

Opt('MustDeclareVars', 1)

Dim $ret = "", $bt11, $nMsg, $input, $s, $s1, $ttt = 22, $i = 0, $wk1, $card, $setcard = ''

getipinfo()


Local $msg
GUICreate("My GUI combo") ; will create a dialog box that when displayed is centered
GUICtrlCreateLabel("请选择网克使用网卡:", 10, 30)
For $i = 1 To $card
    $setcard &= $card[$i] & '|'
Next
$input = GUICtrlCreateCombo('请选择一块网卡', 10, 50, 380, 20) ; create first item
GUICtrlSetData($input, $setcard) ; add other item snd set a new default
$bt11 = GUICtrlCreateButton("测试", 100, 160, 50, 30)
GUISetState()
$s = GUICtrlRead($input)

While 1
    $nMsg = GUIGetMsg()
    Select
      Case $nMsg = $GUI_EVENT_CLOSE
            Exit
      Case $nMsg = $bt11
            $s1 = GUICtrlRead($input)
            MsgBox(64,"网卡检测:",$s1)
    EndSelect
WEnd

Func getipinfo() ;获取本机网卡全部信息
    Local $wbemFlagReturnImmediately, $wbemFlagForwardOnly, $deip, $demask, $dedns, $dedns2, $degete, $dename
    Local $network, $strComputer, $objWMIService, $colItems, $Output, $strIPAddress, $strDNSServerSearchOrder, $strIPSubnet
    $wbemFlagReturnImmediately = 0x10
    $wbemFlagForwardOnly = 0x20
    $colItems = ""
    $strComputer = "localhost"

    $Output = ""
    $Output = $Output & "计 算 机:" & $strComputer & @CRLF
    $Output = $Output & "==========================================" & @CRLF
    $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
    $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled != 0", "WQL", _
            $wbemFlagReturnImmediately + $wbemFlagForwardOnly)
   
    Local $check2 = '', $check1 = '', $counter = 0
    If IsObj($colItems) Then
      For $objItem In $colItems
            $Output = $Output & "网    卡: " & $objItem.Description & @CRLF
            ;$strDefaultIPGateway = $objItem.DefaultIPGateway(0)
            $Output = $Output & "默认网关: " & $objItem.DefaultIPGateway(0) & @CRLF

            $Output = $Output & "计算机名: " & $objItem.DNSHostName & @CRLF
            $strIPAddress = $objItem.IPAddress(0)
            $Output = $Output & "IP地址: " & $strIPAddress & @CRLF
            $strDNSServerSearchOrder = $objItem.DNSServerSearchOrder(0)
            $Output = $Output & "默认 DNS: " & $strDNSServerSearchOrder & @CRLF
            $strDNSServerSearchOrder = $objItem.DNSServerSearchOrder(1)
            $Output = $Output & "备用 DNS: " & $strDNSServerSearchOrder & @CRLF

            $strIPSubnet = $objItem.IPSubnet(0)
            $Output = $Output & "子网掩码: " & $strIPSubnet & @CRLF
            $Output = $Output & "M AC地址: " & $objItem.MACAddress & @CRLF
            if Msgbox(1,"WMI获取的信息:",$Output) = 2 then ExitLoop
            $Output = ""
            $check1 = $objItem.MACAddress
            If $check1 = $check2 Then
                If $check1 = '' Then Return MsgBox(64, '', '无网上卡')
                ExitLoop
            EndIf
            $check2 = $check1
            $counter += 1
            $card += 1
            
            $deip = $strIPAddress
            $demask = $strIPSubnet
            $dedns = $objItem.DNSServerSearchOrder(0)
            $dedns2 = $objItem.DNSServerSearchOrder(1)
            $degete = $objItem.DefaultIPGateway(0)
            $dename = $objItem.DNSHostName

            $card[$counter] = $objItem.Description & $counter
            MsgBox(64, "netcardname", $strIPAddress)


      Next
      For $i = 1 To $counter
            MsgBox(64, "card"&$i, $card[$i])
      Next
    EndIf
EndFunc   ;==>getipinfo


测试了下,我单网卡,显示还是2块网卡。

[ 本帖最后由 番茄 于 2008-10-17 12:39 编辑 ]

liongodmien 发表于 2008-10-17 12:51:11


测试了下,我单网卡,显示还是2块网卡。

由 番茄 于 2008-10-17 12:39
那我就没办法了,我在自己的机子上反复的试,除了预设的那一句话,就只有一个网卡...

dgsf520 发表于 2008-10-21 10:47:05

原帖由 liongodmien 于 2008-10-17 12:34 发表 http://www.autoitx.com/images/common/back.gif
我没有多网卡试,改了下,不知道行不行:

#include
#include
#Region ### START Koda GUI section ### Form=c:\documents and settings\cface\桌面\222.kxf
;Opt("TrayIconHide", 1)
Opt("TrayMenuMod ...
按你这样的写的话运行没有反应。

s20305 发表于 2008-10-21 14:05:14

我这也是一块啊,呵呵!

redapple2008 发表于 2008-11-12 11:24:17

我的电脑上一块网卡,一个也不显示。晕

wxlj 发表于 2009-3-10 19:53:09

参观…………………………

lxmx 发表于 2009-4-8 23:21:38

嗯嗯,实现了,效果不错。

xz00311 发表于 2009-9-29 17:42:13

列表获取啊
页: [1] 2
查看完整版本: 如何获取本机所有网卡IP信息及网卡名字