一些公司可能会设置分配静态ip来管理网络,只有设置公司给你的静态ip才能上网,但是家里的网络可能就需要你使用动态IP才能上网,这时你需要一个切换ip的脚本来帮助你方便切换网络。
工具/原料
- 电脑
- 记事本
方法/步骤
- 1
对于频繁切换ip的人来说,使用如下图:手动设置ip已经out了,你需要一个能够快速切换ip的脚本。
- 2
首先你需要定义你的网络名称和要切换的静态ip地址、子网掩码、网关、DNS和备用DNS。你可以这样写:
@echo off
rem //设置网络变量
set NetName="以太网"
rem //设置IP地址、子网掩码、网关、DNS
set Addr=192.168.xx.xxx
set Mask=255.255.255.0
set Gway=192.168.xx.xxx
set Dns=192.168.xx.xxx
set DnsRem=
网络名称如下图:
- 3
接着加入命令行中的选项功能,命令如下:
echo 请根据自己的需求选择下面的选项并回车
echo ●●●●●●●●●●●●●●●●●●
echo ● 1 设置为静态IP ●
echo ● 2 设置为动态IP ●
echo ● 3 退出 ●
echo ●●●●●●●●●●●●●●●●●●
set /p InputNum=
if %InputNum%==1 goto 1
if %InputNum%==2 goto 2
if %InputNum%==3 goto 3
- 4
根据各个选项写入相应的设置:
:1
rem //可以根据自己的需求设置代理,命令为set http_proxy=网络 set http_proxy_user=账号 set http_proxy_pass=密码
echo 静态IP设置中,请稍等...
echo. I P 地址 = %Addr%
echo. 子网掩码 = %Mask%
netsh interface ip set address name=%NetName% source=static addr=%Addr% mask=%Mask% gateway=%Gway% gwmetric=0 >nul
echo. 首选 DNS = %Dns%
netsh interface ip set dns name=%NetName% source=static addr=%Dns% register=PRIMARY >nul
echo. 备用 DNS = %Dns2%
netsh interface ip add dns name=%Nic% addr=%Dns2% index=2 >nul
echo ----------------------------
echo 设置完成!
pause
goto end
:2
echo 动态IP设置中,请稍等...
echo. 从DHCP自动获取IP地址...
netsh interface ip set address "以太网" dhcp
echo. 从DHCP自动获取DNS地址...
netsh interface ip set dns "以太网" dhcp
echo ----
echo 全部设置完成!
pause
goto end
:3
echo bye!
goto end
- 5
脚本编写如下:首先创建一个文本文档
- 6
再在文档中写入:
@echo off
rem //设置网络变量
set NetName="以太网"
rem //设置IP地址、子网掩码、网关、DNS
set Addr=192.168.xx.xxx
set Mask=255.255.255.0
set Gway=192.168.xx.xxx
set Dns=192.168.xx.xxx
set DnsRem=
echo 请根据自己的需求选择下面的选项并回车
echo ●●●●●●●●●●●●●●●●●●
echo ● 1 设置为静态IP ●
echo ● 2 设置为动态IP ●
echo ● 3 退出 ●
echo ●●●●●●●●●●●●●●●●●●
set /p InputNum=
if %InputNum%==1 goto 1
if %InputNum%==2 goto 2
if %InputNum%==3 goto 3
:1
rem //可以根据自己的需求设置代理,命令为set http_proxy=网络 set http_proxy_user=账号 set http_proxy_pass=密码
echo 静态IP设置中,请稍等...
echo. I P 地址 = %Addr%
echo. 子网掩码 = %Mask%
netsh interface ip set address name=%NetName% source=static addr=%Addr% mask=%Mask% gateway=%Gway% gwmetric=0 >nul
echo. 首选 DNS = %Dns%
netsh interface ip set dns name=%NetName% source=static addr=%Dns% register=PRIMARY >nul
echo. 备用 DNS = %Dns2%
netsh interface ip add dns name=%Nic% addr=%Dns2% index=2 >nul
echo ----------------------------
echo 设置完成!
pause
goto end
:2
echo 动态IP设置中,请稍等...
echo. 从DHCP自动获取IP地址...
netsh interface ip set address "以太网" dhcp
echo. 从DHCP自动获取DNS地址...
netsh interface ip set dns "以太网" dhcp
echo ----
echo 全部设置完成!
pause
goto end
:3
echo bye!
goto end
- 7
将文档保存下来并退出来,给新建的文档重新命名为:IP切换.bat。如下图:
END