JavaScript获取客户端计算机硬件及系统等信息的方法

本文为大家详细介绍下如何使用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("");
for (;!e.atEnd();e.moveNext ())
{
var p = e.item ();
document.write("");
document.write("");
document.write("");
document.write("");
document.write("");
document.write("");
document.write("");
document.write("");
document.write("");
document.write("");
document.write("");
}
document.write("
" + p.Caption + "" + p.DeviceID + "" + p.Name + "" + p.CpuStatus + "" + p.Availability + "" + p.Level + "" + p.ProcessorID + "" + p.SystemName + "" + p.ProcessorType + "
");
}

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("");
for (;!e.atEnd();e.moveNext ())
{
var p = e.item ();
document.write("");
document.write("");
document.write("");
document.write("");
document.write("");
document.write("");
document.write("");
}
document.write("
" + p.Caption + "" + p.Description + "" + p.Drive + "" + p.Status + "" + p.MediaLoaded + "
");
}

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("");
for (;!e.atEnd();e.moveNext ())
{
var p = e.item ();
document.write("");
document.write("");
document.write("");
document.write("");
document.write("");
}
document.write("
" + p.Description + "" + p.Name + "" + p.Status + "
");
}

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("");
for (;!e.atEnd();e.moveNext ())
{
var p = e.item ();
document.write("");
document.write("");
document.write("");
document.write("");
document.write("");
document.write("");
document.write("");
document.write("");
}
document.write("
" + p.HostingBoard + "" + p.Manufacturer + "" + p.PoweredOn + "" + p.Product + "" + p.SerialNumber + "" + p.Version + "
");
}


另外,通过以下方式也可以获得系统的相关信息:
复制代码 代码如下:

WMI Scripting HTML













复制代码 代码如下:

其实最关键的还是用到两个ActiveX:


不过这两个ActiveX都是系统自带,不用去下载或注册。
接下来的工作就是用脚本和ActiveX交互










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;
};

以上就是JavaScript获取客户端计算机硬件及系统等信息的方法的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » JavaScript 教程