Board logo

标题: ARP绑定批处理的蹊跷 [打印本页]

作者: jiulong     时间: 2008-3-14 06:11    标题: ARP绑定批处理的蹊跷
-------------------绑定本机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 上例是用过渡文件做绑,优点是100%成功,缺点是需要过渡文件,对于玩火者是约不允许的(我就喜欢精简代码) 再来看下例: @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 :echo echo\ echo/ echo 网关IP:%GateWay% 网关MAC:%GateMac% echo 本机IP:%IP% 本机MAC:%Mac% pause>nul 上面的代码在FOR命令后不加&&和goto跳转就绝对失败,这个脚本运行时,有时成功有时失败,而第一例的是不管运行多少次都成功,为什么有这么奇怪的事情发生呢?百思不得其解?

作者: Climbing     时间: 2008-3-14 11:22
skip只能跳过前面的行,而后面的行是无法跳过的。 建议使用(·ipconfig /all ^| find "关键字"`)作为for命令的过滤源,这样就没有必要做跳转操作了。例如: for /f "usebackq tokens=12" %%M in (`ipconfig /all ^| find "Physical Address"`) do set Mac=%%M 不过如果一台机器安装了多个网卡,这还是无效的,要进一步想办法。

作者: jiulong     时间: 2008-3-19 02:58
楼上确实说得很正确,安装多块网卡的时候任何ARP绑定批处理都失效 除非固定死的 ARP -S IP MAC就不会失效