Skip to content

Instantly share code, notes, and snippets.

@happyincent
Created August 20, 2018 07:57
Show Gist options
  • Save happyincent/7318da7a88a3cfda0254d10b7d3a4dc8 to your computer and use it in GitHub Desktop.
Save happyincent/7318da7a88a3cfda0254d10b7d3a4dc8 to your computer and use it in GitHub Desktop.
:: Author: DDL
:: Date: Aug. 20, 2018
@ECHO off
SETLOCAL enableDelayedExpansion
chcp 65001
SET "IP_Addr=140.116.0.0"
SET "Sub_Mask=255.255.255.224"
SET "D_Gate=140.116.0.0"
SET "DNS1=8.8.8.8"
SET "DNS2=8.8.4.4"
::-------------------GotAdmin-------------------
REM -- Check for permissions
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
REM -- If error flag set, we do not have admin.
if '%errorlevel%' NEQ '0' (
ECHO Requesting administrative privileges...
goto UACPrompt
) else ( goto gotAdmin )
:UACPrompt
ECHO Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
ECHO UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%temp%\getadmin.vbs"
"%temp%\getadmin.vbs"
exit /B
:gotAdmin
if exist "%temp%\getadmin.vbs" ( del "%temp%\getadmin.vbs" )
pushd "%CD%"
CD /D "%~dp0"
::-------------------Check Interface Name-------------------
SET interface_name=
for /F "tokens=1-2 delims=*" %%a in (%0) do (
SET interface_name=%%b
)
if '%interface_name%'=='' GOTO set_interface_name
GOTO main
::-------------------Main-------------------
:main
CLS
netsh interface ipv4 show addresses "%interface_name%" | findstr /r /i /c:"DHCP.*No$" >nul 2>nul
IF ERRORLEVEL 1 ECHO Static IP mode --^> %IP_Addr% ... && GOTO set_static
IF ERRORLEVEL 0 ECHO DHCP mode ... && GOTO set_dhcp
::choice fail
GOTO end
::-------------------SET IP-------------------
:set_static
netsh interface ipv4 SET address name=%interface_name% static %IP_Addr% %Sub_Mask% %D_Gate% 1
IF ERRORLEVEL 1 PAUSE & GOTO set_interface_name
IF ERRORLEVEL 0 ipconfig /renew %interface_name% && GOTO set_dnsservers
:set_dhcp
netsh interface ipv4 SET address name=%interface_name% source=dhcp
IF ERRORLEVEL 1 PAUSE & GOTO set_interface_name
IF ERRORLEVEL 0 ipconfig /renew %interface_name% && GOTO set_dnsservers
:set_dnsservers
netsh interface ipv4 delete dnsservers name=%interface_name% all >nul
netsh interface ipv4 SET dnsserver name=%interface_name% static %DNS1% primary validate=no
netsh interface ipv4 add dnsserver name=%interface_name% %DNS2% index=2 validate=no
ECHO Finished.
PAUSE
GOTO end
::-------------------SET Interface Name-------------------
:set_interface_name
SET count=0
SET choices=""
CLS
ECHO Select Interface Name
ECHO.
for /f "skip=2 tokens=3*" %%A in ('netsh interface show interface') do (
SET /a count+=1
SET arr!count!=%%B
SET choices=!choices!!count!
echo [!count!] %%B
)
ECHO.
choice /C !choices! /M "Select Ethernet Interface: " /N
SET level=%ERRORLEVEL%
::choice fail
if %level% EQU 0 GOTO set_interface_name
::choice success
SET interface_name="!arr%level%!"
::write to file
ECHO.>> %0
ECHO ::REM*%interface_name%>> %0
::goto next
GOTO main
::--------------------------------------
:end
ENDLOCAL
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment