1.代码
REM 这是一个注释:告诉系统,这是UTF8格式,否则无法识别中文
chcp 65001
@echo off
REM 设置字体颜色
color 0A
:start
set mask=255.255.255.0
REM 设置本机网络名称
set interface=以太网
echo 选择你要切换的网络类型:
echo 1.固定ip
echo 2.动态ip
set /p type=请输入编号:
if %type%==1 (
call:getNetData
) else if %type%==2 (
call:getDynamic
) else (
echo 输入有误,请重新输入
goto start
)
pause
goto:eof
:getDynamic
netsh interface ip set address "%interface%" dhcp
if %errorlevel%==0 (
echo 已切换为动态ip如下:&& cmd /c ipconfig
)
goto:eof
:getNetData
:start1
set /p ip=请输入ip地址:
set /p gateway=请输入网关地址:
netsh interface ip set address "%interface%" static %ip% %mask% %gateway%
if %errorlevel%==0 (
echo 已切换为固定ip:%ip%
) else (
goto start1
)
goto:eof
2.使用说明
将以上代码拷贝到文本文件中,并重名为.bat后缀的文件。
创建.bat的桌面快捷方式并以管理员运行。
根据个人电脑的网络名称修改代码块中的第十一行内容:set interface=以太网,将以太网修改为对应的网络名称。