-------------------绑定本机IP与MAC
if exist ipconfig.txt del ipconfig.txt
ipconfig /all >ipconfig.txt
if exist phyaddr.txt del phyaddr.txt
find "Physical Address" ipconfig.txt >phyaddr.txt
for /f "skip=2 tokens=12" %%M in (phyaddr.txt) do set Mac=%%M
if exist IPAddr.txt del IPaddr.txt
find "IP Address" ipconfig.txt >IPAddr.txt
for /f "skip=2 tokens=15" %%I in (IPAddr.txt) do set IP=%%I
arp -s %IP% %Mac%
del ipaddr.txt
del ipconfig.txt
del phyaddr.txt
再来看下例:
@echo off
arp -d
for /f "skip=17 tokens=13 usebackq" %%i in (`ipconfig/all`) do set GateWay=%%i&&goto IP
:ip
for /f "skip=15 tokens=15 usebackq" %%i in (`ipconfig/all`) do set IP=%%i&&goto Mac
:Mac
for /f "skip=13 tokens=12 usebackq" %%i in (`ipconfig/all`) do set Mac=%%i&&goto GateMac
:GateMac
ping /n 1 %GateWay%>nul
for /f "skip=3 tokens=2 usebackq" %%i in (`arp -a %GateWay%l`) do set GateMac=%%i&&goto echo
建议使用(·ipconfig /all ^| find "关键字"`)作为for命令的过滤源,这样就没有必要做跳转操作了。例如:
for /f "usebackq tokens=12" %%M in (`ipconfig /all ^| find "Physical Address"`) do set Mac=%%M