DOS批处理修改计算机的主机名和IP地址
DOS批处理修改计算机的主机名和IP地址
李彬
[摘要] 首先通过DOS批处理命令来收集MAC地址、IP地址和主机名,生成txt文件,然后再通过DOS批处理命令利用收集的txt文件依据MAC地址来修改主机名、IP地址、网关、DNS等。
[关键字]DOS 批处理 主机名 IP地址
网络的发展日新月异,网络上的资源也可谓日渐丰富,信息的沟通,资源的共享,成为当今信息时代网络生活的主旋律,网络上的一些实用信息只要经过适当的整合来合理的利用,就可大大提高工作效率,降低工作成本,下面就以DOS所具备的功能为例,通过网络获取参考信息来实现以下功能:
一、DOS下收集MAC地址、IP地址和主机名,生成txt文件
通过collect_all.bat文件来收集计算机的信息然后保存到生成的mac_new.txt文件中,mac_new.txt中每一行为每一台计算机的信息。
--------------------------------------------------------------------------
collect_all.bat
--------------------------------------------------------------------------
color a
:::: +++++++++++++++++++++++++++++++
::::获取机器的TCP/IP配置,并保存到(ipconfig.txt)文件中
if exist ipconfig.txt del ipconfig.txt
ipconfig /all >>ipconfig.txt
::::分离出机器的物理地址,并保存到(phyaddr.txt)文件中
if exist phyaddr.txt del phyaddr.txt
find "IP Address" ipconfig.txt >>phyaddr.txt
:::: +++++++++++++++++++++++++++++++
::::从(..\phyaddr.txt)文件中取得物理地址字符串,并赋给变量"MacAddress"
for /f "skip=2 tokens=15" %%M in (phyaddr.txt) do set strIP=%%M
::::分离出机器的物理地址,并保存到(phyaddr.txt)文件中
if exist phyaddr.txt del phyaddr.txt
find "Host Name" ipconfig.txt >>phyaddr.txt
:::: +++++++++++++++++++++++++++++++
::::从(..\phyaddr.txt)文件中取得物理地址字符串,并赋给变量"MacAddress"
for /f "skip=2 tokens=16" %%M in (phyaddr.txt) do set strHost=%%M
if exist phyaddr.txt del phyaddr.txt
find "Physical Address" ipconfig.txt >>phyaddr.txt
:::: +++++++++++++++++++++++++++++++
::::从(..\phyaddr.txt)文件中取得物理地址字符串,并赋给变量"MacAddress"
for /f "skip=2 tokens=12" %%M in (phyaddr.txt) do set strMac=%%M
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
if exist phyaddr.txt del phyaddr.txt
find "Default Gateway" ipconfig.txt >>phyaddr.txt
:::: +++++++++++++++++++++++++++++++
::::从(..\phyaddr.txt)文件中取得gateway,并赋给变量"MacAddress"
for /f "skip=2 tokens=13" %%M in (phyaddr.txt) do set strGateway=%%M
@echo %strHost% %strMac% %strIP% %strGateway% >>mac_new.txt
if exist phyaddr.txt del phyaddr.txt
if exist ipconfig.txt del ipconfig.txt
--------------------------------------------------------------------------
mac_new.txt
--------------------------------------------------------------------------
user1 00-11-5B-ED-F4-79 192.168.1.2 192.168.1.1
user2 00-E0-4C-04-5B-4B 192.168.1.3 192.168.1.1
……
二、DOS下利用收集的txt文件依据MAC地址来修改主机名、IP地址、网关、DNS(txt文件中每行为一条)
通过setip.bat文件来调用mac_new.txt、setip.reg、delsetip.reg,把要分配IP地址等信息的计算机的MAC地址同mac_new.txt表中每一行中MAC地址进行比照,如果存在即分配mac_new.txt表中与之对应的某一行的主机名、IP地址、网关、DNS等相关信息。
setip.reg和delsetip.reg是修改计算机注册表信息的文件。
--------------------------------------------------------------------------
setip.bat
--------------------------------------------------------------------------
@echo on
:::--------------------------------------------------------------------------
:::援引网络并在实践中进行调整了
:::::请你在使用把[MacAddress] 字段下改为你自己的机器配置:::::::::
:::::此文件,在winxp 专业版下测试过:::::::::::::
:::::带有“::::” 后面的语句为 注释::::::::::
:::---------------------------------------------------------------------
cls :::清屏
::::sleep 16
color a
set config=mac_new.txt
:::: +++++++++++++++++++++++++++++++
::::获取机器的TCP/IP配置,并保存到(ipconfig.txt)文件中
if exist ipconfig.txt del ipconfig.txt
ipconfig /all >>ipconfig.txt
::::分离出机器的物理地址,并保存到(phyaddr.txt)文件中
if exist phyaddr.txt del phyaddr.txt
find "Physical Address" ipconfig.txt >>phyaddr.txt
:::: +++++++++++++++++++++++++++++++
::::从(c:\phyaddr.txt)文件中取得物理地址字符串,并赋给变量"MacAddress"
for /f "skip=2 tokens=12" %%M in (phyaddr.txt) do set strMac=%%M
:::显示物理地址字符串
@echo %strMac%
::::以变量"strMac"为关键字搜索(Config.cfg)中相同的字符串行,并保存到(ComputerCfg.txt)
if exist ComputerCfg.txt del ComputerCfg.txt
find "%strMac%" %config% >>ComputerCfg.txt
::::显示机器名. (从"ComputerCfg.txt"中分离出机器名字段,并赋给变量"ComputerName")
for /f "skip=2 tokens=1" %%N in (ComputerCfg.txt) do set ComputerName=%%N
@echo %ComputerName%
::::显示IP地址. (从"ComputerCfg.txt"中分离出 IP地址字段,并赋给变量"IPAddress")
for /f "skip=2 tokens=3" %%I in (ComputerCfg.txt) do set IPAddress=%%I
@echo %IPAddress%
for /f "skip=2 tokens=4" %%I in (ComputerCfg.txt) do set gateway=%%I
@echo %gateway%
::::显示网卡MAC地址.(从"ComputerCfg.txt"中分离出 MAC地址字段,并赋给变量"MacAddress")
for /f "skip=2 tokens=2" %%M in (ComputerCfg.txt) do set MacAddress=%%M
@echo %MacAddress%
::::创建注册表文件
if exist ComputerName.reg del ComputerName.reg
@echo Windows Registry Editor Version 5.00 >ComputerName.reg
@echo [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName] >>ComputerName.reg
::::用变量"%ComputerName%"去替换实际的机器名
@echo "ComputerName"="%ComputerName%" >>ComputerName.reg
@echo [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters] >>ComputerName.reg
::::用变量"%ComputerName%"去替换实际的机器名
@echo "NV Hostname"="%ComputerName%" >>ComputerName.reg
@echo "Hostname"="%ComputerName%" >>ComputerName.reg
::::导入注册表文件。用的时候把" :: " 去掉
regedit /s ComputerName.reg
::::修改IP 地址。用变量"%IPAddress%" 去替换实际的IP 地址
netsh interface ip set address name="本地连接" source=static addr=%IPAddress% mask=255.255.255.0 gateway=%gateway% gwmetric=1
netsh.exe interface ip set dns "本地连接" static 210.47.224.2 primary
netsh interface ip set address name="本地连接2" source=static addr=%IPAddress% mask=255.255.255.0 gateway=%gateway% gwmetric=1
netsh.exe interface ip set dns "本地连接2" static 210.47.224.2 primary
netsh interface ip set address name="本地连接 2" source=static addr=%IPAddress% mask=255.255.255.0 gateway=%gateway% gwmetric=1
netsh.exe interface ip set dns "本地连接 2" static 210.47.224.2 primary
::::重起机器批处理!
color 1b
::::sleep 8
regedit /s delsetip.reg
del ComputerCfg.txt
del ComputerName.reg
del ipconfig.txt
del phyaddr.txt
ipconfig/all
pause
--------------------------------------------------------------------------
setip.reg
--------------------------------------------------------------------------
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run]
"SETIP"="C:\\IP_CONFIG.BAT"
--------------------------------------------------------------------------
delsetip.reg
--------------------------------------------------------------------------
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run]
"SETIP"=-
通过以上软件的操作,可为处理局域网大批机器提高效率,比如说学校内的机房、公众用的网吧等环境下,收集原有的信息,以便在重新安装系统或需要分批处理机器的时候实现程序在启动项中加载来自动分配计算机的主机名、IP地址等一系列信息。极大地方便人们的工作和生活。
参考文献:
[1]http://blog.chinaunix.net/u1/34948/showart_323736.html
[2] http://www.fjly.org/html/wz/wl/200912/03-7684.html
[3] http://www.cnxhacker.com/Article/show/440.html