Skip to content

Instantly share code, notes, and snippets.

@MrCarb0n
Last active August 20, 2024 09:42
Show Gist options
  • Save MrCarb0n/6d63d753d35dd72b674f30e2b2278845 to your computer and use it in GitHub Desktop.
Save MrCarb0n/6d63d753d35dd72b674f30e2b2278845 to your computer and use it in GitHub Desktop.
Batch script to toggle a network adapter between static IP and DHCP for TP-Link router recovery mode access.
@echo off
REM ==========================================================
REM Title: Toggle Network Adapter IP Between Static and Automatic
REM Description:
REM This batch script toggles the IP configuration of a specified
REM network adapter between a static IP and automatic (DHCP)
REM configuration. It is designed for use with TP-Link router
REM recovery mode. The script assigns a static IP address when
REM DHCP is enabled, and reverts to automatic IP configuration
REM if a static IP is already set.
REM Usage:
REM 1. Replace "Ethernet" with the name of your network adapter
REM if it differs from "Ethernet".
REM 2. Save the script as a .bat file (e.g., ToggleIP.bat).
REM 3. Run the script with administrator privileges to apply
REM the changes.
REM ==========================================================
REM Set the network adapter name
REM Replace "Ethernet" with your adapter name if different
set "adapter_name=Ethernet"
REM Set the desired static IP address and subnet mask
REM The IP address 192.168.0.66 is used for accessing TP-Link
REM router recovery mode
set "ip_address=192.168.0.66"
set "subnet_mask=255.255.255.0"
REM Check if DHCP is enabled (automatic IP)
REM Extract whether DHCP is enabled or not from the adapter's IP configuration
for /f "tokens=2 delims=:" %%A in ('netsh interface ip show config name^="%adapter_name%" ^| findstr /C:"DHCP enabled"') do set "dhcp_status=%%A"
REM Remove any leading spaces from the dhcp_status variable
set "dhcp_status=%dhcp_status: =%"
REM Conditional logic to toggle IP configuration
if "%dhcp_status%"=="Yes" (
REM If DHCP is enabled, set the static IP and subnet mask
netsh interface ip set address name="%adapter_name%" static %ip_address% %subnet_mask%
echo DHCP was enabled. Changed IP to %ip_address% with subnet mask %subnet_mask%.
echo Note: IP %ip_address% is used for TP-Link router recovery mode.
) else (
REM If DHCP is not enabled, revert to automatic IP configuration
netsh interface ip set address name="%adapter_name%" source=dhcp
echo Static IP was set. Now switched to obtaining IP address automatically.
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment