//系统信息获取
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获取到更多的信息。
常用信息的类:
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:
不过这两个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
{
booleanArpAlwaysSourceRoute;
booleanArpUseEtherSNAP;
stringCaption;
stringDatabasePath;
booleanDeadGWDetectEnabled;
stringDefaultIPGateway[];
uint8 DefaultTOS;
uint8 DefaultTTL;
stringDescription;
booleanDHCPEnabled;
datetime DHCPLeaseExpires;
datetime DHCPLeaseObtained;
stringDHCPServer;
stringDNSDomain;
stringDNSDomainSuffixSearchOrder[];
booleanDNSEnabledForWINSResolution;
stringDNSHostName;
stringDNSServerSearchOrder[];
booleanDomainDNSRegistrationEnabled;
uint32ForwardBufferMemory;
booleanFullDNSRegistrationEnabled;
uint16GatewayCostMetric[];
uint8 IGMPLevel;
uint32Index;
uint32InterfaceIndex;
stringIPAddress[];
uint32IPConnectionMetric;
booleanIPEnabled;
booleanIPFilterSecurityEnabled;
booleanIPPortSecurityEnabled;
stringIPSecPermitIPProtocols[];
stringIPSecPermitTCPPorts[];
stringIPSecPermitUDPPorts[];
stringIPSubnet[];
booleanIPUseZeroBroadcast;
stringIPXAddress;
booleanIPXEnabled;
uint32IPXFrameType[];
uint32IPXMediaType;
stringIPXNetworkNumber[];
stringIPXVirtualNetNumber;
uint32KeepAliveInterval;
uint32KeepAliveTime;
stringMACAddress;
uint32MTU;
uint32NumForwardPackets;
booleanPMTUBHDetectEnabled;
booleanPMTUDiscoveryEnabled;
stringServiceName;
stringSettingID;
uint32TcpipNetbiosOptions;
uint32TcpMaxConnectRetransmissions;
uint32TcpMaxDataRetransmissions;
uint32TcpNumConnections;
booleanTcpUseRFC1122UrgentPointer;
uint16TcpWindowSize;
booleanWINSEnableLMHostsLookup;
stringWINSHostLookupFile;
stringWINSPrimaryServer;
stringWINSScopeID;
stringWINSSecondaryServer;
};