SoFunction
Updated on 2025-03-10

Use vbs to automatically modify ip code


'///// Main program
dim msginf,machname' definition variable: dialog box, machine name
msginf=msgbox("This program can only be executed once, please execute it after the XP system hardware is installed!" &chr(13) & "Do you continue?",65, "Modify the machine network configuration") 'Information prompt
if msginf=1 then ' If you press OK, then

machname=inputon() 'Analysis with the function inputon()

if machname<>"quit" then ' If the return value is not equal to "quit", then
wmitoip(machname) ' Run the 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("") 

'Delete the startup group
if ("C:\Documents and Settings\Administrator\"Start" Menu\Program\Start\") then
set f1=("C:\Documents and Settings\Administrator\"Start" Menu\Program\Start\")
 
end if 

'Delete the vbs file
if ("e:\") then 
set f2=("e:\") 
 
end if 

Set WshShell = ("") 
' (" -r -t 5") ' Restart

end sub 

'/// Generate computer name
function inputon() ' function inputon()
dim t ' Variable
while true 'Loop until the function exits
t=inputbox("Click the rule to enter: " & chr(13) & "The first digit represents the computer room number" & chr(13) & "The second and third digits represent the machine number" & chr(13) & "Teacher computer use 00 represents" & chr(13) & "For example: 123 represents the computer room number 23" & chr(13) & "Please make sure the input is correct!!","Please enter the 3-digit machine ID!","") ' Enter the computer name, the default value is empty
if t="" then ' If t is equal to empty (the cancel key is pressed), then
inputon="quit" ' The return value is "quit"
exit function ' Exit program
end if 
if len(t)=3 then ' The length of the computer number is 3 digits
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 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,work group
lt=cint(left(t,1))'The left 1 digit numeric value of the machine number
rt=cint(right(t,2)) ' The right numerical value of the machine number

if lt=1 or lt=2 then'Judgement Gateway
gateway="192.168.1.254" 
else 
gateway="192.168.3.254" 
end if 

if lt=1 then 'Computer Room 1
lan="S01" 
ipv="192.168.1." 
if rt=0 then 'Teacher Machine
ipv=ipv+"100" 
else's student machine
ipv=ipv+Cstr(rt) 
end if 
end if 

if lt=2 then 'Computer Room 2
lan="S02" 
ipv="192.168.1." 
if rt=0 then 'Teacher Machine
ipv=ipv+"200" 
else's student machine
rt=rt+100 
ipv=ipv+Cstr(rt) 
end if 
end if 

if lt=3 then 'Computer Room 3
lan="S03" 
ipv="192.168.3." 
if rt=0 then 'Teacher Machine
ipv=ipv+"80" 
else's student machine
ipv=ipv+Cstr(rt) 
end if 
end if 

if lt=4 then 'Computer Room 4
lan="S04" 
ipv="192.168.3." 
if rt=0 then 'Teacher Machine
ipv=ipv+"160" 
else's student machine
rt=rt+80 
ipv=ipv+Cstr(rt) 
end if 
end if 

if lt=5 then 'Computer Room 5
lan="S05" 
ipv="192.168.3." 
if rt=0 then 'Teacher Machine
ipv=ipv+"240" 
else's student machine
rt=rt+160 
ipv=ipv+Cstr(rt) 
end if 
end if 

Set objWMIService=GetObject("winmgmts:\\" & strComputer & "\root\cimv2") 
Set colNetAdapters=("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE") 
strIPAddress=Array(ipv) 
strSubnetMask=Array(strmask) 
strGateway = Array(gateway) 'Modify the gateway
'strGatewayMetric = Array(1) 'Hop
strDNS=Array("61.134.1.4") 

For Each objNetAdapter in colNetAdapters  
errEnable=(strIPAddress, strSubnetMask)'ip, mask
errGateways = (strGateway)'Gateway
errDns=(strDNS)'dns 
Next 

Set objWMIService = GetObject("winmgmts:" _ 
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 
Set colComputers =  _ 
    ("Select * from Win32_ComputerSystem") 
For Each objComputer in colComputers 
err = ("No_" & t)' machine name
ReturnValue = ("S0" & left(t,1))'Work Group
Next 

end sub