fjvip 发表于 2009-9-7 08:44:14

求助au3获取网卡名称,并给出变量

本帖最后由 fjvip 于 2009-9-7 11:14 编辑

下面一段代码是修改IP地址的,可是很遗憾的是,不够人性化,经常有时候因为摄像头驱动的影响,本地连接,经常名称是“本地连接 2”这样的名称,而下面,是针对“本地连接”修改IP的。所以不够灵活。希望高手能使用AU3获取网卡名称,传给一个变量值。

;修改IP
        GUICtrlSetData($Edit4_1, "2、修改IP地址为: " & $sdip & @CRLF, "1")
        Sleep(500)
        GUICtrlSetData($Edit4_1, "3、修改子网掩码为: " & $mask & @CRLF, "1")
        Sleep(500)
        GUICtrlSetData($Edit4_1, "4、修改网关为: " & $gateway & @CRLF, "1")
        RunWait('netsh interface ip set address name = "本地连接"source = static addr = ' & $sdip & ' mask = ' & $mask & ' gateway = ' & $gateway & ' gwmetric = 0', "c:\windows\system32", @SW_HIDE)
        GUICtrlSetData($Edit4_1, "5、修改DNS1为: " & $Dns & @CRLF, "1")
        RunWait('netsh interface ip set dns name = "本地连接"source = static addr = ' & $Dns & ' register = PRIMARY', "c:\windows\system32", @SW_HIDE)
        GUICtrlSetData($Edit4_1, "6、修改DNS2为: " & $Dns & @CRLF, "1")
        RunWait('netsh interface ip add dns name = "本地连接"addr = ' & $Dns & ' index=2', "c:\windows\system32", @SW_HIDE)

解决方法如下:(在这里感谢提供方法的朋友。谢谢。)

;获取网卡名称
        Dim $objwmiservice = ObjGet('winmgmts:\\localhost\root\CIMV2'), $netn
        $colitems = $objwmiservice.ExecQuery('SELECT * FROM Win32_NetworkAdapter', 'WQL', 0x10 + 0x20)
        If IsObj($colitems) Then
                For $objitem In $colitems
                        If $objitem.netconnectionid <> '' Then
                                $netn = $objitem.netconnectionid
                        EndIf
                Next
        EndIf
        ;修改IP
        GUICtrlSetData($Edit4_1, "2、修改IP地址为: " & $sdip & @CRLF, "1")
        Sleep(500)
        GUICtrlSetData($Edit4_1, "3、修改子网掩码为: " & $mask & @CRLF, "1")
        Sleep(500)
        GUICtrlSetData($Edit4_1, "4、修改网关为: " & $gateway & @CRLF, "1")
        RunWait('netsh interface ip set address name = ' & $netn & '   source = static addr = ' & $sdip & ' mask = ' & $mask & ' gateway = ' & $gateway & ' gwmetric = 0', "c:\windows\system32", @SW_HIDE)
        GUICtrlSetData($Edit4_1, "5、修改DNS1为: " & $Dns & @CRLF, "1")
        RunWait('netsh interface ip set dns name = ' & $netn & 'source = static addr = ' & $Dns & ' register = PRIMARY', "c:\windows\system32", @SW_HIDE)
        GUICtrlSetData($Edit4_1, "6、修改DNS2为: " & $Dns & @CRLF, "1")
        RunWait('netsh interface ip add dns name = ' & $netn & 'addr = ' & $Dns & ' index=2', "c:\windows\system32", @SW_HIDE)

fjvip 发表于 2009-9-7 08:46:41

以下是我使用批处理的解决方案
@echo off
FOR /F "tokens=2*" %%i IN ('ipconfig/all^|find /i "Ethernet adapter "') DO set name=%%j
FOR /F "tokens=1* delims=:" %%i in ("%name%") do set 网卡名称=%%i
netsh -c interface ip set address name="%网卡名称%" source=static addr=172.31.153.201 mask=255.255.0.0
netsh -c interface ip set address name="%网卡名称%" gateway=172.31.153.1 gwmetric=0
netsh -c interface ip add dns name="%网卡名称%" addr=218.85.157.99 index=1
netsh -c interface ip add dns name="%网卡名称%" addr=218.85.152.99 index=2

请高手帮忙使用AU3获取网卡名称变量。
页: [1]
查看完整版本: 求助au3获取网卡名称,并给出变量