蓝色动力网络

 找回密码
 立即注册

扫一扫,访问微社区

QQ登录

只需一步,快速开始

查看: 2258|回复: 0

批处理arp双向绑定详细教程

[复制链接]
发表于 2011-8-17 09:27:14 | 显示全部楼层 |阅读模式
一:此批处理有缺陷,有时不能实现真正意义的双绑!
  1. @echo off
  2. ::读取本机Mac地址
  3. if exist ipconfig.txt del ipconfig.txt
  4. ipconfig /all >ipconfig.txt
  5. if exist phyaddr.txt del phyaddr.txt
  6. find “Physical Address” ipconfig.txt >phyaddr.txt
  7. for /f “skip=2 tokens=12″ %%M in (phyaddr.txt) do set Mac=%%M
  8. ::读取本机ip地址
  9. if exist IPAddr.txt del IPaddr.txt
  10. find “IP Address” ipconfig.txt >IPAddr.txt
  11. for /f “skip=2 tokens=15″ %%I in (IPAddr.txt) do set IP=%%I
  12. ::绑定本机IP地址和MAC地址
  13. arp -s %IP% %Mac%
  14. ::读取网关地址
  15. if exist GateIP.txt del GateIP.txt
  16. find “Default Gateway” ipconfig.txt >GateIP.txt
  17. for /f “skip=2 tokens=13″ %%G in (GateIP.txt) do set GateIP=%%G
  18. ::读取网关Mac地址
  19. if exist GateMac.txt del GateMac.txt
  20. arp -a %GateIP% >GateMac.txt
  21. for /f “skip=3 tokens=2″ %%H in (GateMac.txt) do set GateMac=%%H
  22. ::绑定网关Mac和IP
  23. arp -s %GateIP% %GateMac%
  24. arp -s 网关IP 网关MAC
  25. del GateIP.txt
  26. del GateMac.txt
  27. del IPAddr.txt
  28. del ipconfig.txt
  29. del phyaddr.txt
  30. exit
复制代码


这个批处理要查询本机的ARP 缓存表,看里面是不是有网关的IP和MAC,有则能成功

绑定,但是没有的话就绑不定了!!!不过可以改进一下,达到双绑的目的,比如

加上arp -s 网关IP 网关MAC一行就可以了。。。

二、这个也不能实现真正意义的双绑,只能绑定本机IP和MAC
  1. @echo off
  2. for /f “delims=: tokens=2″ %%a in (‘ipconfig /all^ find “Physical Address”‘) do set local_mac=%%a
  3. for /f “delims=: tokens=2″ %%a in (‘ipconfig /all^ find “IP Address”‘) do set local_ip=%%a
  4. for /f “delims=: tokens=2″ %%a in (‘ipconfig /all^ find “Default Gateway”‘) do set gate_ip=%%a
  5. fo* /* %%* in (‘getmac /nh /s %local_ip%’) do set gate_mac=%%a
  6. arp -s %local_ip% %local_mac%
  7. arp -s %gate_ip% %gate_mac%
复制代码


(这个地方有问题,改进中……)

经测试,此批处理不能绑定网关IP和MAC,只能绑定本机IP和MAC

三、这个还不是很清楚,我要测试才行的,目前所知也能绑定本机IP和MAC
  1. @ECHO OFF
  2. SETLOCAL ENABLEDELAYEDEXPANSION
  3. for /f “tokens=2 delims=[]=” %%i in (‘nbtstat -a %COMPUTERNAME%’) do call set local=!local!%%i
  4. for /f “tokens=3″ %%i in (‘netstat -r^ find ” 0.0.0.0″‘) do set gm=%%i
  5. for /f “tokens=1,2″ %%i in (‘arp -a %gm%^ find /i /v “inter”‘) do set gate=%%i %%j
  6. arp -s %gate%
  7. arp -s %local%
  8. arp -s 网关IP 网关MAC
复制代码

这个批处理可以绑定网关IP和MAC,但是还是有缺陷,要依赖于本机上存在的ARP缓存!
改进方法为在最后加一个arp -s 网关IP和MAC!

四、这个是一个兄弟的博客上找到的,原理和第一个一样,只是改进了一点点!

这个P通过ping网关三次得到了网关的MAC其实以上的批都可以通过这个来搞定网关的IP和MAC,
但是如果开机的时候正在发生ARP欺骗的话 这样你绑的IP和MAC就是错的,不能上网了。。

不过这种情况很少,发过来试一下先吧!
  1. @echo off
  2. :::::::::::::清除所有的ARP缓存
  3. arp -d
  4. :::::::::::::读取本地连接配置
  5. ipconfig /all>ipconfig.txt
  6. :::::::::::::读取内网网关的IP
  7. for /f “tokens=13″ %%I in (‘find “Default Gateway” ipconfig.txt’) do set GatewayIP=%%I
  8. :::::::::::::PING三次内网网关
  9. ping %GatewayIP% -n 3
  10. :::::::::::::读取与网关arp缓存
  11. arp -a find “%GatewayIP%”>arp.txt
  12. :::::::::::::读取网关MAC并绑定
  13. for /f “tokens=1,2″ %%I in (‘find “%GatewayIP%” arp.txt’) do if %%I==%GatewayIP% arp -s %%I %%J
  14. :::::::::::::读取本机的 IP+MAC
  15. for /f “tokens=15″ %%i in (‘find “IP Address” ipconfig.txt’) do set ip=%%i
  16. for /f “tokens=12″ %%i in (‘find “Physical Address” ipconfig.txt’) do set mac=%%i
  17. :::::::::::::绑定本机的 IP+MAC
  18. arp -s %ip% %mac%
  19. :::::::::::::删除所有的临时文件
  20. del ipconfig.txt
  21. del arp.txt
  22. exit
复制代码
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

蓝色动力网络微信平台
网站管理,业务合作联系邮箱: admin#lansedongli.com    QQ:13412492 限网站业务问题.
网站帐号、密码、密保找回请使用注册邮箱,发送邮件至 password#lansedongli.com ,否则不予受理.
免责声明:本论坛所有文字和图片仅代表其个人观点.
本站某些资料或文章来自于互联网,不代表本站观点,如果侵犯了您的权益,请来信告知,我们会在三天内删除.
为了给大家一个更好的交流场所,请勿在本论坛发表与中华人民共和国法律相抵触的言论,请合作,谢谢!
Copyright © 2007-2019 Corporation Powered by网吧系统 版权所有    转载请注明!
浙ICP备11043737号-1 程序:Discuz! x3.4

湘公网安备 43018102000145号

手机版|Archiver|蓝色动力网络   

快速回复 返回顶部 返回列表