工作与家庭网络切换.bat

@echo off
setlocal enabledelayedexpansion
netsh wlan set hostednetwork mode=disallow %/*承载网络模式不禁止的话不可使用(默认都没有承载模式)*/%
cls

:: =========== 工作网络配置 ===========
set "IP_set=10.64.210.249"
set "MASK_set=255.255.255.128"
set "GATEWAY_set=10.64.210.251"
set "DNS1_set=10.64.208.108"

:LISTEN
 cls
 set /a g=0
 set /a v=0
 set /a line=0
 
 set f_Host_Name=Name
 set f_Desc=Description
 set f_IP_host=IP
 set f_MAC_host=Physical
 
 :: 参数分离
 for /f "tokens=2* delims=:" %%i in ('ipconfig/all^|findstr %f_Desc%') do (
  set /a v+=1
  set /a g+=1
  if !v! equ !g! (
   set /a line+=1
   set Desc_!line!=%%i
  )
 )
 :: 网卡跳转,最多只支持三网卡
 if 1 LEQ %line% (
  if "%Desc_1%" neq "%Desc_1:Network=%" (
   set num=1
  )
 )
 if 2 LEQ %line% (
  if "%Desc_2%" neq "%Desc_2:Network=%" (
   set num=2
  )
 
 )
 if 3 LEQ %line% (
  if "%Desc_3%" neq "%Desc_3:Network=%" (
   set num=3
  )
 )
 
 :: 语言跳转
 if %line% EQU 0 (
  goto LISTCN
 )
 
 if "%num%" LEQ "%line%" (goto GETCRDINFO)
 echo 输入错误,请重新输入.
 pause>nul
 cls
goto LISTEN

:LISTCN
 cls
 set /a g=0
 set /a v=0
 set /a line=0
 
 set f_Host_Name=主机名
 set f_Desc=描述
 set f_IP_host=IPv4
 set f_MAC_host=物理地址
 
 :: 参数分离
 for /f "tokens=2* delims=:" %%i in ('ipconfig/all^|findstr %f_Desc%') do (
  set /a v+=1
  set /a g+=1
  if !v! equ !g! (
   set /a line+=1
   set Desc_!line!=%%i
  )
 )
 :: 网卡跳转,最多只支持三网卡
 if 1 LEQ %line% (
  if "%Desc_1%" neq "%Desc_1:Network=%" (
   set num=1
  )
 )
 if 2 LEQ %line% (
  if "%Desc_2%" neq "%Desc_2:Network=%" (
   set num=2
  )
 
 )
 if 3 LEQ %line% (
  if "%Desc_3%" neq "%Desc_3:Network=%" (
   set num=3
  )
 )
 
 if "%num%" LEQ "%line%" (goto GETCRDINFO)
 echo 输入错误,请重新输入.
 pause>nul
 cls
goto LISTCN

:: 获取网卡路径
:GETCRDINFO
 :: Host_Name
 for /f "tokens=2* delims=:" %%i in ('ipconfig/all^|findstr %f_Host_Name%') do (
  set Host_Name=%%i
 )
 :: IP_host
 set /a k=0
 for /f "tokens=2* delims=:" %%i in ('ipconfig/all^|findstr %f_IP_host%') do (
  set /a k=k+1
  if !k! equ !num! (
   set IP_host=%%i
  )
 )
 :: MAC_host
 set /a k=0
 for /f "tokens=2* delims=:" %%i in ('ipconfig/all^|findstr %f_MAC_host%') do (
  set /a k=k+1
  if !k! equ !num! (
   set MAC_host=%%i
  )
 )
goto SHOW

:SHOW
 :: 过滤字符串
 set Host_Name=%Host_Name: =%
 set IP_host=%IP_host: =%
 set IP_host=%IP_host:(=%
 set IP_host=%IP_host:首选=%
 set IP_host=%IP_host:)=%
 set MAC_host=%MAC_host: =%
 :: 显示信息
 echo Host_Name:[%Host_Name%]
 echo IP_host  :[%IP_host%]
 echo MAC_host :[%MAC_host%]
 :: 对比IP,匹配工作网络则设为动态IP,否则设为工作IP
 if "%IP_host%"=="%IP_set%" (
  echo 网络环境为工作网络
  echo 正在转换为家庭网络...
  echo 正在设置自动IP,请稍等......
  netsh interface ip set address name = "本地连接" source = dhcp
  echo 正在设置自动DNS ,请稍等......
  netsh interface ip set dns "本地连接" source = dhcp 
 ) else (
  echo 网络环境为家庭网络
  echo 正在转换为工作网络...
  echo 正在设置静态IP,请稍等......
  netsh interface ip set address "本地连接" source = static addr = %IP_set% mask=%MASK_set%
  echo 正在设置静态网关,请稍等......
  netsh interface ip set address "本地连接" gateway = %GATEWAY_set% gwmetric =1
  echo 正在设置首选DNS ,请稍等......
  netsh interface ip set dns "本地连接" source = static addr = %DNS1_set%
 )
 echo 转换完成
pause>nul

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值