Delphi 調用netsh命令修改IP地址

先介紹一下Netsh命令的使用方法:
 
在這裏跟大家介紹幾個簡單的指令
1.Show IP
1.1Cmd Mode
直接在cmd下面輸入
netsh interface ip show address
亦可簡寫爲
netsh int ip sh ad
看看,指令是不是和Cisconos指令很像!非常懷疑是抄襲Cisco的。

1.2Netsh Mode
您也可以進入netsh的命令模式下
netsh                          //
進入到 netsh mode
netsh>int                    //
進入到 interface 子選項。
interface>ip                //
進入到 ip 子選項。
interface ip>show
show address -
顯示 IP 位址。
show config -
顯示 IP 位址及其他資料。
show dns -
顯示 DNS 服務器位址。
show icmp -
顯示 ICMP 統計
show interface - 顯示 IP 介面統計
show ipaddress - 顯示 IP 位址
show ipnet -
顯示 IP net-to-media 對應
show ipstats - 顯示 IP 統計
show joins -
顯示加入的多點傳送羣組
show offload - 顯示 offload 內容
show tcpconn -
顯示 TCP 連線

show tcpstats - 顯示 TCP 統計
show udpconn -
顯示 UDP 連線
show udpstats -
顯示 UDP 統計
show wins -
顯示 WINS 服務器位址。
2.Set IP
下列是所有可用的指令。
這個內容中的指令:
set address -
在指定的介面設定 IP 位址或預設網關。
set dns -
設定 DNS 服務器模式及位址。
set wins -
設定 WINS 服務器模式及位址。

2.1.
設定IP位址
2.1.1.DHCP
設定
若您希望由DHCP取得IP位址可輸入
interface ip>set ad "
區域連接" DHCP
或簡寫成
interface ip>set ad "
區域連接" D

2.1.2.
靜態IP設定
2.1.2.1.
設定IP位址與子網關
netsh -
進入到 netsh mode
netsh>int -
進入到 interface 子選項。
interface>ip -
進入到 ip 子選項。
interface ip>set address name = "
區域連接" source = static addr = 10.2.2.100 mask = 255.255.255.0
可簡寫成
interface ip>set ad "
區域連接" s 10.2.2.100 255.255.255.0

2.1.2.2.
設定IP路由
interface ip>set address name = "
區域連接" gateway = 10.2.2.254 gwmetric = 1
可簡寫成
interface ip>set ad "
區域連接" ga=10.2.2.254 gw = 1

2.1.2.3
同時設定IP位址和路由
interface ip>set address name = "
區域連接" source = static addr = 10.2.2.100 mask = 255.255.255.0 gateway = 10.2.2.254 gwmetric = 1
可簡寫成
interface ip>set ad "
區域連接" s 10.2.2.100 255.255.255.0 10.2.2.254 1

設定完後,記得用sh ad去看一下設定的對不對。

2.3
設定DNS來源
若是由DHCP取得,請輸入
interface ip>set dns "
區域連接" source=dhcp
若是使用靜態設定,請輸入
interface ip>set dns name = "
區域連接" source = static addr = 10.2.5.2
新增第二組DNS,請輸入
interface ip>add dns name = "
區域連接" addr = 10.2.5.3

2.4
設定WINS來源
若是由DHCP取得,請輸入
interface ip>set wins "
區域連接" source=dhcp
若是使用靜態設定,請輸入
interface ip>set wins name = "
區域連接" source = static addr = 10.2.5.10
新增第二組WINS,請輸入
interface ip>add wins name = "
區域連接" addr = 10.2.5.17


3.
將網路狀態設定導出/導入
3.1
導出
netsh -c interface dump >c:/netset.txt
當然,interface可以簡寫成intdump更可簡化成d,所以就變成了
netsh -c int d >c:/netset.txt
3.2
導入
netsh -f c:/netset.txt
既可 

在Delphi中調用過程:
//設置成自動獲取IP:
        NetShTxt := 'netsh interface ip set address ' + '"' + NetWork + '"' + ' source=dhcp';
        winexec(pchar(NetShTxt), sw_hide);
//以下代碼設置IP地址和網關:
        NetShTxt := 'netsh interface ip set address name=' + '"' + NetWork + '"' + ' source=static addr=' +   IPAddress.IPString + ' mask=' + submask.IPString;
        winexec(pchar(NetShTxt), sw_hide);
       //網關
        NetshTxt := 'netsh interface ip set address name=' + '"' + NetWork + '"' + ' gateway=' + DefaultGateway.IPString + '  gwmetric=0';
        winexec(pchar(NetShTxt), sw_hide);
其中使用了IPEdit,這個組件,經過修改,已經基本能用於IP地址的輸入和獲取.本來從網上下載的以爲能用,可實際用的過程中會發現不少問題.
設置DNS:
            NetshTxt := 'netsh interface ip  set dns name=' + '"' + NetWork + '"' + ' source=static addr=' + FirstDns.IPString + ' register=PRIMARY';
            winexec(pchar(NetShTxt), SW_Hide);
           //netsh interface ip add dns name="本地連接" addr=你的DNS的IP地址 index=2
            NetshTxt := 'netsh interface ip  add dns name=' + '"' + NetWork + '"' + ' addr=' + SecondDNS.IPString + ' index=2';
注意以上第二個DNS,添加用的命令:
 //netsh interface ip add dns name="本地連接" addr=你的DNS的IP地址 index=2
第三個DNS:
 //netsh interface ip add dns name="本地連接" addr=你的DNS的IP地址 index=3

後期調用winExec執行,感覺很不錯,注意生成後使用GetAdaptersInfo函數進行檢查一下IP地址是否設置正確.
在網絡上流傳的好多類似的工具都是這個原理.


發佈了28 篇原創文章 · 獲贊 0 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章