jiexiao 发表于 2010-2-12 16:56:15

ip 设置代码,求鉴定

#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <GuiIPAddress.au3>
#include <StaticConstants.au3>
#include <TabConstants.au3>
#include <WindowsConstants.au3>
#include "Constants.au3"
#include <String.au3>
#Region ### START Koda GUI section ### Form=c:\documents and settings\administrator\桌面\form1.kxf
$Form1_1 = GUICreate("IP设置", 389, 418, -1, -1, BitOR($WS_SYSMENU,$WS_CAPTION,$WS_POPUP,$WS_POPUPWINDOW,$WS_BORDER,$WS_CLIPSIBLINGS))
$Tab1 = GUICtrlCreateTab(0, 4, 385, 413)
GUICtrlSetFont(-1, 9, 400, 0, "Arial")
GUICtrlSetResizing(-1, $GUI_DOCKWIDTH+$GUI_DOCKHEIGHT)
$TabSheet1 = GUICtrlCreateTabItem("常规")
$Label8 = GUICtrlCreateLabel("计算机名称:", 26, 60, 76, 17)
$Input1 = GUICtrlCreateInput("", 202, 55, 125, 21)
$Label1 = GUICtrlCreateLabel("连接名称 :", 26, 90, 58, 17)
$Group1 = GUICtrlCreateGroup("IP 地址", 14, 122, 357, 101)
$Label2 = GUICtrlCreateLabel("IP 地址 :", 30, 142, 70, 17)
$Label3 = GUICtrlCreateLabel("子网掩码 :", 30, 166, 70, 17)
$Label4 = GUICtrlCreateLabel("默认网关 :", 30, 194, 70, 17)
$IPAddress1 = _GUICtrlIpAddress_Create($Form1_1, 202, 138, 125, 21)
$IPAddress2 = _GUICtrlIpAddress_Create($Form1_1, 202, 166, 125, 21)
$IPAddress3 = _GUICtrlIpAddress_Create($Form1_1, 202, 194, 125, 21)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Input2 = GUICtrlCreateInput("", 202, 90, 125, 21)
$Group2 = GUICtrlCreateGroup("DNS服务器地址", 14, 238, 357, 77)
$IPAddress4 = _GUICtrlIpAddress_Create($Form1_1, 204, 255, 125, 21)
$IPAddress5 = _GUICtrlIpAddress_Create($Form1_1, 204, 283, 125, 21)
$Label5 = GUICtrlCreateLabel("首选 DNS服务器 :", 26, 262, 96, 17)
$Label6 = GUICtrlCreateLabel("备用 DNS服务器 :", 26, 288, 96, 17)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Button1 = GUICtrlCreateButton("应用", 192, 381, 81, 21, 0)
$Button2 = GUICtrlCreateButton("退出", 288, 381, 81, 21, 0)
GUICtrlCreateTabItem("")
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

Dim $eth,$ip,$mask,$gw,$dns1,$dns2,$Computername
_mr()
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $Button1
        _re()
        _do()
Case $Button2
        Exit
EndSwitch
WEnd

Func _mr();默认的配置
$eth=ljmc()
$ip="192.168.1.32"
$mask="255.255.255.0"
$gw="192.168.1.1"
$dns1="61.134.1.4"
$dns2="218.30.19.40"
$Computername=""
GUICtrlSetData($Input2,$eth)
_GUICtrlIpAddress_Set($IPAddress1,$ip)
_GUICtrlIpAddress_Set($IPAddress2,$mask)
_GUICtrlIpAddress_Set($IPAddress3,$gw)
_GUICtrlIpAddress_Set($IPAddress4,$dns1)
_GUICtrlIpAddress_Set($IPAddress5,$dns2)
GUICtrlSetData($Input1,$Computername)
EndFunc

Func _re()
$eth=GUICtrlRead($Input2)
$ip=_GUICtrlIpAddress_get($IPAddress1)
$mask=_GUICtrlIpAddress_Get($IPAddress2)
$gw=_GUICtrlIpAddress_Get($IPAddress3)
$dns1=_GUICtrlIpAddress_Get($IPAddress4)
$dns2=_GUICtrlIpAddress_get($IPAddress5)
$Computername=GUICtrlRead($Input1)
EndFunc

func _do();配置ip,dns。。。
        SplashTextOn("","配置中,耐心等待.",200,50)
FileDelete(@TempDir&"\DO.BAT")
FileWrite(@TempDir&"\DO.BAT","if " & $gw & "==none netsh interface ip set address " &'"'& $eth & '"' & " static " & $ip & " " & $mask & " " & $gw & @CRLF & "if not " & $gw & "==none netsh interface ip set address "& '"'&$eth&'"'&" static "&$ip & " " & $mask & " " & $gw & " auto" & @CRLF &"if not "&$gw&"==none netsh interface ip set address name="&'"'&$eth&'"'&" source=static gateway="&$gw&" gwmetric=auto" & @CRLF & "netsh interface ip set dns " & $eth & " static " & $dns1 & " primary" & @CRLF & "netsh interface ip add dns " & $eth & " " & $dns2 & " index=2" & @CRLF & "ipconfig/all")
RunWait(@TempDir&"\DO.BAT",@TempDir,@SW_HIDE)
If $Computername<>"" Then
RegWrite("HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\ComputerName\ComputerName","ComputerName","REG_SZ",$Computername)
RegWrite("HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\Tcpip\Parameters","NV Hostname","REG_SZ",$Computername)
RegWrite("HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\Tcpip\Parameters","Hostname","REG_SZ",$Computername)
RegWrite("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName","ComputerName","REG_SZ",$Computername)
RegWrite("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters","NV Hostname","REG_SZ",$Computername)
RegWrite("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters","Hostname","REG_SZ",$Computername)
EndIf
SplashOff()
MsgBox(0,"提示","成功")
FileDelete(@TempDir&"\DO.BAT")
EndFunc

Func ljmc();获取连接名称,要是有多个网卡的话不知道是什么情况
        SplashTextOn("","获取连接名称",200,50)
$cod = 'netsh interface ip show address'
$foo = Run($cod, @WorkingDir, @SW_HIDE, $STDOUT_CHILD)
While 1
        $line = StdoutRead($foo);获取数据
        If Not @error Then
                $line = $line
                If StringInStr($line, "的配置") <> 0 Then
                        $cont = _StringBetween($line, '接口 "', '" 的配置')
                        ExitLoop
                EndIf
                If StringInStr($line, "没有可用的数据了。") <> 0 Then
                        MsgBox(16, "错误", "请确定有可用连接后再运行本程序", 10)
                        Exit
                EndIf
        EndIf
WEnd
SplashOff()
Return $cont
EndFunc

weeks1 发表于 2010-2-14 19:18:36

呵呵,不错啊

yingf20 发表于 2010-9-14 22:32:11

这个不错。下载研究一下。谢谢

zpmc123 发表于 2017-1-2 13:53:03

能运行,很不错的代码,下载保存实用

austere 发表于 2017-1-5 12:43:55

这个是通过注册表来修改IP地址的,实际操作中问题有很多,比如不能立即生效或者多网卡本地链接名称不是本地链接就容易出问题
页: [1]
查看完整版本: ip 设置代码,求鉴定