2013-11-09 65 views
0

我必須編寫一個文件,通過靜態IP的輸入將IP設置更改爲靜態。 寫這樣做的文件並不難(槽BATCH或VBS),但問題是連接的名稱,標準窗口是本地連接,但它必須與每個連接一起工作,即使我(例如)重命名我的連接測試。還有一些人有2個或更多的連接,只有標準的應該改變,其他應該被禁用(WIFI,Hamachi等)。它將用於LAN-Party,以快速將每個人的IP地址改變爲給定IP地址(必須有某種輸入),而不是手動工作(需要200多人的時間)。編寫一個腳本來改變IP靜態

你們能給我一些提示/例子嗎?

由於事先 巴特

回答

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