1.Show IP
1.1Cmd Mode
直接在cmd下面輸入
netsh interface ip show address
亦可簡寫爲
看看,指令是不是和Cisco的nos指令很像!非常懷疑是抄襲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 ipnet - 顯示 IP net-to-media 對應
show joins - 顯示加入的多點傳送羣組
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可以簡寫成int,dump更可簡化成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地址是否設置正確.
在網絡上流傳的好多類似的工具都是這個原理.