批处理自动切换ip地址与网络的启用、禁用

启用禁用网络

@echo off 
 
:: BatchGotAdmin 
:------------------------------------- 
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" 
:-------------------------------------- 
 
cls
@ECHO OFF
title 启用或禁用网络
CLS
color 0a
GOTO MENU
:MENU
ECHO.
ECHO. ==============启用禁用本地连接==============
ECHO.
ECHO. 1 禁用本地连接
ECHO. 2 启用本地连接
ECHO. 3 禁用WIFI
ECHO. 4 启用WIFI
ECHO. 0 退 出
ECHO. ==========================================
ECHO.
ECHO.
echo. 请输入选择项目的序号:
set /p ID=
if "%id%"=="1" goto qiyong
if "%id%"=="2" goto jinyong

if "%id%"=="3" goto DISABLED_WIFI
if "%id%"=="4" goto enable_wifi

if "%id%"=="0" exit
PAUSE
:qiyong
echo 禁用本地连接
netsh interface set interface name="以太网" admin=DISABLED
goto MENU
:jinyong
echo 启用本地连接
netsh interface set interface name="以太网" admin=ENABLED
GOTO MENU

:DISABLED_WIFI
echo 禁用WIFI
netsh interface set interface name="WLAN" admin=DISABLED
goto MENU
:enable_wifi
echo 启用WIFI
netsh interface set interface name="WLAN" admin=enable
GOTO MENU









切换ip地址

@echo off
title 切换IP地址
rem off 就会只显示echo的描述
rem 延迟变量,变量获得及时性
setlocal enabledelayedexpansion
color 71
%1 start "" mshta vbscript:CreateObject("Shell.Application").ShellExecute("cmd.exe","/c ""%~s0"" ::","","runas",1)(window.close)&&exit

:startip

set NAME=以太网
set MASK=255.255.255.0


echo 修改ip地址:0:退出,1:自动获取ip, 2:手动输入ip地址,ip:网络配置
echo yz:渝中,yc:永川...(首字母小写)
set sel=
set/p sel=请选择修改方式:
if "%sel%"=="0" goto end
if "%sel%"=="1" goto auto
if "%sel%"=="2" goto inputIp
if "%sel%"=="ip" goto ipconfig

if "%sel%"=="yz" goto yz
if "%sel%"=="yc" goto yc

echo 您没有选择修改方式。
goto end

:yc
set IP=10.0.0.0
set GATE=10.0.0.254
goto setIp

:yz
set IP=172.0.0.0
set MASK=255.255.254.0
set GATE=172.0.0.254
echo 更改为%sel%!
goto setIp
 
 
 
:auto "以太网"如果网络名称带后缀的话中间有空格
::netsh interface ip set address name ="以太网 5" source = dhcp
::netsh interface ip set dns name ="以太网 5" source = dhcp
netsh interface ip set address name=%NAME% source=dhcp
netsh interface ip delete dns %NAME% all
ipconfig /flushdns
echo 更改IP地址完成!
goto startip

:inputIp 
cls
Call :displayIPinfo disp
echo.
set /p IPin= IP地址: 
set /p "MASK= 子网掩码:【按下回车为%MASK%】"
set /p GATEin= 网  关: 
echo..........
netsh interface ip set address %NAME% static %IPin% %MASK% %GATEin% 
echo.请稍等...
choice /t 4 /d y /n >nul
echo..........
cls
Call :displayIPinfo disp
echo.
goto startip

:setIp
netsh interface ipv4 set address %NAME% static %IP% %MASK% %GATE%
choice /t 4 /d y /n >nul
echo..........
::cls
Call :displayIPinfo disp
echo 更改IP地址完成!"网络名称:%NAME%  地址:%IP% 掩码:%MASK% 网关:%GATE%"
goto startip

:ipconfig
ipconfig/all
goto startip

:displayIPinfo
for /f "tokens=2 delims=:" %%i in ('ipconfig^|findstr "IPv4"')     do set IPnow=%%i
for /f "tokens=2 delims=:" %%i in ('ipconfig^|findstr "子网掩码"') do set MASKnow=%%i
for /f "tokens=2 delims=:" %%i in ('ipconfig^|findstr "默认网关"') do set GATEnow=%%i
echo ------------------------------------------
echo 当前IP地址  :%IPnow%
echo 当前子网掩码:%MASKnow%
echo 当前默认网关:%GATEnow%


 
:end
rem pause

你可能感兴趣的:(TCP/IP,bat,网络,tcp/ip,windows)