javascript利用wmi擷取用戶端網卡資訊

來源:互聯網
上載者:User

本文系轉載

近因工作所需,需要擷取用戶端網卡資訊,試過多種方法,如使用VC寫Activex控制項,但此法需對控制項簽名,否則如果IE瀏覽器安全層級設定較高控制項可能經常被提示

而且很有可能無法運行;另又發現了使用VBscript也可以擷取用戶端網卡資訊,稍候附上代碼,但使用此法IE可能會提示無法'建立對象',暫無發現良好解決辦法,最

終找到了一種簡便且有效方法,即使用使用JavascriptSWbemLocator對象操作WMI 的 Win32_NetworkAdapterConfiguration來擷取所需資訊.

什麼是WMI

WMI是Windows 2K/XP管理系統的核心;對於其他的Win32作業系統,WMI是一個有用的外掛程式。WMI以CIMOM為基礎,CIMOM即公用資訊模型對象管理器(Common

Information Model Object Manager),是一個描述作業系統構成單元的對象資料庫,為MMC和指令碼程式提供了一個訪問作業系統構成單元的公用介面。有了WMI

,工具軟體和指令碼程式訪問作業系統的不同部分時不需要使用不同的API;相反,作業系統的不同部分都可以插入WMI,工具軟體和WMI可以方便地讀寫WMI,所以不

只Javascript可以串連WMI,VBscript,C#等語言亦可。


什麼是Win32_NetworkAdapterConfiguration


Win32_NetworkAdapterConfiguration描述了一個網路介面卡的屬性及行為,此類包括支援TCP/IP管理及與相對網路介面卡獨立的IPX協議的額外屬性及方法
下面簡單翻譯一下來自MSDN的關於Win32_NetworkAdapterConfiguration的主要屬性及方法,詳細資料請參考
http://msdn.microsoft.com/en

-

us/library/aa394217(VS.85).aspx
class Win32_NetworkAdapterConfiguration : CIM_Setting
{
boolean ArpAlwaysSourceRoute;
boolean ArpUseEtherSNAP;
string Caption;//CIM_Setting對象的描述,該屬性從CIM_Setting繼承
string DatabasePath;
boolean DeadGWDetectEnabled;
string DefaultIPGateway[];//預設閘道,注意該類型為數組
uint8 DefaultTOS;
uint8 DefaultTTL;//IP包頭的預設TTL值
string Description;//CIM_Setting對象的描述,該屬性從CIM_Setting繼承
boolean DHCPEnabled;//是否允許DHCP,如果允許在建立網路連接的時候由DHCP伺服器來分配地址
datetime DHCPLeaseExpires;//DHCP租用有效期間
datetime DHCPLeaseObtained;//DHCP租用擷取時間
string DHCPServer;//DHCP伺服器
string DNSDomain;//DHCP伺服器域
string DNSDomainSuffixSearchOrder[];
boolean DNSEnabledForWINSResolution;
string DNSHostName;
string DNSServerSearchOrder[];
boolean DomainDNSRegistrationEnabled;
uint32 ForwardBufferMemory;
boolean FullDNSRegistrationEnabled;
uint16 GatewayCostMetric[];
uint8 IGMPLevel;
uint32 Index;
uint32 InterfaceIndex;
string IPAddress[];//IP地址,該類型為數組
uint32 IPConnectionMetric;
boolean IPEnabled;//TCP/IP在該網路介面卡上是否被啟用
boolean IPFilterSecurityEnabled;
boolean IPPortSecurityEnabled;
string IPSecPermitIPProtocols[];
string IPSecPermitTCPPorts[];
string IPSecPermitUDPPorts[];
string IPSubnet[];//子網路遮罩,如"255.255.0.0"
boolean IPUseZeroBroadcast;
string IPXAddress;//IPX地址
boolean IPXEnabled;//IPX是否允許
uint32 IPXFrameType[];
uint32 IPXMediaType;
string IPXNetworkNumber[];
string IPXVirtualNetNumber;
uint32 KeepAliveInterval;
uint32 KeepAliveTime;
string MACAddress;
uint32 MTU;//傳輸單元最大值
uint32 NumForwardPackets;
boolean PMTUBHDetectEnabled;
boolean PMTUDiscoveryEnabled;
string ServiceName;
string SettingID;
uint32 TcpipNetbiosOptions;
uint32 TcpMaxConnectRetransmissions;
uint32 TcpMaxDataRetransmissions;
uint32 TcpNumConnections;
boolean TcpUseRFC1122UrgentPointer;
uint16 TcpWindowSize;
boolean WINSEnableLMHostsLookup;
string WINSHostLookupFile;
string WINSPrimaryServer;
string WINSScopeID;
string WINSSecondaryServer;
};
主要方法

DisableIPSec
在該被激的TCP/IP網路介面卡是禁用IPSec

EnableDHCP
啟用DHCP

EnableDNS
啟用DNS

EnableIPFilterSec
啟用IPFilterSec

EnableIPSec
啟用IPSec.

EnableStatic
目標網路介面卡上應用靜態TCP/IP定址

EnableWINS
啟用WINS,但獨立於網路介面卡.

ReleaseDHCPLease
釋放綁定在已激DHCP的網路介面卡上的IP地址

ReleaseDHCPLeaseAll
釋放所有綁定在已激DHCP的網路介面卡上的IP地址.

RenewDHCPLease
Renews 綁定在已激DHCP的網路介面卡上的IP地址

RenewDHCPLeaseAll
Renews所有綁定在已激DHCP的網路介面卡上的IP地址

SetArpAlwaysSourceRoute

SetArpUseEtherSNAP
使用 802.3 SNAP 編碼允許Ethernet packets

SetDatabasePath
設定標準INTERNET資料檔案的路徑(HOSTS, LMHOSTS, NETWORKS, and PROTOCOLS).

SetDeadGWDetect
啟用死網關探測.

SetDefaultTOS
(過時)

SetDefaultTTL
設定預設TTL值

SetDNSDomain
設定DNS網域名稱.

SetDNSServerSearchOrder
設定DNS伺服器搜尋順序

SetDNSSuffixSearchOrder

SetDynamicDNSRegistration

SetForwardBufferMemory

SetGateways
設定網關

SetIGMPLevel
設定IGMP層級

SetIPConnectionMetric
設定routing metric .

SetIPXFrameTypeNetworkPairs

SetKeepAliveTime

設定通過發送一個Keep Alive 包來確定TCP嘗試校正一個空閑串連是否可用的時間

SetMTU
設定傳輸單元最大值MTU不再被支援

SetNumForwardPackets

設定路由包資料包隊列分配的IP包頭數
SetPMTUBHDetect
允許Black Hole routers探測.

SetPMTUDiscovery
允許MTU discovery.

SetTcpipNetbios

設定TCP/IP上的NETBIOS操作
Windows 2000 and Windows NT 4.0: 此屬性不可用

SetTcpMaxConnectRetransmissions
設定TCP在重新發送串連請求的嘗試次數

SetTcpMaxDataRetransmissions
Sets the number of times TCP will retransmit an individual data segment before aborting the connection.

SetTcpNumConnections
TCP最大並發串連數.

SetTcpUseRFC1122UrgentPointer
指定是否TCP使用RFC1122規範,或BSD系統使用的模式

SetTcpWindowSize
設定TCP視窗大小.

SetWINSServer
設定WINS伺服器

作業碼

Javascript代碼

<script language="javascript">
<!--

function GetAdapterInfo() {

var locator = new ActiveXObject ("WbemScripting.SWbemLocator");
var service = locator.ConnectServer("."); //串連本機伺服器
var properties = service.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration");
//查詢使用SQL標準
var e = new Enumerator (properties);

for (;!e.atEnd();e.moveNext ())
{
var p = e.item ();

document.write("Caption:" + p.Caption + " "); //網卡描述,也可以使用Description
document.write("IP:" + p.IPAddress(0) + " ");//IP地址為數群組類型,子網俺碼及預設閘道亦同
document.write("Net MASK:" + p.IPSubnet(0) + " ");
document.write("Default gateway:" + p.DefaultIPGateway(0) + " ");
document.write("MAC:" + p.MACAddress + " "); //網卡物理地址
document.write("<hr>");
}

}

GetAdapterInfo();

//-->
</script>
將以上代碼放在HTML代碼中可以看到JS輸出的詳細資料,如顯示更詳細資料請參照以上的列出的屬性
如果機器安裝了多塊網卡,只想得到某一塊網卡,可使用查詢過濾的方法得到如安裝了intel和Realtek的網卡,擷取Realtek的網卡只需要使用SQL查詢即可,如下
var properties = service.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration where description like '%Realtek%'");

Vbscript代碼

strComputer = "."
Set objWMIService = GetObject( _
    "winmgmts://" & strComputer & "/root/cimv2")
Set IPConfigSet = objWMIService.ExecQuery _
    ("Select IPAddress from Win32_NetworkAdapterConfiguration" _
        & " where Description like '%Realtek%'")

For Each IPConfig in IPConfigSet
    If Not IsNull(IPConfig.IPAddress) Then
        For i=LBound(IPConfig.IPAddress) _
        to UBound(IPConfig.IPAddress)
            WScript.Echo IPConfig.IPAddress(i)
        Next
    End If
Next
將以上文本儲存為test.vbs,雙擊即可

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.