找回密码
 加入
搜索
查看: 3960|回复: 7

[网络通信] 求助IP修改时获取网卡名称为"本地连接 2",时不正常(解决)

[复制链接]
发表于 2009-9-13 18:57:26 | 显示全部楼层 |阅读模式
本帖最后由 fjvip 于 2009-9-13 21:28 编辑

;求助IP修改时获取网卡名称为"本地连接 2",时不正常。通过试验表明以下代码只要名称中不含空格都可以正常获取网卡变量名称,然后修改。可是如果网卡名称为“本地连接 2”空格 2的时候,这时取到了变量了,但也无法完成修改,请高手指教。下列的'netsh interface ip add dns name =' &$netsh& ' 这样表达有什么不正确的地方?


        ;获取网卡名称
        Dim $objwmiservice = ObjGet('winmgmts:\\localhost\root\CIMV2'), $netsh
        $colitems = $objwmiservice.ExecQuery('SELECT * FROM Win32_NetworkAdapter', 'WQL', 0x10 + 0x20)
        If IsObj($colitems) Then
                For $objitem In $colitems
                        If $objitem.netconnectionid <> '' Then
                                $netsh = $objitem.netconnectionid
                        EndIf
                Next
        EndIf
        ;修改IP
        GUICtrlSetData($Edit4_1, "2、当前网卡名称: " & $netsh & @CRLF, "1")
        Sleep(500)
        GUICtrlSetData($Edit4_1, "3、修改IP地址为: " & $sdip & @CRLF, "1")
        Sleep(500)
        GUICtrlSetData($Edit4_1, "4、修改子网掩码为: " & $mask & @CRLF, "1")
        Sleep(500)
        GUICtrlSetData($Edit4_1, "5、修改网关为: " & $gateway & @CRLF, "1")
        RunWait('netsh interface ip set address name =' &$netsh& '  source = static addr = ' & $sdip & ' mask = ' & $mask & ' gateway = ' & $gateway & ' gwmetric = 0', "c:\windows\system32", @SW_HIDE)
        GUICtrlSetData($Edit4_1, "6、修改DNS1为: " & $Dns[1] & @CRLF, "1")
        RunWait('netsh interface ip set dns name =' &$netsh& '  source = static addr = ' & $Dns[1] & ' register = PRIMARY', "c:\windows\system32", @SW_HIDE)
        GUICtrlSetData($Edit4_1, "7、修改DNS2为: " & $Dns[2] & @CRLF, "1")
        RunWait('netsh interface ip add dns name =' &$netsh& '  addr = ' & $Dns[2] & ' index=2', "c:\windows\system32", @SW_HIDE)
发表于 2009-9-13 19:00:13 | 显示全部楼层
试下修改最后一行
RunWait('netsh interface ip add dns name ="' & $netsh & '"  addr = ' & $Dns[2] & ' index=2', "c:\windows\system32", @SW_HIDE)
 楼主| 发表于 2009-9-13 20:13:25 | 显示全部楼层
把这 name =' &$netsh& '  ,替换成 name ="' & $netsh & '" 这个格式后,还是不行。
测试时网卡名称为"本地连接 2" 这样的形式的名称,
我看问题还是name ="' & $netsh & '" 不等于name="本地连接 2"
不知道为什么,名称后面有一个空格了,就不行,如果任何一个名称没有空格都可以实现。
请教高手!
发表于 2009-9-13 20:19:21 | 显示全部楼层
把run换成msgbox看看那一句的结果
msgbox(0,0,'netsh interface ip add dns name ="' & $netsh & '"  addr = ' & $Dns[2] & ' index=2')
 楼主| 发表于 2009-9-13 20:34:09 | 显示全部楼层
本帖最后由 fjvip 于 2009-9-13 20:35 编辑


弹出的提示也确实是name="我爱中国 3",可是你看状态,还是没有修改成功

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?加入

×
 楼主| 发表于 2009-9-13 21:25:44 | 显示全部楼层
;以下代码可以解决了,谢谢!
;获取网卡名称
        Dim $objwmiservice = ObjGet('winmgmts:\\localhost\root\CIMV2'), $netsh
        $colitems = $objwmiservice.ExecQuery('SELECT * FROM Win32_NetworkAdapter', 'WQL', 0x10 + 0x20)
        If IsObj($colitems) Then
                For $objitem In $colitems
                        If $objitem.netconnectionid <> '' Then
                                $netsh = $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 = "' & $netsh & '"  source = static addr = ' & $sdip & ' mask = ' & $mask & ' gateway = ' & $gateway & ' gwmetric = 0', "c:\windows\system32", @SW_HIDE)
        GUICtrlSetData($Edit4_1, "5、修改DNS1为: " & $Dns[1] & @CRLF, "1")
        RunWait('netsh interface ip set dns name = "' & $netsh & '"  source = static addr = ' & $Dns[1] & ' register = PRIMARY', "c:\windows\system32", @SW_HIDE)
        GUICtrlSetData($Edit4_1, "6、修改DNS2为: " & $Dns[2] & @CRLF, "1")
        RunWait('netsh interface ip add dns name = "' & $netsh & '"  addr = ' & $Dns[2] & ' index=2', "c:\windows\system32", @SW_HIDE)
        
发表于 2009-10-19 09:23:28 | 显示全部楼层
学习了!这个挺实用的!
发表于 2009-10-21 01:37:17 | 显示全部楼层
好像应该排除摄像头的TV连接 这样安全点
您需要登录后才可以回帖 登录 | 加入

本版积分规则

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

GMT+8, 2024-5-19 17:13 , Processed in 0.081580 second(s), 25 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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