编写脚本来更改IP tot static

时间:2013-11-09 15:39:12

标签: batch-file vbscript network-programming ip lan

我必须编写一个文件,将IP设置更改为静态,并输入静态IP。 编写执行此操作的文件(通过BATCH或VBS)并不是很难,但问题是连接的名称,标准窗口是本地连接,但它必须适用于每个连接,即使我(对于示例)将我的连接重命名为test。还有一些人有2个或更多的连接,只有标准的连接应该被更改,其他每个都应该被禁用(WIFI,Hamachi等)。它将用于LAN-Party,以快速将每个人的IP地址更改为给定的(必须有某种输入),而不是手动工作(需要花费200多人的时间)。

你们能给我一些提示/例子吗?

先谢谢, 巴特

1 个答案:

答案 0 :(得分:0)

我前一段时间为了类似的目的写了这篇文章。

它有点费力,但基本上它要求用户修改哪个网络连接,然后询问他们是否要打开DHCP,或键入手动IP地址。我想,登录用户需要管理权限才能更改此

Option Explicit

Const SCRIPT_NAME = "Set IP"
Const SUBNET_MASK = "255.255.255.0"

Dim objWMI
Dim arrNANames
Dim colNa, objNa
Dim colNAConfig, objNAConfig
Dim strIP
Dim intIPRet
Dim intCount, strSelectString, intSelected

Set objWMI = GetObject("winmgmts:\\.\root\cimv2")
Set colNA = objWMI.ExecQuery("select * from Win32_NetworkAdapter")

ReDim arrNANames(colNA.Count)
intCount = 0
strSelectString = "Select a network adapter to modify:" & vbCrLf
For Each objNa In colNa
    arrNANames(intCount) = objNA.Name
    strSelectString = strSelectString & intCount & ")  " & arrNANames(intCount) & vbCrLf
    intCount = intCount + 1
Next

Do
    intSelected = inputbox(strSelectString, SCRIPT_NAME)
    If intSelected = "" Or Not IsNumeric(intSelected) Then
        quitScript
    End If
Loop Until CInt(intSelected) < UBound(arrNANames) And CInt(intSelected) > -1

Set colNA = objWMI.ExecQuery("select * from Win32_NetworkAdapter where Name='" & arrNANames(intSelected) & "'")

For Each objNA In colNA
    Set colNAConfig = objWMI.ExecQuery("ASSOCIATORS OF {Win32_NetworkAdapter.DeviceID='" & objNA.DeviceID & "'} WHERE resultClass = win32_NetworkAdapterConfiguration ")
    For Each objNAConfig In colNAConfig
        If MsgBox("Do you want to enable automatic IP (DHCP/APIPA) for device " & chr(34) & objNa.Name & chr(34), vbQuestion+vbYesNo, SCRIPT_NAME) = vbYes Then
            intIPRet = objNAConfig.EnableDHCP
            Select Case intIPRet
                Case 0      MsgBox "DHCP enabled successfully", vbInformation, SCRIPT_NAME
                Case 1      MsgBox "DHCP enabled successfully" & vbCrLf & "Please reboot for changes to take effect", vbInformation, SCRIPT_NAME
                Case Else   MsgBox "Could not enable DHCP", vbCritical, SCRIPT_NAME
            End Select
        Else
            Do
                strIP = inputbox("Type an IP for network adapter: " & objNA.Name, SCRIPT_NAME)
                If strIP = "" Then
                    quitScript
                End If
            Loop Until isValidIP(strIP)
            intIPRet = objNAConfig.EnableStatic(Array(strIP),Array(SUBNET_MASK))
            Select Case intIPRet
                Case 0      MsgBox "IP changed to " & strIP, vbInformation, SCRIPT_NAME
                Case 1      MsgBox "IP changed to " & strIP & vbCrLf & "Please reboot for changes to take effect", vbInformation, SCRIPT_NAME
                Case Else   MsgBox "Could not change IP", vbCritical, SCRIPT_NAME
            End Select
        End If
    Next
Next

quitScript

'returns true if the parameter is a valid IP address
Function isValidIP(ip)
    Dim arrNums, intNum

    arrNums = Split(ip, ".")
    If UBound(arrNums) <> 3 Then
        isValidIP = False
        Exit Function
    End If
    For Each intNum In arrNums
        If Not IsNumeric(intNum) Then
            isValidIP = False
            Exit Function
        End If
        If intNum < 0 Or intNum > 255 Then
            isValidIP = False
            Exit Function
        End If
        If Len(intNum) > 1 And Left(intNum,1) = "0" Then
            isValidIP = False
            Exit Function
        End If
    Next
    isValidIP = True
End Function

Sub quitScript
    Set objWMI = Nothing
    Set colNa = Nothing
    WScript.Quit
End Sub