.bat文件设置IP、DNS

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

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

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 interface ip set address "Ethernet" static 192.168.1.199 255.255.255.0 192.168.1.10 1
netsh interface ip set dnsservers "Ethernet" static 202.103.44.151

4.管理员权限运行

修改后的IP和DNS

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

时间: 2024-10-26 12:54:08

.bat文件设置IP、DNS的相关文章

windows bat 文件设置JAVA_HOME 环境变量

背景介绍: 原来安装的MyEclipse 8.5 为32位的,前段时间开始学习Android开发,搞了一个adt-bundle 64位的,经常需要更改java版本,所以写了两个bat文件来修改JAVA_HOME bat内容 @echo off&setlocal enabledelayedexpansion&title 更改JAVA_HOME环境变量 color 0A cls echo======================================================

Ubuntu 设置IP DNS

具体步骤如下: 1.找到文件并作如下修改: sudo vi /etc/network/interfaces 修改结果为如下部分: auto lo iface lo inet loopback auto p2p1 iface p2p1 inet static address 192.168.1.244 gateway 192.168.1.1 netmask 255.255.255.0 注:p2p1为机器的网卡名,不同服务器网卡名不同,请自行修改 2. 修改dns解析 因为以前是dhcp解析,所以会

针对访问的文件设置IP地址

LAMP的discuz论坛 如果对部分目录设置允许访问IP地址 在虚拟机的配置中,增加 <filesmatch "(.*)admin(.*)">             Order deny,allow             Deny from all             Allow from 127.0.0.1             Allow from  2.2.2.2 </filesmatch> 那么所有 域名下的  还有admin的文件类型 都只

RHEL 7.5 设置IP地址,及常用基础网络命令

#nmtui 图形化设置IP地址 =================== #vim /etc/sysconfig/network-scripts/ifcfg-ens33 通过文件设置IP地址 TYPE=Ethernet PROXY_METHOD=none BROWSER_ONLY=no BOOTPROTO=static DEFROUTE=yes IPV4_FAILURE_FATAL=no IPV6INIT=yes IPV6_AUTOCONF=yes IPV6_DEFROUTE=yes IPV6_

自动设置ip bat文件 虚函数重载

实现软件启动的时候就自动修改ip地址,可以自动调用 重载项目ForcePlate.cpp中的InitInstance()函数,并在该初始化函数中加 ShellExecute(NULL, _T("open"), _T("1.bat"),NULL, m_strEXEPath, SW_SHOW); 1.bat文件:(修改ip) netsh int ip set addr name="本地连接" source=static addr=192.168.2.

自动设置IP地址和自动获取IP地址bat批处理文件

自动设置IP地址.bat Echo offecho  手动设置IP地址....Netsh interface IP Set Addr "本地连接" Static 192.168.1.8 255.255.255.0 192.168.1.1 1echo  手动设置DNS地址....Netsh interface IP Set dns "本地连接" static 202.106.196.115 primaryecho  手动设置备份DNS地址....Netsh inter

RHEL7设置IP地址、网关和DNS

█网卡命名格式:enoxxxxx en:代表enthernet(以太网) o:代表onboard(内置) xxxxx:是主板的某种索引编号自动生成,以便保证其唯一性 ●方法1:进入网络配置文件,修改网卡配置文件 TYPE="Ethernet|Bridge"    #网络接口类型 BOOTPROTO="static|none"    #启动静态IP地址(BOOTPROTO=dhcp启用动态获取IP) DEFROUTE="yes" IPV4_FAIL

SUSE Linux Enterprise Server设置IP地址、网关、DNS

说明: ip:202.118.83.247 子网掩码:255.255.255.0 网关:202.118.83.2 dns:8.8.8.8 / 8.8.4.4 1.设置ip $ vi /etc/sysconfig/network/ifcfg-eth0 #编辑配置文件 1 BOOTPROTO=’static’ #静态IP 2 BROADCAST=’′ #广播地址 3 IPADDR=’202.118.83.247′ #IP地址 4 NETMASK=’255.255.255.0′ #子网掩码 5 NET

Centos 7 安装 设置 IP地址,DNS,主机名,防火墙,端口 (实测+笔记)

环境: 系统硬件:vmware vsphere (CPU:2*4核,内存2G,双网卡) 系统版本:CentOS-7.0-1406-x86_64-DVD.iso 安装步骤: 1.虚拟系统安装 1.1 使用VMware 虚拟机,读入ISO进行安装.启动后进行安装界面 1.2 选择 Install CentOS7,进行语言选择 1.3 选择简体中文,进入信息摘要 1.4 点击"安装位置",选择系统需要安装到的硬盘,点击“完成”. 1.5 点击"网络和主机名",进行网络设置