Configure IP Address and DNS from Command Line

之前在使用電腦的時候經常遇到需要手動修改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. 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章