Network management dedicated automatic change ip\ machine name \ workgroup Batch _dos/bat

Source: Internet
Author: User
Tags dns2 echo display goto
I am responsible for the department has hundreds of machines, after reloading need to restore the original configuration, I have used many methods:
1: Manual change, this one will not say
2: Use Netsh dump + files and netsh-f + files to change, but the entire disk is formatted is useless.
3: Use a dedicated small program to change the IP address, but still need to manually specify IP

Finally, I came up with the following approach:
1: First implementation: netsh interface ip to change IP
2: Use WMIC ComputerSystem to modify the workgroup
3: Use REG add to change the machine name
4: Since the change machine name to restart before it takes effect, then use: Reboot.inf to restart
5: Later added the new machine name and IP configuration, using the machine's serial number (Lenovo: WMIC BIOS get serialnumber) to do the machine name, IP address the last 1 bits using random
Because the serial number can be read through WMIC, it is much more convenient to register the network configuration of this machine.
6: Finally put their own IP address and other corresponding tables into the bat, avoid bat call other files.
7: After the implementation of the above function, I can package the bat file inside the Gho file, restore after the restart can be automatically set, I do not do other operations, users can use. If necessary, you can add the execution after the automatic deletion itself.

The entire code is as follows: (Welcome to continue to optimize and organize)
Copy Code code as follows:

@echo off & Code by m13632750052
for/f "tokens=12 delims="%%i in (' Ipconfig/all^|find "Physical Address") do set localmac=%%i
Echo finds the corresponding machine name via MAC address%localmac%
for/f "skip=90 tokens=1,2,3,4 delims="%%i in (%~f0) do (
If [%%k]==[%localmac%] set name=%%i
If [%%k]==[%localmac%] set Ip=%%j
If [%%k]==[%localmac%] set Mac=%%k
If [%%k]==[%localmac%] set workgroup=%%l
Set mask=255.255.0.0
Set gway=168.30.1.233
Set dns=168.1.4.147
Set dns2=168.1.6.243
)
echo Display lookup results code by Cn-dos Breakme
If defined Name (echo%name%%ip%%mac%%workgroup%) Else (goto Newset)
: Enterset
FOR/L%%i in (3,-1,0) do ping-n 2 127.0.0.%%i>nul && @echo. %%i s after changing machine name ...

REG ADD "Hkey_current_user\software\microsoft\windows\shellnoroam"/V @/T reg_sz/d "%name%"/F
REG ADD "Hklm\system\currentcontrolset\control\computername\computername"/V "ComputerName"/t reg_sz/d "%name%"/F
REG ADD "Hklm\system\currentcontrolset\control\computername\activecomputername"/V "ComputerName"/t reg_sz/d "%Name% "/F
REG ADD "HKLM\SYSTEM\CurrentControlSet\Services\Eventlog"/V "ComputerName"/t reg_sz/d "%name%"/F
REG ADD "Hklm\system\currentcontrolset\control\computername\computername"/V "ComputerName"/t reg_sz/d "%name%"/F
REG ADD "HKLM\System\CurrentControlSet\Services\Tcpip\Parameters"/V "NV Hostname" T reg_sz/d "%name%"/F
REG ADD "HKLM\System\CurrentControlSet\Services\Tcpip\Parameters"/V "Hostname"/t reg_sz/d "%name%"/F

Echo Refreshes the registry
If exist C:\Winnt (Secedit/refreshpolicy machine_policy/enforce) Else (Gpupdate/force)

echo Modify Workgroup
WMIC ComputerSystem where name= "%name%" Call JoinDomainOrWorkgroup name= "%workgroup%"
WMIC ComputerSystem where name= "%computername%" Call JoinDomainOrWorkgroup name= "%workgroup%"

echo modifies IP
:: Find the connection name of the Mac
for/f "tokens=1,2,3,4,5,6 delims=-"%%a in ("%mac%") do set macwmic=%%a:%%b:%%c:%%d:%%e:%%f
for/f "skip=2 tokens=2* delims=="%%a in (' WMIC NICs WHERE ' macaddress= '%macwmic% ' and Netconnectionid!=null ' get Netconne Ctionid/value ') do set "Connname=%%a"
If not defined Connname set connname= "Local Area Connection"
netsh interface ip set address name= "%connname%" source=static addr=%ip% mask=%mask%
netsh interface ip set address name= "%connname%" gateway=%gway% gwmetric=0
netsh interface ip set DNS name= "%connname%" source=static addr=%dns% register=primary
netsh interface ip add dns name= "%connname%" addr=%dns2% index=2
netsh interface ip set wins name= "%connname%" Source=static Addr=none


echo ARP binding%ip%%mac%
Arp-d
Arp-s%ip%%mac%
echo ARP binding gateway%gway%, if the network is not working, the default gateway will be used mac:00-00-5e-00-01-01
Ping-n 2%gway%>nul 2>nul
If [%errorlevel%]==[1] (set gatemac=00-00-5e-00-01-01) Else (
for/f "Skip=3 tokens=2"%%h in (' Arp-a%gway% ') do set gatemac=%%h
)
CMD/C arp-s%gway%%gatemac%

FOR/L%%i in (3,-1,0) do ping-n 2 127.0.0.%%i>nul && @echo. %%i s reboot after the computer ...
Setlocal
CD/D%temp%
echo [Version] > Reboot.inf
Set Inf=installhinfsection DefaultInstall
Echo signature= $chicago $ >> reboot.inf
echo [DefaultInstall] >> Reboot.inf
rundll32 setupapi,%inf% 1%temp%\reboot.inf
Del Reboot.inf
EXIT

: Newset
Echo your machine may be a new machine, you need to enter IP manually
for/f "tokens=1,2 delims=="%%i in (' WMIC bios get serialnumber/value^|find ' = ') do set name=%%j
If not defined name set/p name= Please enter the name of the new machine
Echo Machine Name:%name%
Call:select "Physical Address" "Mac"
Echo Mac:%mac%
set/a aa=%random%%%9
Set ip=168.30.7.1%aa%
Set mask=255.255.0.0
Set gway=168.30.1.233
Set dns=168.1.4.147
Set dns2=168.1.6.243
Set workgroup=vga00l
echo IP configuration:%ip%%mask%%gway%%dns%%dns2%%workgroup%
FOR/L%%i in (5,-1,0) do ping-n 2 127.0.0.%%i>nul && @echo. %%i s after changing machine name ...
Goto Enterset

: Select
for/f "tokens=2 delims=:"%%i in (' Ipconfig/all ^| findstr/i/c:%1 ') do if not "!%~2!" = "" Set "%~2=%%i"
Goto:eof

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.