现在的位置: 首页 > 综合 > 正文

Configure IP Address and DNS from Command Line

2018年03月30日 ⁄ 综合 ⁄ 共 1986字 ⁄ 字号 评论关闭

之前在使用电脑的时候经常遇到需要手动修改ip地址来实现在局域网和独立端口之间切换的问题,在windows下面这个过程显得比较繁琐,在linux下面我们可以通过新建一个vpn链接来实现过个本地链接,在windows下面不能实现这样的功能,所以便产生了用bat脚本来一键修改本地连接ip地址的想法。

基本信息如下:

@echo off 
cls 
color 4A 
title ip_change
echo IP地址更改小工具 
set IP=115.156.219.37
set MASK=255.255.255.224 
set GATEWAY=115.156.219.62
set NAME="本地连接" 

set IP1=192.168.0.37
set MASK1=255.255.255.0 
set GATEWAY1=192.168.0.1
set NAME1="本地连接" 

echo. 
echo 独立端口 请按 1 
echo. 
echo 局域网 请按 2 

set /p KEY= [您的选择是:] 
if %KEY% == 1 goto ONE 
if %KEY% == 2 goto TWO 

:ONE 
echo 正在更改成独立端口...... 
netsh interface ip set address %NAME% static %IP% %MASK% %GATEWAY% 
netsh interface ip add dns %NAME%  202.112.20.131
netsh interface ip add dns %NAME%  202.114.0.242 index=2
echo 更改结束 
exit

:TWO
echo 正在更改成局域网ip
netsh interface ip set address %NAME1% static %IP1% %MASK1% %GATEWAY1% 
netsh interface ip add dns %NAME1%  202.112.20.131
netsh interface ip add dns %NAME1%  202.114.0.242 index=2
echo 更改结束 
exit

====================================分割线=================================================

下面就是netsh用于修改ip信息的相关命令的介绍。

The IP address of your computer can be set from the command prompt by running the following commands at an administrative level prompt:

netsh interface ip set address name="Local Area Connection" static 123.123.123.123 255.255.255.0 123.123.123.1 1

Local Area Connection is the name of the adapter you want to modify. In single NIC systems it is normally called Local Area Connection.

123.123.123.123 is the IP address you want to set. 

255.255.255.0 is the subnet mask. 

123.123.123.1 is the gateway.

1 is the gateway metric. You can leave this as 1 for almost all cases.  

If you want to enable DHCP you can run:

netsh interface ip set address name="Local Area Connection" dhcp

There are two commands for DNS since administrators typically configure a primary and secondary DNS server. 

For the primary DNS run:

netsh interface ip set dns name="Local Area Connection" static 208.67.222.222

For the secondary run:

netsh interface ip add dns name="Local Area Connection" 208.67.220.220 index=2

If you want to configure the computer to use DNS from DHCP run:

netsh interface ip set dnsservers name="Local Area Connection" source=dhcp

When you are finished with all of your IP and DNS changes run ipconfig -all to review the new settings. 

抱歉!评论已关闭.