Use vbs to automatically modify ip code

Source: Internet
Author: User

The system of the data center needs to be re-installed. There are nearly 300 devices in total. It is troublesome to re-set the ip address of the client after cloning using the ghost Network. The teaching management software we use requires the client to have a fixed ip address. The five data centers in the unit are as follows (DNS: 61.134.1.4, mask: 255.255.255.0 ):

Data Center

Starting ip Address

Ip termination ip

Gateway

Machine name

Working Group

1 192.168.1.1 100 254 No_100 ~ No_160 S01
2 192.168.1.101 200 254 No_200 ~ No_260 S02
3 192.168.3.1 80 254 No_300 ~ No_360 S03
4th 192.168.3.81 160 254 No_400 ~ No_460 S04
5 192.168.3.161 240 254 No_500 ~ No_560 S05

The following is the source code of vbs:
1. XP system (the test is successful. The user is Administrator and the file is E: \ fxp. vbs. Set up a shortcut for the Startup Group fxp. lnk to run automatically once after it is started)Copy codeThe Code is as follows: '// The main program
Dim msginf, machname' defines the variable: dialog box, machine name
Msginf = msgbox ("This program can only be executed once. Please execute it after the hardware of the XP system is installed! "& Chr (13) &" continue? ", 65," Modify machine network configuration ") 'Information prompt
If msginf = 1 then', if you press OK

Machname = inputon () 'analyzed using the inputon () function

If machname <> "quit" then', if the return value is not equal to "quit ",
Wmitoip (machname) 'Run function wmitoip () to set machine information
Mreboot () 'Restart the machine
End if
End if

'// Restart the machine
Sub mreboot ()
Dim fso, f1, f2
Set fso = CreateObject ("Scripting. FileSystemObject ")

'Delete the Startup Group
If fso. fileexists ("C: \ Documents ents and Settings \ Administrator \" start "Menu \ Program \ Start \ fxp. lnk") then
Set f1 = fso. getfile ("C: \ Documents ents and Settings \ Administrator \" start "Menu \ Program \ Start \ fxp. lnk ")
F1.delete
End if

'Delete the vbs File
If fso. fileexists ("e: \ fxp. vbs") then
Set f2 = fso. getfile ("e: \ fxp. vbs ")
F2.delete
End if

Set WshShell = Wscript. CreateObject ("Wscript. Shell ")
'Wshshell. Run ("shutdown.exe-r-t 5") 'restart

End sub

'// Generate the computer name
Function inputon () 'function inputon ()
Dim t' variable
While true 'loop until the function exits
T = inputbox ("Enter according to the following rule:" & chr (13) & chr (13) & "1st bits represent the IDC number" & chr (13) & "2nd, 3 represents the machine number" & chr (13) & "instructor machine 00 represents" & chr (13) & "e. g: 123 represents machine 23, Data Center 1 "& chr (13) &" Please make sure the input is correct !! "," Enter a three-digit machine ID! "," ") 'Enter the computer name. The default value is null.
If t = "" then', if t is null (the cancel key is pressed ),
Inputon = "quit" 'Return Value: "quit"
Exit function 'Exit the program
End if
If len (t) = 3 then' the computer number is 3 characters long
If Cint (t)> = 100 and Cint (t) <580 then' Verification
Inputon = t' return the required computer name
Exit function
End if
End if
Wend
End function

'// Modify the machine ip address, mask, gateway, working group, and machine name
Sub wmitoip (t)
StrComputer = "."
Strmask = "255.255.255.0"
Dim lt, rt 'variable
Dim ipv, gateway, lan ip, gateway, workgroup
Lt = cint (left (t, 1) 'value of the first digit on the left of the host number
Rt = cint (right (t, 2) 'machine number right two-digit Value

If lt = 1 or lt = 2 then' judge Gateway
Gateway = "192.168.1.254"
Else
Gateway = "192.168.3.254"
End if

If lt = 1 then'1 data center
Lan = "S01"
Ipv = "192.168.1 ."
If rt = 0 then' host
Ipv = ipv + "100"
Else student Machine
Ipv = ipv + Cstr (rt)
End if
End if

If lt = 2 then' Data Center 2
Lan = "S02"
Ipv = "192.168.1 ."
If rt = 0 then' host
Ipv = ipv + "200"
Else student Machine
Rt = rt + 100
Ipv = ipv + Cstr (rt)
End if
End if

If lt = 3 then' Data Center 3
Lan = "S03"
Ipv = "192.168.3 ."
If rt = 0 then' host
Ipv = ipv + "80"
Else student Machine
Ipv = ipv + Cstr (rt)
End if
End if

If lt = 4 then' Data Center 4
Lan = "S04"
Ipv = "192.168.3 ."
If rt = 0 then' host
Ipv = ipv + "160"
Else student Machine
Rt = rt + 80
Ipv = ipv + Cstr (rt)
End if
End if

If lt = 5 then' Data Center 5
Lan = "S05"
Ipv = "192.168.3 ."
If rt = 0 then' host
Ipv = ipv + "240"
Else student Machine
Rt = rt + 160
Ipv = ipv + Cstr (rt)
End if
End if

Set ob1_miservice = GetObject ("winmgmts: \" & strComputer & "\ root \ cimv2 ")
Set colNetAdapters = obw.miservice. ExecQuery ("Select * from Win32_NetworkAdapterConfiguration where IPEnabled = TRUE ")
StrIPAddress = Array (ipv)
StrSubnetMask = Array (strmask)
StrGateway = Array (gateway) 'modify gateway
'Strgatewaymetric = Array (1) 'hops
StrDNS = Array ("61.134.1.4 ")

For Each objNetAdapter in colNetAdapters
ErrEnable = objNetAdapter. EnableStatic (strIPAddress, strSubnetMask) 'ip address, mask
ErrGateways = objNetAdapter. SetGateways (strGateway) 'Gateway
ErrDns = objNetAdapter. SetDNSServerSearchOrder (strDNS) 'dns
Next

Set ob1_miservice = GetObject ("winmgmts :"_
& "{ImpersonationLevel = impersonate }! \ "& StrComputer &" \ root \ cimv2 ")
Set colComputers = obw.miservice. ExecQuery _
("Select * from Win32_ComputerSystem ")
For Each objComputer in colComputers
Err = ObjComputer. Rename ("No _" & t) 'MACHINE name
ReturnValue = objComputer. JoinDomainOrWorkGroup ("S0" & left (t, 1) 'workgroup
Next

End sub

System 2.98

The 98 system can generate the ip. reg registry file, which can be imported. The source code is as follows (the main idea is that this time there is no 98 system, so it is not completed. For details, refer to the improvement of the XP system ):

Copy codeThe Code is as follows: '// The main program
Dim msginf, machname' defines the variable: dialog box, machine name
Msginf = msgbox ("generate the Registry file. Continue? ", 65," getreg ") 'Information prompt
If msginf = 1 then', if you press OK

Machname = inputon () 'analyzed using the inputon () function

If machname <> "quit" then', if the return value is not equal to "quit ",
Setreg (machname) 'Run the setreg () function to generate registry ip. reg
End if
End if

'// Generate the computer name
Function inputon () 'function inputon ()
Dim t' variable
While true 'loop until the function exits
T = inputbox ("Enter according to the following rule:" & chr (13) & chr (13) & "1st bits represent the IDC number" & chr (13) & "2nd, 3 represents the machine number" & chr (13) & "instructor machine 00 represents" & chr (13) & "e. g: 123 represents machine 23, Data Center 1 "& chr (13) &" Please make sure the input is correct !! "," Enter a three-digit machine ID! "," ") 'Enter the computer name. The default value is null.
If t = "" then', if t is null (the cancel key is pressed ),
Inputon = "quit" 'Return Value: "quit"
Exit function 'Exit the program
End if
If len (t) = 3 then' the computer number is 3 characters long
If Cint (t)> = 100 and Cint (t) <580 then' Verification
Inputon = t' return the required computer name
Exit function
End if
End if
Wend
End function

'// Generate the registration file
Sub setreg (t) 'generates the Registry. t is the machine number.
Dim fso, f1, f2, lt, rt 'variable
Dim ipv, gateway, lan ip, gateway, workgroup
Lt = cint (left (t, 1) 'value of the first digit on the left of the host number
Rt = cint (right (t, 2) 'machine number right two-digit Value

If lt = 1 or lt = 2 then' judge Gateway
Gateway = "192.168.1.254"
Else
Gateway = "192.168.3.254"
End if

If lt = 1 then'1 data center
Lan = "S01"
Ipv = "192.168.1 ."
If rt = 0 then' host
Ipv = ipv + "100"
Else student Machine
Ipv = ipv + Cstr (rt)
End if
End if

If lt = 2 then' Data Center 2
Lan = "S02"
Ipv = "192.168.1 ."
If rt = 0 then' host
Ipv = ipv + "200"
Else student Machine
Rt = rt + 100
Ipv = ipv + Cstr (rt)
End if
End if

If lt = 3 then' Data Center 3
Lan = "S03"
Ipv = "192.168.3 ."
If rt = 0 then' host
Ipv = ipv + "80"
Else student Machine
Ipv = ipv + Cstr (rt)
End if
End if

If lt = 4 then' Data Center 4
Lan = "S04"
Ipv = "192.168.3 ."
If rt = 0 then' host
Ipv = ipv + "160"
Else student Machine
Rt = rt + 80
Ipv = ipv + Cstr (rt)
End if
End if

If lt = 5 then' Data Center 5
Lan = "S05"
Ipv = "192.168.3 ."
If rt = 0 then' host
Ipv = ipv + "240"
Else student Machine
Rt = rt + 160
Ipv = ipv + Cstr (rt)
End if
End if

Set fso = CreateObject ("Scripting. FileSystemObject ")
If fso. fileexists ("e: \ ip. reg") then
Set f2 = fso. getfile ("e: \ ip. reg ")
F2.delete
End if 'if ip. reg exists, delete it first

Set f1 = fso. CreateTextFile ("e: \ ip. reg", True) 'create file ip. cfg
The following 'f1. WriteLine ("REGEDIT4") 'generates the Registry
F1.WriteLine ("Windows Registry Editor Version 5.00 ")
F1.WriteBlankLines (1)
F1.WriteLine ("[HKEY_LOCAL_MACHINE \ System \ CurrentControlSet \ Control \ ComputerName]")
F1.WriteLine (chr (34) & "ComputerName" & chr (34) & "=" & chr (34) & t & chr (34) 'computer name
F1.WriteLine ("[HKEY_LOCAL_MACHINE \ System \ CurrentControlSet \ Services \ Class \ NetTrans \ 0000]")
F1.WriteLine (chr (34) & "IPAddress" & chr (34) & "=" & chr (34) & ipv & chr (34) 'IP
F1.WriteLine ("[HKEY_LOCAL_MACHINE \ System \ CurrentControlSet \ Services \ Class \ NetTrans \ 0000]")
F1.WriteLine (chr (34) & "DefaultGateway" & chr (34) & "=" & chr (34) & gateway & chr (34) 'gateway
F1.WriteLine ("[HKEY_LOCAL_MACHINE \ System \ CurrentControlSet \ Services \ Class \ NetTrans \ 0000]")
F1.WriteLine (chr (34) & "IPMask" & chr (34) & "=" & chr (34) & "255.255.255.0" & chr (34) 'Subnet Mask
F1.WriteLine ("[HKEY_LOCAL_MACHINE \ System \ CurrentControlSet \ Services \ VxD \ VNETSUP]")
F1.WriteLine (chr (34) & "Comment" & chr (34) & "=" & chr (34) & t & chr (34) 'computer description
F1.WriteLine ("[HKEY_LOCAL_MACHINE \ System \ CurrentControlSet \ Services \ VxD \ VNETSUP]")
F1.WriteLine (chr (34) & "ComputerName" & chr (34) & "=" & chr (34) & t & chr (34) 'computer name
F1.WriteLine ("[HKEY_LOCAL_MACHINE \ System \ CurrentControlSet \ Services \ VxD \ VNETSUP]")
F1.Writeline (chr (34) & "Workgroup" & chr (34) & "=" & chr (34) & lan & chr (34) 'Workgroup
End sub

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.