文為大家詳細介紹下如何使用JavaScript獲取客戶端計算機硬件及系統等信息,下面有個不錯的示例,感興趣的朋友可以參考下
JavaScript 獲取客戶端計算機硬件及系統信息 通過WMI來實現獲取客戶端計算機硬件及系統信息: 代碼如下: function getSysInfo(){ var locator = new ActiveXObject ("WbemScripting.SWbemLocator"); var service = locator.ConnectServer("."); //CPU信息 var cpu = new Enumerator (service.ExecQuery("SELECT * FROM Win32_Processor")).item(); var cpuType=cpu.Name,hostName=cpu.SystemName; //內存信息 var memory = new Enumerator (service.ExecQuery("SELECT * FROM Win32_PhysicalMemory")); for (var mem=[],i=0;!memory.atEnd();memory.moveNext()) mem[i++]={cap:memory.item().Capacity/1024/1024,speed:memory.item().Speed} //系統信息 var system=new Enumerator (service.ExecQuery("SELECT * FROM Win32_ComputerSystem")).item(); var physicMenCap=Math.ceil(system.TotalPhysicalMemory/1024/1024),curUser=system.UserName,cpuCount=system.NumberOfProcessors return {cpuType:cpuType,cpuCount:cpuCount,hostName:hostName,curUser:curUser,memCap:physicMenCap,mem:mem} } 代碼實現主要包括這幾部分: 先通過new ActiveXObject ("WbemScripting.SWbemLocator"); 訪問到WbemScripting對象。 通過locator.ConnectServer(".");連接我們本地電腦(.代表本地電腦,當然 也可以訪問其他計算機)。 通過service.ExecQuery("SELECT * FROM Win32_Processor")這個類似sql的語句(其實系統信息也是存儲在計算中一個類似數據庫的文件中)獲取我們需要的對象的記錄集。 通過new Enumerator來創建一個可枚舉的對象,下面就可以遍歷取信息了。 注意:運行的前提是要修改浏覽器安全設置,“允許對未標記為可安全執行的ActiveX 腳本的運行”。 這裡主要取了CPU、內存及系統用戶幾個信息,大家利用WMI的API或者借助JSEDIT獲取 到更多的信息。下面列出了常用信息的類: Win32_Processor // CPU 處理器 Win32_PhysicalMemory // 物理內存 Win32_Keyboard // 鍵盤 Win32_PointingDevice // 點輸入設備,如鼠標 Win32_DiskDrive // 硬盤驅動器 Win32_CDROMDrive // 光盤驅動器 Win32_BaseBoard // 主板 Win32_BIOS // BIOS 芯片 Win32_ParallelPort // 並口 Win32_SerialPort // 串口 Win32_SoundDevice // 多媒體設置 Win32_USBController // USB 控制器 Win32_NetworkAdapter // 網絡適配器 Win32_NetworkAdapterConfiguration // 網絡適配器設置 Win32_Printer // 打印機 Win32_PrinterConfiguration // 打印機設置 Win32_PrintJob // 打印機任務 Win32_TCPIPPrinterPort // 打印機端口 Win32_POTSModem // MODEM Win32_POTSModemToSerialPort // MODEM 端口 Win32_DesktopMonitor // 顯示器 Win32_VideoController // 顯卡細節。 Win32_VideoSettings // 顯卡支持的顯示模式。 Win32_TimeZone // 時區 Win32_SystemDriver // 驅動程序 Win32_DiskPartition // 磁盤分區 Win32_LogicalDisk // 邏輯磁盤 Win32_LogicalMemoryConfiguration // 邏輯內存配置 Win32_PageFile // 系統頁文件信息 Win32_PageFileSetting // 頁文件設置 Win32_BootConfiguration // 系統啟動配置 Win32_OperatingSystem // 操作系統信息 Win32_StartupCommand // 系統自動啟動程序 Win32_Service // 系統安裝的服務 Win32_Group // 系統管理組 Win32_GroupUser // 系統組帳號 Win32_UserAccount // 用戶帳號 Win32_Process // 系統進程 Win32_Thread // 系統線程 Win32_Share // 共享 Win32_NetworkClient // 已安裝的網絡客戶端 Win32_NetworkProtocol // 已安裝的網絡協議 WMI Win32類的完整信息及詳細列表請參考MSDN: http://msdn2.microsoft.com/en-us/library/aa394084(VS.85).aspx 示例: 代碼如下: function button1_onclick() {//cpu 信息 var locator = new ActiveXObject ("WbemScripting.SWbemLocator"); var service = locator.ConnectServer("."); var properties = service.ExecQuery("SELECT * FROM Win32_Processor"); var e = new Enumerator (properties); document.write("<table border=1>"); for (;!e.atEnd();e.moveNext ()) { var p = e.item (); document.write("<tr>"); document.write("<td>" + p.Caption + "</td>"); document.write("<td>" + p.DeviceID + "</td>"); document.write("<td>" + p.Name + "</td>"); document.write("<td>" + p.CpuStatus + "</td>"); document.write("<td>" + p.Availability + "</td>"); document.write("<td>" + p.Level + "</td>"); document.write("<td>" + p.ProcessorID + "</td>"); document.write("<td>" + p.SystemName + "</td>"); document.write("<td>" + p.ProcessorType + "</td>"); document.write("</tr>"); } document.write("</table>"); } function Button2_onclick() {//CD-ROM 信息 var locator = new ActiveXObject ("WbemScripting.SWbemLocator"); var service = locator.ConnectServer("."); var properties = service.ExecQuery("SELECT * FROM Win32_CDROMDrive"); var e = new Enumerator (properties); document.write("<table border=1>"); for (;!e.atEnd();e.moveNext ()) { var p = e.item (); document.write("<tr>"); document.write("<td>" + p.Caption + "</td>"); document.write("<td>" + p.Description + "</td>"); document.write("<td>" + p.Drive + "</td>"); document.write("<td>" + p.Status + "</td>"); document.write("<td>" + p.MediaLoaded + "</td>"); document.write("</tr>"); } document.write("</table>"); } function Button3_onclick() {//鍵盤信息 var locator = new ActiveXObject ("WbemScripting.SWbemLocator"); var service = locator.ConnectServer("."); var properties = service.ExecQuery("SELECT * FROM Win32_Keyboard"); var e = new Enumerator (properties); document.write("<table border=1>"); for (;!e.atEnd();e.moveNext ()) { var p = e.item (); document.write("<tr>"); document.write("<td>" + p.Description + "</td>"); document.write("<td>" + p.Name + "</td>"); document.write("<td>" + p.Status + "</td>"); document.write("</tr>"); } document.write("</table>"); } function Button4_onclick() {//主板信息 var locator = new ActiveXObject ("WbemScripting.SWbemLocator"); var service = locator.ConnectServer("."); var properties = service.ExecQuery("SELECT * FROM Win32_BaseBoard"); var e = new Enumerator (properties); document.write("<table border=1>"); for (;!e.atEnd();e.moveNext ()) { var p = e.item (); document.write("<tr>"); document.write("<td>" + p.HostingBoard + "</td>"); document.write("<td>" + p.Manufacturer + "</td>"); document.write("<td>" + p.PoweredOn + "</td>"); document.write("<td>" + p.Product + "</td>"); document.write("<td>" + p.SerialNumber + "</td>"); document.write("<td>" + p.Version + "</td>"); document.write("</tr>"); } document.write("</table>"); } 另外,通過以下方式也可以獲得系統的相關信息: 代碼如下: <HTML><HEAD><TITLE>WMI Scripting HTML</TITLE> <META http-equiv=Content-Type content="text/html; charset=gb2312"> <SCRIPT language=JScript event="OnCompleted(hResult,pErrorObject, pAsyncContext)" for=foo> document.forms[0].txtMACAddr.value=unescape(MACAddr); document.forms[0].txtIPAddr.value=unescape(IPAddr); document.forms[0].txtDNSName.value=unescape(sDNSName); //document.formbar.submit(); </SCRIPT> <SCRIPT language=JScript event=OnObjectReady(objObject,objAsyncContext) for=foo> if(objObject.IPEnabled != null && objObject.IPEnabled != "undefined" && objObject.IPEnabled == true) { if(objObject.MACAddress != null && objObject.MACAddress != "undefined") MACAddr = objObject.MACAddress; if(objObject.IPEnabled && objObject.IPAddress(0) != null && objObject.IPAddress(0) != "undefined") IPAddr = objObject.IPAddress(0); if(objObject.DNSHostName != null && objObject.DNSHostName != "undefined") sDNSName = objObject.DNSHostName; } </SCRIPT> <META content="MSHTML 6.00.2800.1106" name=GENERATOR></HEAD> <BODY> <OBJECT id=locator classid=CLSID:76A64158-CB41-11D1-8B02-00600806D9B6 VIEWASTEXT></OBJECT> <OBJECT id=foo classid=CLSID:75718C9A-F029-11d1-A1AC-00C04FB6C223></OBJECT> <SCRIPT language=JScript> var service = locator.ConnectServer(); var MACAddr ; var IPAddr ; var DomainAddr; var sDNSName; service.Security_.ImpersonationLevel=3; service.InstancesOfAsync(foo, 'Win32_NetworkAdapterConfiguration'); </SCRIPT> <FORM id="formfoo" name="formbar" action="index.do" method="post"><INPUT value="00-11-11-B4-52-EF" name="txtMACAddr"> <INPUT value="210.42.38.50" name="txtIPAddr"> <INPUT value="zhupan" name="txtDNSName"> </FORM></BODY></HTML> 復制代碼 代碼如下: 其實最關鍵的還是用到兩個ActiveX: <OBJECT id=locator classid=CLSID:76A64158-CB41-11D1-8B02-00600806D9B6 VIEWASTEXT></OBJECT> <OBJECT id=foo classid=CLSID:75718C9A-F029-11d1-A1AC-00C04FB6C223></OBJECT> 不過這兩個ActiveX都是系統自帶,不用去下載或注冊。 接下來的工作就是用腳本和ActiveX交互 <HTML> <head> <script language="javascript" type="text/javascript"> function getCode(){ var locator = new ActiveXObject("WbemScripting.SWbemLocator"); var service = locator.ConnectServer("."); var properties = service.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled=TRUE"); var e = new Enumerator(properties); document.write("<table border=1>"); for (; !e.atEnd(); e.moveNext()) { var p = e.item(); //if(p.IPAddress(0)!=null && p.IPAddress(0)!="undefined" && p.IPAddress(0)!="0.0.0.0" && p.IPAddress(0)!="127.0.0.1"){ document.write("<tr>"); document.write("<td>" + p.ServiceName + "</td>"); document.write("<td>" + p.MACAddress + "</td>"); document.write("<td>" + p.IPAddress(0) + "</td>"); document.write("</tr>"); //break; //} } document.write("</table>"); } </script> </head> <BODY> <button id="TEST" value="test" onclick="getCode()">TEST</button> </BODY> </HTML> class Win32_NetworkAdapterConfiguration : CIM_Setting { boolean ArpAlwaysSourceRoute; boolean ArpUseEtherSNAP; string Caption; string DatabasePath; boolean DeadGWDetectEnabled; string DefaultIPGateway[]; uint8 DefaultTOS; uint8 DefaultTTL; string Description; boolean DHCPEnabled; datetime DHCPLeaseExpires; datetime DHCPLeaseObtained; string DHCPServer; string DNSDomain; string DNSDomainSuffixSearchOrder[]; boolean DNSEnabledForWINSResolution; string DNSHostName; string DNSServerSearchOrder[]; boolean DomainDNSRegistrationEnabled; uint32 ForwardBufferMemory; boolean FullDNSRegistrationEnabled; uint16 GatewayCostMetric[]; uint8 IGMPLevel; uint32 Index; uint32 InterfaceIndex; string IPAddress[]; uint32 IPConnectionMetric; boolean IPEnabled; boolean IPFilterSecurityEnabled; boolean IPPortSecurityEnabled; string IPSecPermitIPProtocols[]; string IPSecPermitTCPPorts[]; string IPSecPermitUDPPorts[]; string IPSubnet[]; boolean IPUseZeroBroadcast; string IPXAddress; boolean IPXEnabled; 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; };