netsh 设置多网卡问题 【已解决】
本帖最后由 qw5838132 于 2020-3-24 16:18 编辑做一个设置多网卡的工具,现在单网卡没有问题,但是加多一张网卡的时候就只能设置新加的网卡,原网卡没反应了,把新网卡拆了,原网卡又能设置了,论坛上找了好多帖子都解决不了,来求救大神们了
#RequireAdmin
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GuiIPAddress.au3>
#include <StaticConstants。 au3>
#include <ComboConstants.au3>
#include <GuiStatusBar.au3>
#Include <Array.au3>
#Region ### START Koda GUI部分### Form =
Global Const $ aArray = GetAdapterProperty()
$ Form1 = GUICreate( “ Form1”,615,438、192、124)
$ Button1 = GUICtrlCreateButton(“确定”,
440,328,75,25 )$ IPAddress1 = _GUICtrlIpAddress_Create($ Form1、96、128、130、21); 80
_GUICtrlIpAddress_Set($ IPAddress1,“ 192.168.0.0”)
$ IPAddress2 = _GUICtrlIpAddress_Create($ Form1,96,176,130,21); 128
_GUICtrlIpAddress_Set($ IPAddress2,“ 255.255.255.0”)
$ IPAddress3 = _GUICtrlIpAddress_Create($ Form1,96,224,130,21); 176
_GUICtrlIpAddress_Set($ IPAddress3,“ 192.168.0.0”)
$ IPAddress4 = _GUICtrlIpAddress_Create($ Form1,96,264,130,21); 224
_GUICtrlIpAddress_Set($ IPAddress4,“ 192.168.0.1”)
$ IPAddress5 = _GUICtrlIpAddress_Create($ Form1,96,304,130 ,21); 264
_GUICtrlIpAddress_Set($ IPAddress5,“ 192.168.0.2”)
$ Combo1 = GUICtrlCreateCombo(“请拖动选择网卡”,25,90,280,25,BitOR($ CBS_DROPDOWNLIST,$ CBS_AUTOHSCROLL))
$ aaa = _ArrayToString( $ aArray)
; _ArrayDisplay($ aArray)
GUICtrlSetData($ Combo1,$ aaa)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI部分###
While 1
$ nMsg = GUIGetMsg()
开关$ nMsg
Case $ GUI_EVENT_CLOSE
退出
案例$ Button1
$ ipset = GUICtrlRead($ Combo1)
$ ip = _GUICtrlIpAddress_Get($ IPAddress1)
$ ym = _GUICtrlIpAddress_Get ($ IPAddress2)
$ wg = _GUICtrlIpAddress_Get($ IPAddress3)
$ dns = _GUICtrlIpAddress_Get($ IPAddress4)
$ dns1 = _GUICtrlIpAddress_Get($ IPAddress5)
; MsgBox(64,“ ok”,$ ip)
; MsgBox(64,“ ok”,$ ipset)
RunWait(@ ComSpec&'/ c netsh接口ip设置地址'&$ ipset&'static'&“”&$ ip&“”&$ ym&“”&$ wg& “”&'gwmetric = 1',``@SW_HIDE)
RunWait('netshinterface ip set dns name ='&$ ipset&'source = static addr ='&$ dns&'register = PRIMARY',“ c:\ windows \ system32“,@SW_HIDE)
RunWait('netshinterface ip add dns name ='&$ ipset&'addr ='&$ dns1&'index = 2',” c:\ windows \ system32“,@ SW_HIDE)
; MsgBox(64 ,”ok“,$ ipset)
EndSwitch
WEnd
Func GetAdapterProperty()
本地$ objShare
本地$ objEveryColl
$ objShare = ObjCreate(“ HNetCfg.HNetShare.1”)
如果不是IsObj($ objShare),则返回
$ objEveryColl = $ objShare.EnumEveryConnection
如果不是IsObj($ objEveryColl),然后返回
本地$ objNetConn,$ objShareCfg,$ objNCProps,$ str
本地$ aProps
对于$ objNetConn在$ objEveryColl
$ objShareCfg = $ objShare.INetSharingConfigurationForINetConnection($ objNetConn)
如果IsObj($ objShareCfg)然后
$ objNCProps = $ objShare.NetConnectionProps($ objNetConn)
如果IsObj($ objNCProps)然后
ReDim $ aProps
$ aProps = $ objNCProps.Name
EndIf
EndIf
Next
返回$ aProps
EndFunc; ==> GetAdapterProperty
为何不搜素一下
https://www.autoitx.com/thread-56350-1-1.html 看上去是命令行写的有问题
#RequireAdmin
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GuiIPAddress.au3>
#include <StaticConstants.au3>
#include <ComboConstants.au3>
#include <GuiStatusBar.au3>
#include <Array.au3>
#Region ### START Koda GUI section ###Form=
Global Const $aArray = GetAdapterProperty()
$Form1 = GUICreate("Form1", 615, 438, 192, 124)
$Button1 = GUICtrlCreateButton("确定", 440, 328, 75, 25)
$IPAddress1 = _GUICtrlIpAddress_Create($Form1, 96, 128, 130, 21) ;80
_GUICtrlIpAddress_Set($IPAddress1, "192.168.0.0")
$IPAddress2 = _GUICtrlIpAddress_Create($Form1, 96, 176, 130, 21) ;128
_GUICtrlIpAddress_Set($IPAddress2, "255.255.255.0")
$IPAddress3 = _GUICtrlIpAddress_Create($Form1, 96, 224, 130, 21) ;176
_GUICtrlIpAddress_Set($IPAddress3, "192.168.0.0")
$IPAddress4 = _GUICtrlIpAddress_Create($Form1, 96, 264, 130, 21) ;224
_GUICtrlIpAddress_Set($IPAddress4, "192.168.0.1")
$IPAddress5 = _GUICtrlIpAddress_Create($Form1, 96, 304, 130, 21) ;264
_GUICtrlIpAddress_Set($IPAddress5, "192.168.0.2")
$Combo1 = GUICtrlCreateCombo("请下拉选择网卡", 25, 90, 280, 25, BitOR($CBS_DROPDOWNLIST, $CBS_AUTOHSCROLL))
$aaa = _ArrayToString($aArray)
;_ArrayDisplay($aArray)
GUICtrlSetData($Combo1, $aaa)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $Button1
$ipset = GUICtrlRead($Combo1)
$ip = _GUICtrlIpAddress_Get($IPAddress1)
$ym = _GUICtrlIpAddress_Get($IPAddress2)
$wg = _GUICtrlIpAddress_Get($IPAddress3)
$dns = _GUICtrlIpAddress_Get($IPAddress4)
$dns1 = _GUICtrlIpAddress_Get($IPAddress5)
;MsgBox(64,"ok",$ip)
;MsgBox(64,"ok",$ipset)
RunWait(@ComSpec & ' /c netsh interface ip set address "' & $ipset & '" static' & " " & $ip & " " & $ym & " " & $wg & " " & 'gwmetric=1', '', @SW_HIDE)
RunWait('netsh interface ip set dns name = "' & $ipset & '" source = static addr = ' & $dns & ' register = PRIMARY', "c:\windows\system32", @SW_HIDE)
RunWait('netsh interface ip add dns name = "' & $ipset & '" addr = ' & $dns1 & ' index=2', "c:\windows\system32", @SW_HIDE)
;MsgBox(64,"ok",$ipset)
EndSwitch
WEnd
Func GetAdapterProperty()
Local $objShare
Local $objEveryColl
$objShare = ObjCreate("HNetCfg.HNetShare.1")
If Not IsObj($objShare) Then Return
$objEveryColl = $objShare.EnumEveryConnection
If Not IsObj($objEveryColl) Then Return
Local $objNetConn, $objShareCfg, $objNCProps, $str
Local $aProps
For $objNetConn In $objEveryColl
$objShareCfg = $objShare.INetSharingConfigurationForINetConnection($objNetConn)
If IsObj($objShareCfg) Then
$objNCProps = $objShare.NetConnectionProps($objNetConn)
If IsObj($objNCProps) Then
ReDim $aProps
$aProps = $objNCProps.Name
EndIf
EndIf
Next
Return $aProps
EndFunc ;==>GetAdapterProperty
zghwelcome 发表于 2020-3-22 17:27
看上去是命令行写的有问题
#RequireAdmin
不知道哪里出问题了,能帮忙看一下吗 229989799 发表于 2020-3-22 16:57
为何不搜素一下
https://www.autoitx.com/thread-56350-1-1.html
我是个新手,这个代码我有看过,太费劲了,看不太明白 qw5838132 发表于 2020-3-22 17:39
我是个新手,这个代码我有看过,太费劲了,看不太明白
改过了,还有问题吗 zghwelcome 发表于 2020-3-22 17:59
改过了,还有问题吗
还是只能修改新加的网卡,原网卡没反应 首先需要识别所有网卡,然后在具体操作的时候,可以使用netsh或者wmic或者api修改ip。具体的网上都有教程。重点:1.网卡识别;2.全面的可靠的彻底的IP修改;3.输入的ip的验证和设置完成后的状态检查。4.UI编写,让基本操作可视化。没有什么困难的。把你的1个问题分解成若干个具体的问题。哪些问题可以查资料解决,哪些问题解决不了。修改ip不论是批处理还是AU3,写起来都没有难度。 看了一下,你这边的几个问题。
1.你获取的网卡列表过于简单,无法知道哪个网卡是虚拟的还是物理的。不知道mac地址、index、网卡名称,ip、dns等信息。只知道连接名称。不便于你后续的检查和操作。而且也不便于用户选择正确的网卡。
建议:重新使用恰当的网卡列表获取UDF。并应用获取到的这些参数,助力你后面的程序使用或者用户辨识。
2.修改ip地址是系统重要操作。需要加入UAC权限(提权)
3.一个runwait你用出了两种技术。建议再参考一下。另外我场景写过一个获取回显的批处理udf。建议使用这样的,方便分析回现结果。
4.仔细分析了你的修改ip的代码,并且和我以前写的做了对比,实际命令基本没有区别。
5.综上,目前你只需要加个管理员权限,就可以了。为方便查看执行结果,建议用我写的udf。为方便用户查看和选择,建议强化ui
netsh interface ip set address "以太网" static 192.168.0.0 255.255.255.0 192.168.0.0 gwmetric=1
netsh interface ip set dns name = "以太网" source = static addr = 192.168.0.1 register = PRIMARY
netsh interface ip add dns name = "以太网" addr = 192.168.0.2 index=2
rem 修改IP地址,子网掩码,默认网关
Netsh interface IP set Address "以太网" Static !ip! !子网掩码! !默认网关! 1 >nul 2>nul
rem 修改首选DNS
Netsh interface IP set dns "以太网" static !首选DNS! primary >nul 2>nul
rem 修改备用DNS
Netsh interface IP add dns "以太网" !备用DNS! >nul 2>nul
附件是我去年随手写的一个简易的ip修改工具。使用没有任何问题,唯一的缺陷就是DHCP后,不会再去获取新的ip并显示到界面上,需要关闭程序重新打开。
支持手动输入ip,dhcp。
修改ip地址时操作完整,不会遗漏。
在使用VPN等软件,导致ip地址配置异常(例如ip正常,dns不正常等,有奇效。可正确清除)
供参考。
xyx115 发表于 2020-3-23 17:25
看了一下,你这边的几个问题。
1.你获取的网卡列表过于简单,无法知道哪个网卡是虚拟的还是物理的。不知道 ...
我做的这个工具是我自己用的,不是给别人用的,平时经常用到修改IP,我的想法是尽量精简,不要太多的按钮,不需要太多的功能,UI是我随便乱建的,成品不是这样的,这个udf是h版主发出来的,其实还可以设别很多功能,我给精简掉了 xyx115 发表于 2020-3-23 17:01
首先需要识别所有网卡,然后在具体操作的时候,可以使用netsh或者wmic或者api修改ip。具体的网上都有教程。 ...
测试的时候发现netsh interface ip set address name = "本地连接 5" source = static address = 192.168.1.1 mask = 255.255.255.0 gateway = 192.168.1.2 gwmetric = 1这样run 是没问题的,所有有个想法,把他建成一个文本,再读取出来然后再run但是测试后又发现,只要是中文名字的网卡,写入到文本就会换行,这个怎么解决呢,能帮忙测试一下吗,我把代码发出来
#RequireAdmin
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GuiIPAddress.au3>
#include <StaticConstants.au3>
#include <ComboConstants.au3>
#include <GuiStatusBar.au3>
#include <Array.au3>
#include <MsgBoxConstants.au3>
#include <WinAPIFiles.au3>
#Region ### START Koda GUI section ###Form=
Global Const $aArray = GetAdapterProperty()
$wenjian = @ScriptDir & "\ceshi.txt" ;新建文本到当前脚本位置
$Form1 = GUICreate("Form1", 615, 438, 192, 124)
$Button1 = GUICtrlCreateButton("确定", 440, 328, 75, 25)
$IPAddress1 = _GUICtrlIpAddress_Create($Form1, 96, 128, 130, 21) ;80
_GUICtrlIpAddress_Set($IPAddress1, "192.168.0.0")
$IPAddress2 = _GUICtrlIpAddress_Create($Form1, 96, 176, 130, 21) ;128
_GUICtrlIpAddress_Set($IPAddress2, "255.255.255.0")
$IPAddress3 = _GUICtrlIpAddress_Create($Form1, 96, 224, 130, 21) ;176
_GUICtrlIpAddress_Set($IPAddress3, "192.168.0.0")
$IPAddress4 = _GUICtrlIpAddress_Create($Form1, 96, 264, 130, 21) ;224
_GUICtrlIpAddress_Set($IPAddress4, "192.168.0.1")
$IPAddress5 = _GUICtrlIpAddress_Create($Form1, 96, 304, 130, 21) ;264
_GUICtrlIpAddress_Set($IPAddress5, "192.168.0.2")
$Combo1 = GUICtrlCreateCombo("请下拉选择网卡", 25, 90, 280, 25, BitOR($CBS_DROPDOWNLIST, $CBS_AUTOHSCROLL))
$aaa = _ArrayToString($aArray)
;_ArrayDisplay($aArray)
GUICtrlSetData($Combo1, $aaa)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $Button1
$ipset = GUICtrlRead($Combo1)
$ip = _GUICtrlIpAddress_Get($IPAddress1)
$ym = _GUICtrlIpAddress_Get($IPAddress2)
$wg = _GUICtrlIpAddress_Get($IPAddress3)
$dns = _GUICtrlIpAddress_Get($IPAddress4)
$dns1 = _GUICtrlIpAddress_Get($IPAddress5)
;MsgBox(64,"ok",$ip)
;MsgBox(64,"ok",$ipset)
If FileExists($wenjian) = 0 Then
IniWrite($wenjian, "ni", "hao","netsh interface ip set address name = """& $ipset& """ source = static address = "& $ip &" mask = "& $ym &" gateway = "& $wg &" gwmetric = 1")
;IniWrite($wenjian, "ni", "hao", "netsh interface ipv4 set address name = & $ipset&source = static address = & $ip & mask = & $ym & gateway = & $wg & gwmetric = 1")
Local $sRead = IniRead($wenjian, "ni", "hao", "Default Value")
MsgBox($MB_SYSTEMMODAL, "", "字段是 " & $sRead)
;FileDelete($wenjian)
EndIf
;MsgBox($MB_SYSTEMMODAL, "", "字段是1 " & $sRead)
;RunWait(@ComSpec & ' /c netsh interface ip set address "' & $sRead & '" static' & " " & $ip & " " & $ym & " " & $wg & " " & 'gwmetric=1', '', @SW_HIDE)
RunWait('netsh interface ip set dns name = "' & $ipset & '" source = static addr = ' & $dns & ' register = PRIMARY', "c:\windows\system32", @SW_HIDE)
RunWait('netsh interface ip add dns name = "' & $ipset & '" addr = ' & $dns1 & ' index=2', "c:\windows\system32", @SW_HIDE)
;MsgBox(64,"ok",$ipset)
EndSwitch
WEnd
Func GetAdapterProperty()
Local $objShare
Local $objEveryColl
$objShare = ObjCreate("HNetCfg.HNetShare.1")
If Not IsObj($objShare) Then Return
$objEveryColl = $objShare.EnumEveryConnection
If Not IsObj($objEveryColl) Then Return
Local $objNetConn, $objShareCfg, $objNCProps, $str
Local $aProps
For $objNetConn In $objEveryColl
$objShareCfg = $objShare.INetSharingConfigurationForINetConnection($objNetConn)
If IsObj($objShareCfg) Then
$objNCProps = $objShare.NetConnectionProps($objNetConn)
If IsObj($objNCProps) Then
ReDim $aProps
$aProps = $objNCProps.Name
EndIf
EndIf
Next
Return $aProps
EndFunc ;==>GetAdapterProperty xyx115 发表于 2020-3-23 17:01
首先需要识别所有网卡,然后在具体操作的时候,可以使用netsh或者wmic或者api修改ip。具体的网上都有教程。 ...
应该是跟编码有问题,英文名字的网卡是识别在一行,中文名字的就会换行出现 本帖最后由 floor6ggg 于 2020-3-24 11:56 编辑
试试:$ipset = StringStripWS(GUICtrlRead($Combo1),1)
应该是在第43行的代码,
直接删除行首的换行符
页:
[1]
2