由于在虚拟机中做实验的需要,经常要把客户端的IP设置为静态IP。按照一般的操作肯定是手动设置IP了。今天,我参照别人的批处理自己也弄了一个。呵呵,方便自己。说明,我是先通过DHCP获取IP,然后在进行静态设置的。
以下是整个批处理的内容:
@echo on
:::::::::读取本机Mac地址
if exist ipconfig.txt del ipconfig.txt
ipconfig /all >ipconfig.txt
:::::::::读取本机ip地址
if exist IPAddr.txt del IPaddr.txt
find "IP Address" ipconfig.txt >IPAddr.txt
find "Subnet Mask" ipconfig.txt >MASKAddr.txt
for /f "skip=2 tokens=15" %%I in (IPAddr.txt) do set IP=%%I
for /f "skip=2 tokens=15" %%m in (MASKAddr.txt) do set MASKS=%%m
:::::::::读取网关地址
if exist GateIP.txt del GateIP.txt
find "Default Gateway" ipconfig.txt >GateIP.txt
for /f "skip=2 tokens=13" %%G in (GateIP.txt) do set GateIP=%%G
:::::::::读取DNS地址
if exist DNSServers.txt del DNSServers.txt
find "DNS Servers" ipconfig.txt >DNSServers.txt
for /f "skip=2 tokens=15" %%e in (DNSServers.txt) do set DNSIP=%%e
:::::::::设置本机IP地址
netsh interface ip set address name="本地连接" static %IP% %MASKS% %GateIP% 1
netsh int ip add dns name="本地连接" %DNSIP% index=1
:::::::::清理文件
del /q ipconfig.txt
del /q IPAddr.txt
del /q MASKAddr.txt
del /q GateIP.txt
del /q DNSServers.txt
echo 任务完成