Wednesday, June 20, 2012

MAC Address Using WMI on Internet Explorer

In this little article, i will help you in finding the MAC address from javascript with the help of WMI Library. The script runs only on IE with the following limitations
  • Works on Internet Explorer only
  • Internet Explorer security settings should allow creating ActiveX Objects
  • WMI scripting library is installed on the client machine
Setting IE Security Level
First of all you will need to change the security settings of IE, allowing the following two options
  • Initialize and script ActiveX controls not marked as safe for script -> Set it to enable or prompt
  • Run ActiveX controls and plugins -> Set it to enable or prompt
To change these two options, go to Tools -> Internet Options -> Security -> Custom Level 
 






Installing WMI scripting library
The next step is to install WMI Library, you can download it for free from the Microsoft website. WMI Library can be downloaded from
http://www.microsoft.com/downloads/details.aspx?FamilyID=6430f853-1120-48db-8cc5-f2abdc3ed314&displaylang=en

What is WMI scripting library?
The WMI scripting library provides the set of automation objects through which scripting languages, such as VBScript, JScript, and ActiveState ActivePerl access the WMI infrastructure. The WMI scripting library is implemented in a single automation component named wbemdisp.dll that physically resides in the systemroot\System32\Wbem directory. (description from microsoft.com)

SWbemLocator
At the top of the WMI scripting library object model is the SWbemLocator object. SWbemLocator is used to establish an authenticated connection to a WMI namespace, much as the VBScript GetObject function and the WMI moniker "winmgmts:" are used to establish an authenticated connection to WMI. However, SWbemLocator is designed to address two specific scripting scenarios that cannot be performed using GetObject and the WMI moniker.(description from microsoft.com)

The whole script is given below, just copy this script and execute in IE only.

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Getting MAC Address From Javascript(IE Only)</title>
 
<script language="javascript">
function showMacAddress(){
 
	var obj = new ActiveXObject("WbemScripting.SWbemLocator");
	var s = obj.ConnectServer(".");
	var properties = s.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration");
	var e = new Enumerator (properties);

 
	var output;
	output='<table border="0" cellPadding="5px" cellSpacing="1px" bgColor="#CCCCCC">';
	output=output + '<tr bgColor="#EAEAEA"><td>Caption</td><td>MACAddress</td></tr>';
	while(!e.atEnd())

	{
		e.moveNext();
		var p = e.item ();
		if(!p) continue;
		output=output + '<tr bgColor="#FFFFFF">';
		output=output + '<td>' + p.Caption; + '</td>';
		output=output + '<td>' + p.MACAddress + '</td>';
		output=output + '</tr>';
	}

	output=output + '</table>';
	document.getElementById("box").innerHTML=output;
}
</script>
 
</head>
<body>
	<input type="button" value="Show MAC Address" onclick="showMacAddress()" />

	<div id="box">
	</div>
</body>
</html>
 

No comments:

Post a Comment