便捷更改IP批处理
先上效果图
功能:
可以把常用的静态IP写到里面,方便直接更改。也可以临时修改一个自定义的IP或动态IP。在主界面直接输入数字或字母即可执行对应的选项。
更改网络名称
需要用记事本编辑一下网络名称。
把第3行的变量Eth修改为你要编辑的网络名称。否则是不会更改任何adapter的IP的。
set Eth="要配置的网卡的名字"
整体代码
如下:
@echo off
set Eth="Ethernet"
set MaxNum=7
set answer=u
:menu
color F1
cls
call :showStatus
echo. & echo.
call :showMenu
set /p answer=
color 0F
if %answer% lss %MaxNum%+1 goto writeValue
if %answer%==c goto c
if %answer%==d goto d
if %answer%==r goto r
if %answer%==s goto s
if %answer%==h goto h
if %answer%==i goto i
if %answer%==e goto e
echo please enter correct opction
pause
goto menu
: writeValue
color 72
cls
if %answer%==1 (
set Addr=169.254.1.128
set Mask=255.255.0.0
set Gway=169.254.1.1)
if %answer%==2 (
set Addr=192.168.0.7
set Mask=255.255.255.0
set Gway=192.168.0.80)
if %answer%==3 (
set Addr=10.186.92.12
set Mask=255.255.255.0
Set Gway=10.186.92.1)
if %answer%==4 (
set Addr=192.168.250.128
set Mask=255.255.255.0
set Gway=192.168.250.1)
if %answer%==5 (
set Addr=192.168.254.128
set Mask=255.255.255.0
set Gway=192.168.254.100)
if %answer%==6 (
set Addr=10.10.1.128
set Mask=255.255.255.0
Set Gway=10.10.1.10)
if %answer%==7 (
set Addr=100.100.100.128
set Mask=255.255.255.0
Set Gway=100.100.100.100)
call :setIP
goto menu
:c
color 72
cls
echo.
echo.
echo.
echo NOTE!
echo default setting is:
echo Addr=192.168.0.16
echo Mask=255.255.255.0
echo Gway=192.168.0.1
echo press Enter to skip for default setting
echo.
echo.
echo type the IP address and press "Enter"
set /p Addr=
echo type the Mask and press "Enter"
set /p Mask=
echo type the GateWay and press "Enter"
Set /p Gway=
if "%Addr%"=="" set Addr=192.168.0.16
if "%Mask%"=="" set Mask=255.255.255.0
if "%Gway%"=="" Set Gway=192.168.0.1
call :setIP
goto menu
:showStatus
echo **************
echo current IP:
for /f "tokens=4" %%a in ('route print^|findstr 0.0.0.0.*0.0.0.0') do (echo %%a)
echo **************
goto :eof
:showMenu
echo static IP
echo 1 -- 169.254.1.128 --- description 1
echo 2 -- 192.168.0.7 ----- description 2
echo 3 -- 10.186.92.12 ---- description 3
echo 4 -- 192.168.250.128 - description 4
echo 5 -- 192.168.254.128 - description 5
echo 6 -- 10.10.1.128 ----- description 6
echo 7 -- 100.100.100.128 - description 7
echo.
echo c set a customer define IP address
echo d set dynamic IP
echo r run "route print -4"
echo s run "ipconfig /all"
echo h help
echo i info
echo e exit
echo.
echo please input an option for adapter %Eth% and press "Enter"
goto :eof
:setIP
cls
call :showMenu
echo configuring the IP address, please wait ....
netsh interface ipv4 set address name=%Eth% source=static addr=%Addr% mask=%Mask% gateway=%Gway%
rem ping -n 5 127.0.0.1>nul
echo done!presss any key to return to menu! (wait 5sec is recommanded, to make ip shown normally)
pause
goto :eof
:d
color 72
cls
call :showMenu
echo configuring the IP address, please wait ....
echo configuring the IP to Dynamic, please wait ....
netsh interface ip set address %Eth% dhcp
netsh interface ip set dns %Eth% dhcp
echo done
pause
goto menu
:s
ipconfig /all
pause
goto menu
:r
route print -4
pause
goto menu
:h
color 2F
cls
echo please edit value of "Eth" into the name of the adapter you want to change
echo if the adapter name doesn't exist, nothing will be changed
echo make sure to run as administrator
echo please edit value of "MaxNum" if new IP added, and add the new IP to ": writeValue"
pause
goto menu
:i
color 2F
cls
echo #####change IP#####
echo.
echo ┭ V02. date Jul 7th 2020 by lewis
echo ├ debug dynamic ip setting
echo ├ show all ip address on top
echo ├ cancel DNS setting
echo └ change font color of main menu
echo ┭ V03. date Jul 7th 2021 by lewis
echo ├ add ipconfig /all command
echo └ show name of adapter under control in main menu
echo. & echo. & echo.
pause
goto menu
:e
echo BYE!
pause
如何追加一个选项
可以在writeValue追加或更改你常用的IP地址mask和gateway。
比如要追加第8条内容,需要以下修改:
- 在call setIP之前插入如下
if %answer%==8 (
set Addr=192.168.32.100
set Mask=255.255.255.0
Set Gway=192.168.32.1)
- 然后在showenu里加入如下代码。以便在菜单里显示要更改的IP和信息。
echo 8 -- 192.168.32.100 - 你自己的描述
- 最后把第4行 MaxNum改成8
set MaxNum=8
其它
其它功能大家可以自己摸索,不是太难。
有问题请留言。