Windows指令碼 – 關於本機ARP靜態繫結批次檔講解[綁定ip-mac指令碼詳解]

來源:互聯網
上載者:User

http://www.x5dj.com/Blog/00534041/00389986.shtml

原批次檔如下:

@echo off
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

exit

現在以//開頭的為我的解釋

@echo off
//關閉命令回顯

if exist ipconfig.txt del ipconfig.txt
//如果存在 ipconfig.txt 這個檔案就對其進行刪除 

ipconfig /all >ipconfig.txt
//把 ipconfig /all 命令的顯示結果寫入 ipconfig.txt

if exist phyaddr.txt del phyaddr.txt
//如果存在 phyaddr.txt 這個檔案就對其進行刪除

find "Physical Address" ipconfig.txt >phyaddr.txt
//在 ipconfig.txt 檔案裡尋找 Physical Address 欄位的內容並將其欄位內容寫入 phyaddr.txt

for /f "skip=2 tokens=12" %%M in (phyaddr.txt) do set Mac=%%M
//在 phyaddr.txt 檔案中從第一行象下跳兩行,也就是從第三行開始,從第12個符號處取值,並把該值設定成 MAC 變數,舉個例子:Physical Address. . . . . . . . . : 00-E0-FC-0C-A8-4F,每一個連續的數值為一個符號
符號1:Physical
符號2:Address.
符號3:.
符號4:.
符號5:.
符號6:.
符號7:.
符號8:.
符號9:.
符號10:.
符號11::
符號12:00-E0-FC-0C-A8-4F
tokens=12 的意思現在大家該明白了吧,但是說明一點,FOR 命令中的變數在批處理中是用 %%X 表示,但是在 命令提示字元 下輸入卻是用 %X 表示的,切記

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

exit
//這幾句還看不懂的話就未免太``````
上面的指令碼是綁定本機IP和MAC的。
下面的是綁定 網關IP和MAC的。
 
@echo OFF
if %~n0==arp exit
if %~n0==Arp exit
if %~n0==ARP exit
echo 正在擷取本機資訊.....
:IP
FOR /f "skip=13 tokens=15 usebackq " %%i in (`ipconfig /all`) do Set IP=%%i && GOTO MAC
:MAC
echo IP:%IP%
FOR /f "skip=13 tokens=12 usebackq " %%i in (`ipconfig /all`) do Set MAC=%%i && GOTO GateIP
:GateIP
echo MAC:%MAC%
arp -s %IP% %MAC%
echo 正在擷取網關資訊.....
FOR /f "skip=17 tokens=13 usebackq " %%i in (`ipconfig /all`) do Set GateIP=%%i && GOTO GateMac
:GateMac
echo IP:%GateIP%
FOR /f "skip=3 tokens=2 usebackq " %%i in (`arp -a %GateIP%`) do Set GateMAC=%%i && GOTO Start
:Start
echo MAC:%GateMAC%
arp -d
arp -s %GateIP% %GateMAC%
echo 操作完成!!!
exit

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.