来自: http://hi.baidu.com/coolggg/blog/item/0958a3106ded4f0c213f2ef7.html
由于工作环境的变换或内外网切换时,使ip地址不得不频繁的更换 ,为了避免更改ip地址带来的麻烦,用dos命令做一个批处理文件来实现ip地址和dns地址的快速更改。一、命令介绍
这里用到的DOS命令是:netsh interface ip set (address/dns) 或简写为 netsh int ip set (address /dns)用法格式:
netsh interface ip set address[name=]<string>
[[source=]dhcp |
[source=] static [addr=]IP address [mask=]IP subnet mask]
[[gateway=]<IP address>|none [gwmetric=]integer]
netsh interface ip set dns [name=]<string> [source=]dhcp|static [addr=]<IP address>|none
[
参数说明:
name - 接口名称。
source - 下列值之一:
dhcp: 对于指定接口,设置用 DHCP 配置 IP地址或DNS服务器。
static: 设置使用本地静态配置设置 IP地址或DNS服务器。
gateway - 下列值之一:
<IP address>: 您设置的 IP 地址的指定默认网关。
none: 不设置默认网关。
gwmetric - 默认网关的跃点数。如果网关设置为 ‘none’,则不应设置此字段。
只有在 ’source’ 为 ’static’ 时才设置下列选项:
addr - 指定接口的 IP 地址。
mask - 指定 IP 地址的子网掩码。
register - 下面的值之一:
none: 禁用动态 DNS 注册。
primary: 只在主 DNS 后缀下注册。
both: 在主 DNS 后缀下注册,也在特定连接后缀下注册。
二、制作批处理文件(以内外网为例)
设“本地连接”上外网时ip地址为自动获取,DNS为202.99.224.8;上内网是ip地址为192.168.1.123 掩码255.255.255.0 网关192.168.1.1,DNS为自动获取。
上外网的代码是
@echo off
set dns=202.99.224.8
netsh interface ip set address name=”本地连接” source=dhcp
netsh interface ip set dns name=”本地连接” source=static addr=%dns%
把这段代码复制到一个文本里面,将文本另存为“外网ip.bat”(文件名可随意,扩展名必须为bat)。
上内网的代码
@echo off
set name=”本地连接”
set ipaddress=192.168.1.123
set mask=255.255.255.0
set gateway=192.168.1.1
netsh interface ip set address name=%name% source=static addr=%ipaddress% mask=%mask% gateway=%gateway% 1
netsh interface ip set dns name=”本地连接” source=dhcp
把这段代码复制到一个文本里面,将文本另存为“内网ip.bat”。
上内网的时候换上内网网线,执行“内网ip.bat”,上外网的时候换上外网的网线,执行“外网ip.bat”。
大家在使用时根据自己的情况根改IP地址,掩码,网关和DNS。
批处理修改IP | |
|