JavaScript access to client computer hardware and system Information
Access to client computer hardware and system information through WMI:
Copy Code code as follows:
function Getsysinfo () {
var locator = new ActiveXObject ("WbemScripting.SWbemLocator");
var service = Locator. ConnectServer (".");
CPU Information
var CPU = new Enumerator (service. ExecQuery ("SELECT * from Win32_Processor")). Item ();
var cputype=cpu. Name,hostname=cpu. SystemName;
Memory information
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}
System Information
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}
}
The code implementation mainly includes these parts:
First through the new ActiveXObject ("WbemScripting.SWbemLocator"); Access to the WbemScripting object.
Through the locator. ConnectServer ("."); Connect our local computer (. On behalf of the local computer, of course
You can also access other computers).
Through service. ExecQuery ("SELECT * from Win32_Processor") This SQL-like statement (in fact the system information is stored in a similar database file in the calculation) gets the recordset of the object we need.
With the new enumerator to create an enumerable object, you can iterate through the information below.
Note: The prerequisite for running is to modify browser security settings, "Allow ActiveX that is not marked for safe execution
Script run. "
This mainly takes CPU, memory and system user several information, everybody utilizes WMI's API or uses the Jsedit to obtain
to more information. The following lists the classes for common information:
Win32_Processor//CPU processor
Win32_physicalmemory//Physical memory
Win32_Keyboard//keyboard
Win32_pointingdevice//Point input device, such as mouse
Win32_DiskDrive//Hard Drive
Win32_CDROMDrive/CD drive
Win32_baseboard//Motherboard
Win32_BIOS//BIOS chip
Win32_ParallelPort//And Port
Win32_serialport//serial port
Win32_sounddevice//Multimedia settings
Win32_usbcontroller//USB Controller
Win32_NetworkAdapter//Network adapter
Win32_NetworkAdapterConfiguration//Network adapter settings
Win32_Printer//Printer
Win32_PrinterConfiguration//Printer settings
Win32_PrintJob//Printer Tasks
Win32_tcpipprinterport//Printer Port
Win32_potsmodem//MODEM
Win32_potsmodemtoserialport//MODEM Port
Win32_DesktopMonitor//Monitor
Win32_VideoController//graphics details.
Win32_videosettings//Graphics support display mode.
Win32_TimeZone//Time zone
Win32_SystemDriver//Driver
Win32_DiskPartition//Disk partitions
Win32_LogicalDisk//Logical Disk
Win32_LogicalMemoryConfiguration//Logical memory configuration
Win32_PageFile//System page file information
Win32_PageFileSetting//Page file settings
Win32_bootconfiguration//System boot Configuration
Win32_OperatingSystem//Operating System Information
Win32_startupcommand//System automatic Start Program
Win32_Service//System installation Services
Win32_Group//System Management Group
Win32_groupuser//System group account number
Win32_UserAccount//user account
Win32_Process//System process
Win32_thread//System thread
Win32_Share//Shared
Win32_NetworkClient//Installed network clients
WIN32_NETWORKPROTOCOL//Installed network protocol
For complete information and a detailed list of WMI Win32 classes, please refer to MSDN:
http://msdn2.microsoft.com/en-us/library/aa394084 (vs.85). aspx
Example:
Copy Code code as follows:
function Button1_onclick () {//CPU information
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 information
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 () {//keyboard information
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 () {//motherboard information
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>");
}
In addition, information about the system can be obtained in the following ways:
Copy Code code as follows:
<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><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>
Copy Code code as follows:
In fact, the key is to use two ActiveX:
<object Id=locator Classid=clsid:76a64158-cb41-11d1-8b02-00600806d9b6 viewastext></object>
<object Id=foo classid=clsid:75718c9a-f029-11d1-a1ac-00c04fb6c223></object>
However, these two ActiveX systems are self-contained and do not need to be downloaded or registered.
The next task is to use scripting and ActiveX interaction
<HTML>
<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>
<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;
};