您当前的位置:首页 > 计算机 > 编程开发 > DOS/BAT与(脚本)

bat切换IP地址

时间:03-27来源:作者:点击数:

背景

部分环境下,需要切换IP地址才能更换网络,频繁手动切换浪费时间

PS:无法配置多IP

实现代码

@echo off
:: 设置你的网络接口名称
set "adapterName=以太网 3"  :: 修改为你的网络接口名称

:: 定义两个 IP 地址配置
set "ip1=1.1.1.63"
set "subnet1=255.255.255.0"
set "gateway1=1.1.1.254"

set "ip2=1.10.1.63"
set "subnet2=255.255.255.0"
set "gateway2=1.10.1.254"

:: 获取当前 IP 地址
for /f "tokens=3 delims=: " %%a in ('netsh interface ip show address "%adapterName%" ^| findstr /C:"IP 地址"') do (
    set "currentIP=%%a"
)


:: 切换 IP 地址
if "%currentIP%"=="%ip1%" (
    echo 当前 IP 是 %ip1%,正在切换到 %ip2%...
    netsh interface ip set address name="%adapterName%" static %ip2% %subnet2% %gateway2% 1
    echo 已切换到 %ip2%
) else (
    echo 当前 IP 是 %currentIP%,正在切换到 %ip1%...
    netsh interface ip set address name="%adapterName%" static %ip1% %subnet1% %gateway1% 1
    echo 已切换到 %ip1%
)

pause

注意事项

1、bat需要ansi格式

2、执行需要管理员权限

方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门
本栏推荐