这几天遇到个烦心事,每次开机之后都要去手动去设置一下IP地址,一大串的数字还是有点麻烦,于是就想写个批处理文件设置IP

注意:在DOS下设置IP时需要管理员权限运行

1.查看机子设置IP需要用到的名字

名字是Ethernet

2.查看机子在DOS下设置IP和DNS命令的格式

C:\Windows\system32>netsh
netsh>interface
In future versions of Windows, Microsoft might remove the Netsh functionality
for TCP/IP.

Microsoft recommends that you transition to Windows PowerShell if you currently
use netsh to configure and manage TCP/IP.

Type Get-Command -Module NetTCPIP at the Windows PowerShell prompt to view
a list of commands to manage TCP/IP.

Visit http://go.microsoft.com/fwlink/?LinkId=217627 for additional information
about PowerShell commands for TCP/IP.
netsh interface>ip
netsh interface ipv4>set address
One or more essential parameters were not entered.
Verify the required parameters, and reenter them.
The syntax supplied for this command is not valid. Check help for the correct syntax.

Usage: set address [name=]<string>
[[source=]dhcp|static]
[[address=]<IPv4 address>[/<integer>] [[mask=]<IPv4 mask>]
[[gateway=]<IPv4 address>|none [gwmetric=]<integer>]
[[type=]unicast|anycast]
[[subinterface=]<string>]
[[store=]active|persistent]

Parameters:

Tag Value
name - Interface name or index.
source - One of the following values:
dhcp: Enables DHCP for configuring IP addresses for
the specified interface.
static: Disables DHCP for configuring IP addresses for
the specified interface. This value must be
specified if an address or a gateway is being
configured.
address - IPv4 address to add or modify, optionally followed by
the subnet prefix length.
mask - The IP subnet mask for the specified IP address.
gateway - One of the following values:
<IPv4 address>: A specific default gateway for the
static IP address you are setting.
none: No default gateways are set. This is the default.
gwmetric - The metric for the default gateway. This field should
be set only if gateway is specified.
type - One of the following values:
unicast: Marks the address as a unicast address.
This is the default.
anycast: Marks the address as an anycast address.
subinterface - LUID of the subinterface on which the default gateway
exists. This parameter is only needed on interfaces
with multiple subinterfaces.
store - One of the following values:
active: Set only lasts until next boot.
persistent: Set is persistent. This is the default.

Remarks: Used to enable or disable DHCP for IP address configuration.
Also removes any previous static IP addresses and default gateways,
and can add a new static IP address and default gateway.

Examples:

set address name="Wired Ethernet Connection" source=dhcp
set address "Wired Ethernet Connection" static 10.0.0.9 255.0.0.0 10.0.0.1 1

netsh interface ipv4>set dns
One or more essential parameters were not entered.
Verify the required parameters, and reenter them.
The syntax supplied for this command is not valid. Check help for the correct syntax.

Usage: set dnsservers [name=]<string> [source=]dhcp|static
[[address=]<IP address>|none]
[[register=]none|primary|both]
[[validate=]yes|no]

Parameters:

Tag Value
name - The name or index of the interface.
source - One of the following values:
dhcp: Sets DHCP as the source for configuring DNS
servers for the specific interface.
static: Sets the source for configuring DNS servers
to local static configuration.
address - One of the following values:
<IP address>: An IP address for a DNS server.
none: Clears the list of DNS servers.
register - One of the following values:
none: Disables Dynamic DNS registration.
primary: Register under the primary DNS suffix only.
both: Register under both the primary DNS suffix, as
well as under the connection-specific suffix.
validate - Specifies whether validation of the DNS server setting
will be performed. The value is yes by default.

Remarks: Sets DNS server configuration to either DHCP or static mode. Only
when source is 'static', is the 'addr' option also available for
configuring a static list of DNS server IP addresses for the
specified interface. If Validate switch is yes, then
the newly set DNS server is validated.

Examples:

set dnsservers name="Wired Ethernet Connection" source=dhcp
set dnsservers "Wired Ethernet Connection" static 10.0.0.1 primary

netsh interface ipv4>

格式分别是

set address name="Wired Ethernet Connection" source=dhcp  (自动获取IP)
  set address "Wired Ethernet Connection" static 10.0.0.9 255.0.0.0 10.0.0.1 1 (静态设置)

set dnsservers name="Wired Ethernet Connection" source=dhcp (自动获取DNS)
  set dnsservers "Wired Ethernet Connection" static 10.0.0.1 primary (静态设置)

3.写批处理文件

在家里时自动分配IP

HOME.bat

netsh interface ip set address "Ethernet" source=dhcp
netsh interface ip set dns "Ethernet" source=dhcp

上班时

WORK.bat

netsh
netsh interface ip set dnsservers "Ethernet" static 202.103.44.151

4.管理员权限运行

修改后的IP和DNS

有了这两个文件之后就不用手动配啦

最新文章

  1. Nodejs websocket入门
  2. oc程序编译【-framework Foundation 编译】
  3. 【Valid Number】cpp
  4. mysql绿色版安装问题解决(ERROR 2003 (HY000): Can&#39;t connect to MySQL server on &#39;localhost&#39; (10061))
  5. eclipse:eclipse for java EE环境下如何配置tomcat服务器,并让tomcat服务器显示在控制台上,将Web应用部署到tomcat中
  6. php require、require_once和include、include_once的区别
  7. C# WinForm程序退出的方法比较
  8. 用python代码模拟登录网站
  9. java数组冒泡排序
  10. Android 开发 上拉加载更多功能实现
  11. ARM常用汇编指令列表 --- 转自百度文库
  12. vi快捷键使用大全
  13. 开发升级-微信小程序(感谢黄秀杰老师)
  14. textbox只允许输入数字
  15. 【JQuery】数据
  16. PHP session过期机制和配置
  17. MyBatis 学习记录3 MapperMethod类
  18. SpringBoot项目中获取applicationContext对象
  19. oracle数据库创建表
  20. Android开发之EditText利用键盘跳转到下一个输入框

热门文章

  1. 从url中提取参数名和参数值(转)
  2. js获取光标位置
  3. iOS开发之指纹解锁
  4. 起步X5 的铛铛的安装部署过程
  5. ucos任务控制块详解
  6. 总结分享十大iOS开发者最喜爱的库 分类: ios相关 app相关 2015-04-03 16:43 320人阅读 评论(0) 收藏
  7. 每个Javascript开发者都应当知道的那些事
  8. Java编程思想非主流知识点
  9. sqlserver 设置外键
  10. Mysql和Oracle的一些语法区别