把下面的批处理存为mac_set_ip.bat,该批处理需要以管理院身份运行,且要求本机只有一个网卡才能运行成功。
网络连接的名字暂定:“本地连接”,其中的网管(gate)和子网掩码(mask)我是按习惯方式先填了一个,请根据您实际的情况修改。
@setlocal enabledelayedexpansion
@set ip0=192.168.1.
@set gate=192.168.1.254
@set mask=255.255.255.0
@for /f "delims= " %%i in ('getmac|find "-"') do (
set mac=%%i
goto I
)
:I
@if "mac"=="" echo "获取mac出错"&goto X
@set ip=%ip0%%mac:~-2%
@netsh interface ipv4 set address "以太网" static %gate% %mask% %ip%
@netsh interface set interface 本地连接 disabled
@ping -n 1 127.0.0.1 >nul
@netsh interface set interface 本地连接 enabled
:X