本人负责的部门有上百台机器,重装完后需要恢复原有配置,我曾使用过很多方法:
1:手工更改,这条就不要说了
2:使用netsh dump +文件和 netsh -f +文件来更改,但是整个盘都格式化就没用.
3:使用专用的更改ip地址的小程序,但还是需要手工来指定IP
最后,我想到如下办法:
1:首先实现:netsh interface ip 来更改IP
2:使用wmic computersystem 修改工作组
3:使用reg add 来更改机器名
4:因为修改机器名后要重启才生效,于是使用:reboot.inf 来重启
5:后来增加了新机器名和ip的配置,使用机器的序列号(联想机:wmic bios get SerialNumber)来做机器名,ip地址最后1位使用随机
因为可以通过 wmic来读取序列号,那么我们可以用来登记本机的网络配置时,就方便多了.
6:最后将自己整理的IP地址等对应表放到了bat中,避免bat调用其他文件.
7:实现上面功能后,我就可以将该bat文件封装到gho文件里面,恢复重启完后就可以自动设置,我不要做其他操作,用户就可以使用.如果有必要,你可以加上执行完后自动删除本身.
整个代码如下:(欢迎大家继续优化和整理)
复制代码 代码如下:
@echo off & code by m13632750052
for /f "tokens=12 delims= " %%i in ('ipconfig/all^|find "Physical Address"') do set localmac=%%i
echo 通过MAC地址%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 显示查找结果 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 后修改机器名……
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 刷新注册表
if exist c:/winnt (secedit /refreshpolicy machine_policy /enforce) else (gpupdate /force)
echo 修改工作组
wmic computersystem where Name="%Name%" call JoinDomainOrWorkgroup Name="%Workgroup%"
wmic computersystem where Name="%ComputerName%" call JoinDomainOrWorkgroup Name="%Workgroup%"
echo 修改IP
::查找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 NIC WHERE "MACAddress='%MacWMIC%' AND NetConnectionID!=NULL" GET NetConnectionID /value') do set "ConnName=%%A"
if not defined ConnName set ConnName="本地连接"
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绑定 %IP% %MAC%
arp -d
arp -s %IP% %MAC%
echo arp绑定网关 %gway%,如果网络不通,将使用默认网关的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 后重启电脑……
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 您的机器可能是新机器,需要手工输入ip
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=请输入新机器的名字
echo 机器名:%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 配置: %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 后修改机器名……
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