Code _vbs for automatically modifying IP using VBS

Source: Internet
Author: User
Tags chr
Unit room of the system needs to be reinstalled, a total of nearly 300 devices, the use of Ghost network cloning, the client reset IP is a troublesome thing. We use the teaching management software requires the client must have fixed IP, Unit 5 rooms are as follows (dns:61.134.1.4, mask is: 255.255.255.0):

Room

Start IP

IP Termination IP

Gateway

Machine name

Working Group

No. 1th 192.168.1.1 100 254 no_100~no_160 S01
No. 2nd 192.168.1.101 200 254 no_200~no_260 S02
No. 3rd 192.168.3.1 80 254 no_300~no_360 S03
No. 4th 192.168.3.81 160 254 no_400~no_460 S04
No. 5th 192.168.3.161 240 254 no_500~no_560 S05

The following is the VBS source code:
1.XP System (test pass, user for administrator, file for E:\fxp.vbs, Startup group set up shortcut Fxp.lnk to automatically run once after boot)
Copy Code code as follows:

'/////Main program
Dim msginf,machname ' Define Variable: dialog box, machine name
Msginf=msgbox ("This program can only perform 1 times, please do it after the XP system hardware is installed!") "&CHR & Continue?", 65, "Modify Machine Network Configuration") ' Information tips
If Msginf=1 then ' if OK, then

Machname=inputon () ' Using function Inputon () analysis

If machname<> "quit" Then ' if the return value is not equal to ' quit ', the
Wmitoip (machname) ' Run function Wmitoip () Set machine information
Mreboot () ' Reboot machine
End If
End If

'///reboot the Machine
Sub Mreboot ()
Dim fso,f1,f2
Set fso = CreateObject ("Scripting.FileSystemObject")

' Delete Startup group
If Fso.fileexists ("C:\Documents and settings\administrator\" Start menu \ program \ Start \fxp.lnk ") Then
Set F1=fso.getfile ("C:\Documents and settings\administrator\" Start "menu \ Program \ Start \fxp.lnk)
F1.delete
End If

' Delete 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") ' Reboot

End Sub

'///Generate computer name
function Inputon () ' Functions Inputon ()
Dim t ' variable
While True ' loop until the function is exited
T=inputbox ("Click the rule input:" & Chr & Chr () & "1th representative engine room number" & Chr () & "2nd, 3 representative machine number" & Chr () & "Teaching Division 00 representatives "& Chr () &" such as: 123 on behalf of the 1th Room 23rd machine "& Chr &" Please make sure the input is correct!! "," Please enter 3-bit machine ID! "," ") ' Input machine name, default value is null
If t= "then" if T equals null (press the Cancel key), the
inputon= "Quit" returns a value of "quit"
Exit Function ' Exits program
End If
If Len (t) =3 then ' computer number is 3 bits long
If Cint (t) >=100 and Cint (t) <580 then ' validation
Inputon=t ' Returns the computer name required
Exit function
End If
End If
Wend
End Function


'///Modify machine IP, mask, Gateway, workgroup, 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)) ' Machine number 1 digit value
Rt=cint (Right (t,2)) ' machine number two digits 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 ' No. 1th Room
Lan= "S01"
ipv= "192.168.1."
If Rt=0 then ' teacher machine
ipv=ipv+ "100"
Else ' Student machine
IPV=IPV+CSTR (RT)
End If
End If

If Lt=2 then ' No. 2nd Room
Lan= "S02"
ipv= "192.168.1."
If Rt=0 then ' teacher machine
ipv=ipv+ "200"
Else ' Student machine
rt=rt+100
IPV=IPV+CSTR (RT)
End If
End If

If Lt=3 then ' No. 3rd Room
Lan= "S03"
ipv= "192.168.3."
If Rt=0 then ' teacher machine
ipv=ipv+ "80"
Else ' Student machine
IPV=IPV+CSTR (RT)
End If
End If

If Lt=4 then ' No. 4th Room
Lan= "S04"
ipv= "192.168.3."
If Rt=0 then ' teacher machine
ipv=ipv+ "160"
Else ' Student machine
Rt=rt+80
IPV=IPV+CSTR (RT)
End If
End If

If Lt=5 then ' No. 5th Room
Lan= "S05"
ipv= "192.168.3."
If Rt=0 then ' teacher machine
ipv=ipv+ "240"
Else ' Student machine
rt=rt+160
IPV=IPV+CSTR (RT)
End If
End If

Set objwmiservice=getobject ("winmgmts:\\" & StrComputer & "\root\cimv2")
Set colnetadapters=objwmiservice.execquery ("select * from Win32_NetworkAdapterConfiguration where ipenabled=true")
Stripaddress=array (IPV)
Strsubnetmask=array (Strmask)
Strgateway = Array (Gateway) ' Modify Gateway '
' Strgatewaymetric = Array (1) ' Metric
Strdns=array ("61.134.1.4")

For each objnetadapter in Colnetadapters
Errenable=objnetadapter.enablestatic (strIPAddress, strsubnetmask) ' IP, mask
Errgateways = objnetadapter.setgateways (strgateway) ' Gateway
Errdns=objnetadapter.setdnsserversearchorder (strdns) ' DNS
Next

Set objWMIService = GetObject ("winmgmts:" _
& "{impersonationlevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set Colcomputers = objWMIService.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



2.98 System

98 system can generate Ip.reg registry files, after import can be, the source code as follows (the main idea, this time no 98 system, so unfinished, can refer to the improvement of XP system):


Copy Code code as follows:

'/////Main program
Dim msginf,machname ' Define Variable: dialog box, machine name
Msginf=msgbox ("Generate registry file, continue?", "Getreg") ' Info tip
If Msginf=1 then ' if OK, then

Machname=inputon () ' Using function Inputon () analysis

If machname<> "quit" Then ' if the return value is not equal to ' quit ', the
SetReg (machname) ' Run function setreg () Generate registry Ip.reg
End If
End If

'///Generate computer name
function Inputon () ' Functions Inputon ()
Dim t ' variable
While True ' loop until the function is exited
T=inputbox ("Click the rule input:" & Chr & Chr () & "1th representative engine room number" & Chr () & "2nd, 3 representative machine number" & Chr () & "Teaching Division 00 representatives "& Chr () &" such as: 123 on behalf of the 1th Room 23rd machine "& Chr &" Please make sure the input is correct!! "," Please enter 3-bit machine ID! "," ") ' Input machine name, default value is null
If t= "then" if T equals null (press the Cancel key), the
inputon= "Quit" returns a value of "quit"
Exit Function ' Exits program
End If
If Len (t) =3 then ' computer number is 3 bits long
If Cint (t) >=100 and Cint (t) <580 then ' validation
Inputon=t ' Returns the computer name required
Exit function
End If
End If
Wend
End Function

'///Generate registration file
Sub SetReg (t) ' generates registry, T is machine number
Dim FSO, f1,f2,lt,rt ' variable
Dim Ipv,gateway,lan ' IP, Gateway, workgroup
Lt=cint (Left (t,1)) ' Machine number 1 digit value
Rt=cint (Right (t,2)) ' machine number two digits 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 ' No. 1th Room
Lan= "S01"
ipv= "192.168.1."
If Rt=0 then ' teacher machine
ipv=ipv+ "100"
Else ' Student machine
IPV=IPV+CSTR (RT)
End If
End If

If Lt=2 then ' No. 2nd Room
Lan= "S02"
ipv= "192.168.1."
If Rt=0 then ' teacher machine
ipv=ipv+ "200"
Else ' Student machine
rt=rt+100
IPV=IPV+CSTR (RT)
End If
End If

If Lt=3 then ' No. 3rd Room
Lan= "S03"
ipv= "192.168.3."
If Rt=0 then ' teacher machine
ipv=ipv+ "80"
Else ' Student machine
IPV=IPV+CSTR (RT)
End If
End If

If Lt=4 then ' No. 4th Room
Lan= "S04"
ipv= "192.168.3."
If Rt=0 then ' teacher machine
ipv=ipv+ "160"
Else ' Student machine
Rt=rt+80
IPV=IPV+CSTR (RT)
End If
End If

If Lt=5 then ' No. 5th Room
Lan= "S05"
ipv= "192.168.3."
If Rt=0 then ' teacher machine
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 there is a ip.reg, first delete

Set f1 = fso. CreateTextFile ("E:\ip.reg", True) ' Create file Ip.cfg
' F1. WriteLine ("REGEDIT4") ' The following is the build registry
F1. WriteLine ("Windows Registry Editor Version 5.00")
F1. WriteBlankLines (1)
F1. WriteLine ("[Hkey_local_machine\system\currentcontrolset\control\computername\computername]")
F1. WriteLine (CHR) & "ComputerName" & Chr (34) & "=" & Chr (+) & T & Chr ()) ' Computer name
F1. WriteLine ("[hkey_local_machine\system\currentcontrolset\services\class\nettrans\0000]")
F1. WriteLine (CHR) & "IPAddress" & Chr (+) & "=" & Chr (+) & IPV & Chr (%)) ' IP
F1. WriteLine ("[hkey_local_machine\system\currentcontrolset\services\class\nettrans\0000]")
F1. WriteLine (CHR) & "DefaultGateway" & Chr (+) & "=" & Chr (+) & Gateway & Chr (34)) ' Gateway
F1. WriteLine ("[hkey_local_machine\system\currentcontrolset\services\class\nettrans\0000]")
F1. WriteLine (CHR) & "IPMask" & Chr (a) & "=" & Chr (+) & "255.255.255.0" & Chr (34)) ' Subnet mask
F1. WriteLine ("[Hkey_local_machine\system\currentcontrolset\services\vxd\vnetsup]")
F1. WriteLine (CHR) & "Comment" & Chr (a) & "=" & Chr (a) & T & Chr (34)) ' Computer description
F1. WriteLine ("[Hkey_local_machine\system\currentcontrolset\services\vxd\vnetsup]")
F1. WriteLine (CHR) & "ComputerName" & Chr (34) & "=" & Chr (+) & T & Chr ()) ' Computer name
F1. WriteLine ("[Hkey_local_machine\system\currentcontrolset\services\vxd\vnetsup]")
F1. Writeline (CHR) & "Workgroup" & Chr (a) & "=" & Chr (+) & 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.