Tìm kiếm ↓↓↓

Hướng dẫn tạo Batch script thay đổi địa chỉ MAC của bộ điều hợp mạng


Với việc tạo tập tin batch sử dụng để thay đổi địa chỉ MAC của bộ điều hợp mạng bạn không còn phải sử dụng phần mềm hay thay đổi thủ công. Khi cần thay đổi chỉ cần chạy tập tin batch chọn thay đổi đại chỉ MAC hoặc bạn cũng có thể đặt lại địa chỉ MAC về mặc định rất đơn giản. Các bước thực hiện như sau:

Bước 1: Mở trình soạn thảo Notepad copy đoạn code bên dưới vào.

Batch-script


:: Name     : Change MAC Address using Batch Files

:: Author   : Nguyen Tuan.

:: Website  : www.blogthuthuatwin10.com

:: --------------------------------------------------------------------------


chcp 65001 >nul

@echo off

title Change MAC Address

mode con: cols=120 lines=32

color 3f

chcp 65001 >nul

cd /d %~dp0

cls

>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"

if "%errorlevel%" NEQ "0" (

echo: Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"

echo: UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%temp%\getadmin.vbs"

"%temp%\getadmin.vbs" & exit

)

if exist "%temp%\getadmin.vbs" del /f /q "%temp%\getadmin.vbs"

:: --------------------------------------------------------------------------


:: --------------------------------------------------------------------------

:main

setlocal enabledelayedexpansion

call :get-interfaces-info iset iinfo ireg

if "%1"=="list" (

call :cmdline-list-adapters iset iinfo

goto :eof

)

if "%1"=="" (

call :main-menu iset iinfo

pause

goto :eof

)



if "%1"=="change" (

call :cmdline-change-mac-address iinfo "%~2"  "%~3"

goto :eof

)



if "%1"=="reset" (

call :cmdline-reset-mac-address iinfo "%~2"

goto :eof

)



if "%1"=="__internal" (

goto internal-dispatch

goto :eof



:: --------------------------------------------------------------------------

:cmdline-reset-mac-address <1=iinfo> <2=adapater> <3=new mac>

echo.Đang đặt lại "%~2"'s địa chỉ MAC về mặc định.

call :reset-mac-address "%~1" "%~2"

goto :eof



:: --------------------------------------------------------------------------

:cmdline-change-mac-address <1=iinfo> <2=adapater> <3=new mac>

set __t=%~3

set __t=%__t:-=%

echo.Đang thay đổi "%~2"'s địa chỉ MAC sang "%~3"

call :change-mac-address "%~1" "%~2" "%__t%"

goto :eof



:: --------------------------------------------------------------------------

:cmdline-list-adapters <1=iset> <2=iinfo>

echo.

echo.Danh sách tên bộ điều hợp mạng:

echo.-------------------------------

for /f "tokens=2 delims=.=" %%a in ('set %~1.') do (

set __t=!%~2[%%a].mac!

echo Địa chỉ MAC: [!__t!] ^| Tên bộ điều hợp mạng: %%a

)



goto :eof



:: --------------------------------------------------------------------------

:main-menu <1=iset> <2=iinfo>

:main-menu-repeat

cls

echo.Đổi địa chỉ MAC

echo.

echo.ID Bộ điều hợp mạng

echo.

set __choices=Q

for /f "tokens=2 delims=.=" %%a in ('set %~1.') do (

call set __num=%%%~2[%%a].index%%

set __choices=!__choices!!__num!

set __c2i[!__num!]=%%a

echo !__num!^ %%a

)

echo.Q^ Thoát

echo.

set /p="Chọn bộ điều hợp mạng để tiếp tục:" <nul

choice /C:%__choices% >nul

set /a __num=%errorlevel%-1

if %__num%==0 (

call :close

)

set adapter=!__c2i[%__num%]!

call :adapter-menu "%~2" "%adapter%"

goto main-menu-repeat



:: --------------------------------------------------------------------------

:adapter-menu <1=iinfo> <2=adapter>

cls

call set __CurMac=%%%~1[%~2].mac%%

echo.Thông tin bộ điều hợp mạng:

echo.--------------------------

echo.

echo Bộ điều hợp mạng: %~2

call echo.Tên bộ điều hợp mạng: %%%~1[%~2].adapter%%

echo.Địa chỉ MAC: %__CurMac%

call echo.Trạng thái kết nối: %%%~1[%~2].state%%

echo.

echo.Chọn hành động:

echo.--------------------------

echo.

echo.C Đổi địa chỉ MAC

echo.R Đặt lại địa chỉ MAC

echo.B Về Menu chính

echo.

set /p="Lựa chọn của bạn là:" <nul

choice /C:"BCR" >nul

set __option=%errorlevel%



:: Go back to main menu

if %__option%==1 (

exit /b 0

)

if %__option%==2 (

call :prompt-new-mac-address !__CurMac! __NewMac

if !errorlevel!==0 (

goto :eof

)

echo.Đang đổi địa chỉ MAC sang !__NewMac!

call :change-mac-address iinfo "%~2" !__NewMac!

set /p="Địa chỉ MAC đã được đổi thành công!. Gõ bất kỳ phím nào để quay về Menu chính..."<nul

pause >nul

)

if %__option%==3 (

echo.

choice /C:YN /M "Bạn có muốn đặt lại địa chỉ MAC về mặc định "

if !errorlevel!==2 (

goto :eof

)

echo.Đang đặt lại địa chỉ MAC...

call :reset-mac-address iinfo "%~2"

set /p="Địa chỉ MAC đã được đặt lại thành công!. Gõ bất kỳ phím nào để quay về Menu chính..."<nul

pause >nul

)

goto :eof



:: --------------------------------------------------------------------------

:prompt-new-mac-address <1=curmac> <2=newmac> => errorlevel(0=cancel, 1=ok)

cls

echo.Chuẩn định dạng (IEEE 802) bao gồm sáu nhóm, mỗi nhóm gồm 2 số hoặc chữ.

echo.Mỗi nhóm hai chữ số được cách nhau bằng dấu gạch ngang (-).

echo.Để đơn giản bạn chỉ cần đổi từ 1 đến 2 chữ số trong sáu nhóm của địa chỉ

echo.MAC cũ và giữ nguyên 2 chữ số đầu.

echo.



:prompt-new-mac-address-repeat

echo.Địa chỉ MAC cũ: %~1

set /p __NewMAC="Nhập địa chỉ MAC mới: "

choice /C:YN /M "Bạn có chắc chắn thay đổi địa chỉ MAC "

if %errorlevel%==2 (

exit /b 0

)

set __NewMAC=%__NewMAC:-=%

if ".%__NewMAC:~11,1%" EQU "." (

echo.

echo.Địa chỉ MAC quá ngắn

echo.

goto prompt-new-mac-address-repeat

)

if ".%__NewMAC:~12,1%" NEQ "." (

echo.

echo.Địa chỉ MAC quá dài

echo.

goto prompt-new-mac-address-repeat

)



:: Update caller's value

set %~2=%__NewMAC%



set __t=%~1

set __t=%__t:-=%

if "%__NewMAC%"=="%__t%" (

exit /b 0

)

exit /b 1



:: --------------------------------------------------------------------------

:reset-mac-address <1=iinfo> <2=adapter>

set __t=!%~1[%~2].reg!

reg delete "%__t%" /F /V NetworkAddress >nul 2>&1

call :refresh-interface "%~1" "%~2"

goto :eof



:: --------------------------------------------------------------------------

:change-mac-address <1=iinfo> <2=adapter> <3=NewMac>

set __t=!%~1[%~2].reg!

reg add "%__t%" /F /V NetworkAddress /T REG_SZ /D %~3 >nul 2>&1

call :refresh-interface "%~1" "%~2"

goto :eof



:: --------------------------------------------------------------------------

:refresh-interface <1=iinfo> <2=adapter>

netsh interface set interface "%~2" disable

netsh interface set interface "%~2" enable

call set __t=%%%~1.name%%

call :get-interfaces-info "%__t%" "%~1" ireg

goto :eof



:: --------------------------------------------------------------------------

:get-interfaces-info <1=set-name> <2=iinfo> <3=ireg>

set %~2.name=%~1

call :get-interfaces-ipconfig-info "%~2" "%~1"

call :get-interfaces-reg-keys "%~3"

set /a __num=1

for /f "tokens=2 delims=.=" %%a in ('set iset.') do (

set __t=!iinfo[%%a].adapter!

set __v=%~3[!__t!]

call set __t=%%!__v!%%

set %~2[%%a].reg=!__t!

set %~2[%%a].index=!__num!

set /a __num+=1

)

goto :eof



:: --------------------------------------------------------------------------

:get-interfaces-reg-keys <1=arr>

call :get-interfaces-reg-keys-ex "%~1" "DriverDesc" 6 "Driver"

call :get-interfaces-reg-keys-ex "%~1" "AdapterModel" 7 "Adapter"

goto :eof



:: --------------------------------------------------------------------------

:get-interfaces-reg-keys-ex <1=arr> <2=KeyName> <3=MatchLen> <4=MatchVal>

for /f "tokens=*" %%a in (

'reg query "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{4d36e972-e325-11ce-bfc1-08002be10318}" /v "%~2" /s 2^>nul'

) do (

set __t=%%a

if "!__t:~0,10!"=="HKEY_LOCAL" (

set __regkey=%%a

)

if "!__t:~0,%3!"=="%~4" (

for /f "tokens=2*" %%b in ("%%a") do (

set %~1[%%c]=!__regkey!

)

)

)

set __regkey=

set __t=

goto :eof



:: --------------------------------------------------------------------------

:get-interfaces-ipconfig-info <1=arr> <2=interfaces-set>

:: Return interface friendly names

call :get-interfaces-netsh-info %~1



:: For each interface, return the physical address and the adapter name

for /f "tokens=1,2 delims=][" %%a in ('set %~1[') do (

call :get-interface-info %~1 "%%b"

set %~2.%%b=1

)

exit /b 0



:: --------------------------------------------------------------------------

:get-interface-info <1=arr> <2=name>

set /a __stage=0

for /f "tokens=*" %%a in ('ipconfig /all') do (

set __var=%%a

if !__stage! equ 0 (

set __t=!__var:*adapter %~2:=!

if "!__t!" NEQ "!__var!" (

set /a __stage=1

)

)

if !__stage! EQU 1 (

set __t=!__var:Description . . . . . . . . . . . : =!

if "!__t!" NEQ "!__var!" (

set %~1[%~2].adapter=!__t!

set /a __stage+=1

)

)

if !__stage! EQU 2 (

set __t=!__var:Physical Address. . . . . . . . . : =!

if "!__t!" NEQ "!__var!" (

set %~1[%~2].mac=!__t!

set /a __stage+=1

)

)

)

:: Clear work variables

set __stage=

set __var=

set __t=

goto :eof



:: --------------------------------------------------------------------------

:get-interfaces-netsh-info <1=arr>

FOR /F "tokens=2,3*" %%a in ('netsh interface show interface') do (

if /i "%%a" neq "State" (

set %1[%%c].state=%%a

)

)

exit /b 0



:: --------------------------------------------------------------------------

:internal-dispatch

shift

set __t=%1

shift

goto %__t%



:: --------------------------------------------------------------------------

:tests

call :test-get-interfaces-ipconfig-info

call :test-get-interfaces-reg-keys

call :test-get-interfaces-info

goto :eof



:: --------------------------------------------------------------------------

:test-prompt-new-mac-address

call :prompt-new-mac-address 11-22-33-44-55-66 newmac

echo.Kết quả: %errorlevel% ; newmac = %newmac%

goto :eof



:: --------------------------------------------------------------------------

:test-get-interfaces-info

call :get-interfaces-info iset iinfo ireg

echo ----------------------------

echo All interfactes information:

echo ----------------------------

set iinfo[

goto :eof



:: --------------------------------------------------------------------------

:test-get-interfaces-reg-keys

call :get-interfaces-reg-keys ireg

echo Interfaces registry keys map:

echo -----------------------------

set ireg[

goto :eof



:: --------------------------------------------------------------------------

:test-get-interfaces-ipconfig-info

call :get-interfaces-ipconfig-info iinfo ifaces

echo Interfaces information:

echo -----------------------

set iinfo[

echo.

echo All interfaces:

echo ---------------

set ifaces.

goto :eof



:: --------------------------------------------------------------------------

:close

cls

echo.Chương trình sẽ tự động tắt sau 15 giây.

echo.Cảm ơn bạn đã sử dụng chương trình.

echo.Tạm biệt và hẹn gặp lại.

timeout /t 15 /nobreak

exit

:: ------------------------------------------------------------------------------------



::End


Bước 2: Lưu lại với tên ví dụ MACAddressChanger.cmd hoặc MACAddressChanger.bat định dạng All files và Encoding: UTF-8

Bước 3: Chạy tập tin batch vừa tạo. Một vài hình ảnh minh họa.






Phổ biến trong tuần

Tin Tức