動態綁定ip和mac地址的批處理 xp和win7可用(軟體開發的複用思想)

來源:互聯網
上載者:User

米老師給了一個批次檔,很簡單,經過我們改編成為現在這樣可以在win7和xp下都可用的批處理。雖然段代碼挺簡單,但是卻體現了軟體開發的重要思想——複用!!

吾輩有待提高呀。。。

整了半天發現在if 和else中,不能說使用set命令。只好用goto了,並且win7下不能用arp -s命令綁定,需要用進階的綁定命令

 


@echo off
if exist ipconfig.txt del ipconfig.txt
ipconfig /all >ipconfig.txt
setlocal enabledelayedexpansion
if exist phyaddr.txt del phyaddr.txt

rem 尋找指定行
set n=0
for /f "tokens=*" %%i in (ipconfig.txt) do (
set /a n+=1
if !n! equ 10 echo %%i>>phyaddr.txt
)
for /f "tokens=1" %%C in (phyaddr.txt) do set Cont=%%C
if %Cont%==物理地址. (
echo win7
goto win7
) else (
echo xp
goto winxp
)
:win7
if exist phyaddr.txt del phyaddr.txt
rem 尋找指定行
set n=0
for /f "tokens=*" %%i in (ipconfig.txt) do (
set /a n+=1
if !n! equ 10 echo %%i>>phyaddr.txt
)

rem 找到本地的mac地址,給變數mac賦值
for /f "tokens=15" %%M in (phyaddr.txt) do set Mac=%%M

if exist IPAddr.txt del IPaddr.txt
find "IPv4 地址" ipconfig.txt >IPAddr.txt
rem 找到本地的ip
for /f "skip=2 tokens=16" %%I in (IPAddr.txt) do set IP=%%I
set var=%IP:~0,14%
::@echo on
arp -s %var% %Mac%
if exist GateIP.txt del GateIP.txt
find "預設閘道" ipconfig.txt >GateIP.txt

rem 找到網關的ip
for /f "skip=2 tokens=15" %%G in (GateIP.txt) do set GateIP=%%G
if exist GateMac.txt del GateMac.txt
arp -a %GateIP% >GateMac.txt

rem 找到網關的mac地址
for /f "skip=3 tokens=2" %%H in (GateMac.txt) do set GateMac=%%H

netsh interface ipv4 show neighbors >ipv4.txt
set n=0
for /f "tokens=*" %%i in (ipv4.txt) do (
set /a n+=1
if !n! equ 1 echo %%i>>ipv4.txt
)
for /f "tokens=2" %%C in (ipv4.txt) do set ContD=%%C
set varD=%ContD:~0,2%
netsh interface ipv4 set neighbors %varD% %GateIP% %GateMac%
pause

if exist ipconfig.txt del ipconfig.txt
if exist phyaddr.txt del phyaddr.txt
if exist IPAddr.txt del IPAddr.txt
if exist GateMac.txt del GateMac.txt
if exist GateIP.txt del GateIP.txt
if exist ipv4.txt del ipv4.txt
exit

:winxp
echo 執行xp

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
::@echo on
arp -s %IP% %Mac%
if exist GateIP.txt del GateIP.txt
find "Default Gateway" ipconfig.txt >GateIP.txt
for /f "skip=2 tokens=13" %%G in (GateIP.txt) do set GateIP=%%G
if exist GateMac.txt del GateMac.txt
arp -a %GateIP% >GateMac.txt
for /f "skip=3 tokens=2" %%H in (GateMac.txt) do set GateMac=%%H
arp -s %GateIP% %GateMac%

if exist ipconfig.txt del ipconfig.txt
if exist phyaddr.txt del phyaddr.txt
if exist IPAddr.txt del IPAddr.txt
if exist GateMac.txt del GateMac.txt
if exist GateIP.txt del GateIP.txt

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.