menu Chancel's blog
rss_feed
Chancel's blog
有善始者实繁,能克终者盖寡。

Windows下双击脚本切换IP

作者:Chancel Yang, 创建:2021-06-16, 字数:2555, 已阅:70, 最后更新:2024-03-10

Windows的笔记本切换IP非常不方便,哪怕2023年微软切换IP依旧做的拉跨,笔记本在家是固定IP和固定网关,出门在外就需要是DHCP

于是自己实现了一个快速切换IP的双击脚本,根据需要修改:

  • net_interface 是你的网卡适配器名称,在控制面板-网络连接-更改适配器中找到你的网卡适配器名称
  • IP1 是你的固定IP信息

如果有多个静态IP,可以参考IP1的写法,增加IP2IP3...

Batchfile
@echo off
title Switch IP network segment.

::Set background color
setlocal EnableDelayedExpansion&color 3e & cd /d "%~dp0"
%1 %2

mshta vbscript:createobject("shell.application").shellexecute("%~s0","goto :runas","","runas",1)(window.close)&goto :eof
:runas

:: -------- Please modify the network port interface and network segment information. ---:: Network interface name
set net_interface=Wi-Fi

:: IP1
set name1=Home
set ip1=192.168.1.20
set mask1=255.255.255.0
set gateway1=192.168.1.1
set dns1_1=192.168.1.1
set dns1_2=192.168.1.1

:: -------------------------------------------------
:CHOICE
cls
echo.
echo                        Please select your network.
echo.
echo --------------------------------------------------------------------------------                                                        
echo             1. Update to DHCP
echo             2. Update to %name1%(%ip1%/%gateway1%)
echo             3. Restart your network interface
echo.
echo --------------------------------------------------------------------------:mu
set /p choice=My selection is:
IF NOT "%choice%"=="" SET choice=%choice:~0,1%
if /i "%choice%"=="1" goto DHCP
if /i "%choice%"=="2" goto IP1
if /i "%choice%"=="3" goto REBOOT

echo.
echo.
echo "Oops, your input is incorrect. Please try again."
echo.
echo.
GOTO CHOICE

:IP1
echo.
echo  -----------%name1%-----echo       ip:	%ip1%
echo     mask:	%mask1%
echo gateway1:	%gateway1%
echo     dns1:	%dns1_1%
echo     dns2:	%dns1_2%

::Set IP/gateway/subnet mask.
cmd /c netsh interface ip set address "%net_interface%" source=static addr=%ip1% mask=%mask1% gateway=%gateway1%

::Set DNS
cmd /c netsh interface ip set dnsservers "%net_interface%" static %dns1_1% validate=no
cmd /c netsh interface ip add dnsservers "%net_interface%" %dns1_2% index=2 validate=no

ipconfig /flushdns
echo Set network success
GOTO EXIT

:DHCP
echo.
echo  ---Set IP/DNS to automatically obtain.
cmd /c netsh interface ip set address name="%net_interface%" source=DHCP
cmd /c netsh interface ip set dns "%net_interface%" source=DHCP
echo Set network success
GOTO EXIT

:REBOOT
cmd /c netsh interface set interface "%net_interface%" disabled
cmd /c netsh interface set interface "%net_interface%" enable
echo Set network success
GOTO EXIT

:EXIT
echo.
pause | echo Press any key to exit the program.
exit /b

[[replyMessage== null?"发表评论":"发表评论 @ " + replyMessage.m_author]]

account_circle
email
web_asset
textsms

评论列表([[messageResponse.total]])

还没有可以显示的留言...
gravatar
[[messageItem.m_author]] [[messageItem.m_author]]
[[messageItem.create_time]]
[[getEnviron(messageItem.m_environ)]]
[[subMessage.m_author]] [[subMessage.m_author]] @ [[subMessage.parent_message.m_author]] [[subMessage.parent_message.m_author]]
[[subMessage.create_time]]
[[getEnviron(messageItem.m_environ)]]