WSH實用講座:第一講取得機器的網路屬性配置
作者:Eve Cole
更新時間:2009-05-30 19:53:51
原文請到WSH(WHITE的小家)(http://wwwasp.yeah.net )
其實就是讀註冊表,不過如果能獲得機器的IP配置等信息,以後配置IIS時就簡單了。下面的腳本讀出機器的所有可用IP位址,子網路掩碼,卻省網關等資訊:
代碼:
-------------------------------------------------- ------------------------------
Option Explicit Dim WSHShell Dim sNic, sMan Dim Gateway Dim IPAddress Dim SubnetMask Dim i Dim
sTcpipRegKey Dim bIsDHCP Set WSHShell = CreateObject("WScript.Shell") sNic = WSHShell.RegRead
("HKLMSOFTWAREMicrosoftWindows NTCurrentVersionNetworkCards1ServiceName") If sTcpipRegKey
<> "Microsoft" And Err.Number = 0 Then sTcpipRegKey = "HKLMSYSTEMCurrentControlSetServices" & sNic
& "ParametersTcpip" bIsDHCP = WSHShell.RegRead(sTcpipRegKey & "EnableDHCP") If bIsDHCP Then
Gateway = WSHShell.RegRead(sTcpipRegKey & "DhcpDefaultGateway") IPAddress = WSHShell.RegRead
(sTcpipRegKey & "DhcpIPAddress") SubnetMask = WSHShell.RegRead(sTcpipRegKey & "DhcpSubnetMask")
MsgBox ("DefaultGateway: " & Gateway(0) & Chr(10) & Chr(13) & "IPAddress: " & IPAddress & Chr(10) &
Chr(13) & "SubnetMask: " & SubnetMask) Else Gateway = WSHShell.RegRead(sTcpipRegKey
& "DefaultGateway") IPAddress = WSHShell.RegRead(sTcpipRegKey & "IPAddress") SubnetMask =
WSHShell.RegRead(sTcpipRegKey & "SubnetMask") For i=0 至 Ubound(IPAddress)-1 MsgBox
("DefaultGateway: " & Gateway(0) & Chr(10) & Chr(13) & "IPAddress: " & IPAddress(i) & Chr(10) & Chr(13)
& "SubnetMask: " & SubnetMask(i)) Next End If End If
-------------------------------------------------- ------------------------------
說明:機器的網路設定保存在註冊表裡,網路卡項目下面,所以首先必須知道網路卡的名字。然後取註冊表數據,IP位址和子網路遮罩都是數組形式(其實註冊表裡保存的是二進位數據,VBSCRIPT幫我們轉換了)。在WSH裡讀註冊表非常的簡單,具體請看上面的程式。