使用bat一鍵修改IP地址


由於最近需要經常更換IP,所以解決了一下每次都要繁瑣修改的問題

新建文本文檔並加入命令重命名為bat可執行程序,方便修改IP地址

代碼如下

 1 @echo off
 2 ::修改ip
 3 ::李旭 2020年9月6日 22點02分
 4 
 5 
 6 >nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
 7 if '%errorlevel%' NEQ '0' (
 8 goto UACPrompt
 9 ) else ( goto gotAdmin )
10 :UACPrompt
11 echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
12 echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%temp%\getadmin.vbs"
13 "%temp%\getadmin.vbs"
14 exit /B
15 :gotAdmin
16 if exist "%temp%\getadmin.vbs" ( del "%temp%\getadmin.vbs" )
17 
18 
19 
20 echo 修改ip地址:1自動獲取ip, 2宿舍ip地址, 3畢設IP, 4以太網3IP
21 set sel=
22 set/p sel=請選擇修改方式:
23 if "%sel%"=="1" goto auto
24 if "%sel%"=="2" goto home
25 if "%sel%"=="3" goto company
26 if "%sel%"=="4" goto home2
27 echo 您沒有選擇修改方式。
28 goto end
29 
30 
31 :auto
32 ::netsh interface ip set address name ="以太網" source = dhcp
33 ::netsh interface ip set dns name ="以太網" source = dhcp
34 netsh interface ip set address name="以太網" source=dhcp
35 netsh interface ip delete dns "以太網" all
36 ipconfig /flushdns
37 echo 更改IP地址完成!
38 goto end
39 
40 
41 :home
42 echo 正在更改IP地址,請稍等......
43 netsh interface ip set address name="以太網" source=static addr=10.91.5.57 mask=255.255.255.0 gateway=10.91.5.254 gwmetric=1
44 netsh interface ip set dns name="以太網" source=static addr=202.206.32.1
45 netsh interface ip add dns name="以太網" addr=202.206.32.1 index=2 
46 ipconfig /flushdns
47 echo 更改IP地址完成!
48 goto end
49 
50 :home2
51 echo 正在更改IP地址,請稍等......
52 netsh interface ip set address name="以太網3" source=static addr=10.91.5.57 mask=255.255.255.0 gateway=10.91.5.254 gwmetric=1
53 netsh interface ip set dns name="以太網3" source=static addr=202.206.32.1
54 netsh interface ip add dns name="以太網3" addr=202.206.32.1 index=2 
55 ipconfig /flushdns
56 echo 更改IP地址完成!
57 goto end
58 
59 :company
60 echo 正在更改IP地址,請稍等......
61 netsh interface ip set address name="以太網" source=static addr=192.168.63.85 mask=255.255.255.0 gateway=192.168.63.254 gwmetric=1
62 netsh interface ip set dns name="以太網" source=static addr=202.206.32.1
63 netsh interface ip add dns name="以太網" addr=202.206.32.1 index=2 
64 ipconfig /flushdns
65 echo 更改IP地址完成!
66 goto end
67 
68 
69 :end
70 pause

詳細解釋如下:

1,設置ip

參數說明:

1.name:網絡連接名稱,一般為“本地連接”。你可以在“控制面板”->“網絡連接”中看到。

2.source:獲取IP的途徑。動態獲取,則為dhcp,手動設置,則為static。

3.addr:要設置的IP地址。

4.mask:子網掩碼。

5.gateway:網關地址。

6.gwmetric:網關躍點數,可以設置為整型數值,也可以設置為“自動”:auto。

2,設置dns

參數說明:

1.name:網絡連接名稱,一般為“本地連接”。你可以在“控制面板”->“網絡連接”中看到。

2.source:獲取IP的途徑。動態獲取,則為dhcp,手動設置,則為static。

3.addr:要設置的IP地址。

4.register:

5.none: 禁用動態 DNS 注冊。

6.primary: 只在主 DNS 后綴下注冊。

7.both: 在主 DNS 后綴下注冊,也在特定連接后綴下注冊。

8.index:設置的DNS的順序號。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM