JavaScript - How to get client MAC address - Asked By vipul jain on 17-Sep-08 03:54 AM

I have developped a website, containing a poll control using ASP.
When a user votes, a cookie is being written to his machine, so he cannot vote again (unless he deletes the cookie), so the method I am using is not reliable, because any user can delete the cookie and vote again.
Then I tried to retrieve the client mac address, but this is not possible (as I know), unless I use an activeX, but this contains a little issue, the activeX will not run unless the site is in the trusted sites, and this method works only on windows OS since it uses the WMI (since it's a Microsoft component).
....
Here is my question:
1- is there any way to get the client mac address, if yes, what is it?
If no:
2- Is there any unique variable related to the client machine (other than the mac address and IP address), can be retrieved?

How to get client MAC address - Kalit Sikka replied to vipul jain on 17-Sep-08 03:57 AM

ManagementScope theScope = new ManagementScope("\\\\computerName\\root\\cimv2");

StringBuilder theQueryBuilder = new StringBuilder();
theQueryBuilder.Append(
"SELECT MACAddress FROM Win32_NetworkAdapter");
ObjectQuery theQuery = new ObjectQuery(theQueryBuilder.ToString());
ManagementObjectSearcher theSearcher = new ManagementObjectSearcher(theScope, theQuery);
ManagementObjectCollection theCollectionOfResults = theSearcher.Get();

foreach (ManagementObject theCurrentObject in theCollectionOfResults)
{

string macAdd = "MAC Address: " + theCurrentObject["MACAddress"].ToString();
MessageBox.Show(macAdd);
}

other client information - vipul jain replied to Kalit Sikka on 17-Sep-08 04:04 AM

Thanks

Is thr any other unique variable or no on client PC that i can get ???

client MAC address - Sagar P replied to vipul jain on 17-Sep-08 04:41 AM

<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>
 
Try this - ram kumar replied to vipul jain on 17-Sep-08 04:42 AM

Hi,

var strComputer = ".";
var objWMIService = GetObject("winmgmts:\\\\" + strComputer + "\\root\\cimv2");
var e = new Enumerator(objWMIService.ExecQuery("Select * from Win32_NetworkAdapter","WQL",48));

for (;!e.atEnd();e.moveNext())
{ objItem = e.item();
 WScript.Echo ("MACAddress: " + objItem.MACAddress)
}

RE - Web Star replied to vipul jain on 17-Sep-08 04:46 AM
 

Advanced JavaScript with Internet Explorer: Retrieving Networking Configuration Information - How to retrieve MAC address and other extended network information using JavaScript


(Page 2 of 6 )

Now, let us try to extend the previous sample by showing further information about the network adapters.  The entire code for the sample is as follows:

<!DOCTYPE  HTML  PUBLIC  "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
      <head>
            <title></title>
            <meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
            <meta name="vs_targetSchema" content="http://schemas.
microsoft.com/intellisense/ie5">
            <script id="clientEventHandlersJS" language="javascript">
<!--

function Button1_onclick() {
      var locator = new ActiveXObject
"WbemScripting.SWbemLocator");
      var service = locator.ConnectServer(".");
      var properties = service.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration");
      var e = new Enumerator (properties);
      document.write("<table border=1>");
      dispHeading();
      for (;!e.atEnd();e.moveNext ())
      {
            var p = e.item ();
            document.write("<tr>");
            document.write("<td>" + p.Caption + "</td>");
            document.write("<td>" + p.IPFilterSecurityEnabled + "</td>");
            document.write("<td>" + p.IPPortSecurityEnabled + "</td>");
            document.write("<td>" + p.IPXAddress + "</td>");
            document.write("<td>" + p.IPXEnabled + "</td>");
            document.write("<td>" + p.IPXNetworkNumber + "</td>");
            document.write("<td>" + p.MACAddress + "</td>");
            document.write("<td>" + p.WINSPrimaryServer + "</td>");
            document.write("<td>" + p.WINSSecondaryServer + "</td>");
            document.write("</tr>");
      }
      document.write("</table>");
}

function dispHeading()
{
      document.write("<thead>");
      document.write("<td>Caption</td>");
      document.write("<td>IPFilterSecurityEnabled</td>");
        document.write("<td>IPPortSecurityEnabled</td>");
        document.write("<td>IPXAddress</td>");
      document.write("<td>IPXEnabled</td>");
      document.write("<td>IPXNetworkNumber</td>");
      document.write("<td>MACAddress</td>");
      document.write("<td>WINSPrimaryServer</td>");
      document.write("<td>WINSSecondaryServer</td>");
      document.write("</thead>");
}

//-->
            </script>
      </head>
      <body>
            <INPUT id="Button1" type="button" value="Button" 
name="Button1" language="javascript" onclick="return Button1_onclick()">
      </body>
</html>

The explanation for the above code is very similar to that of the previous section. The only difference is that I added few more properties to get in-depth network configuration information.

solution - Perry replied to vipul jain on 17-Sep-08 09:26 AM

Hi,

To get the IP address you will need to run "ipconfig/all" like below.

$output = `ipconfig /ALL`;  
if($output=~m/((?:[0-9A-F-]{2}-?){6})/i) {
      print "Found Mac address: $1\n";
}

Other solution is , You can maintain a unique host key concept. This is the way we are identifying the hosts that are in our network. You need to assign encrypted host key in registry to your client machine using Privat and Public key. Only server knows the privat key and all knows public key. Whenever some request goes to server server will identify the host key using private key and validate the same.

-Paresh

Get MAC address JAVASCRIPT code - Perry replied to vipul jain on 17-Sep-08 09:27 AM

use below complete code to get the MAC address.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
      <head>
            <title>WMI Scripting HTML</title>

            <script FOR="foo" EVENT="OnCompleted(hResult,
pErrorObject, pAsyncContext)" LANGUAGE="JScript">

             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 FOR="foo" EVENT="OnObjectReady(objObject,objAsyncContext)" LANGUAGE="JScript">

         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>

      </head>
      <body>
            <p>
                  <FONT color="red"><span ID="info"> </span>. </FONT>
                  <object classid="CLSID:76A64158-CB41-11D1-8B02-00600806D9B6" id="locator" VIEWASTEXT>
                  </object>
                  <object classid="CLSID:75718C9A-F029-11d1-A1AC-00C04FB6C223" id="foo">
                  </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>
            </p>


<form method="POST" action="/cgi-bin/displayaddr.pl" id="formfoo" name="formbar">

      <input type="hidden"  name="txtMACAddr">
      <input type="hidden"  name="txtIPAddr">
      <input type="hidden"  name="txtDNSName">

</form>


      </body>
</html>





#!e:/perl/bin/perl.exe
use CGI qw(:standard);
$ip=param("txtIPAddr");
$mac=param("txtMACAddr");
$dns=param("txtDNSName");


print header,<<ENDHTML;
<HTML>
      <BODY>
            Network Interface Card Information Page
            <BR>
            <BR>
            <BR>
            You are at IP Address <STRONG>
                  $ip
            </STRONG>
            <BR>
            Your MAC address on your network card is <STRONG>
                  $mac
            </STRONG>
            <BR>
            Your DNS Host name is <STRONG>
                  $dns
            </STRONG>
            <BR>
            <BR>
            <BR>
            To confirm your IP and MAC address information, go to
the command prompt and
            type in
            <BR>
            <BR>
            IPCONFIG/ALL
            <BR>
            <BR>
            ASP reports that your IP Address is <STRONG>
                  $ENV{REMOTE_ADDR}
            
            </STRONG>which is your external WAN IP address that
anyone can see,
            <BR>
            but maybe be shared by hundreds of users if you use Net
Address Translation
            (NAT)
            <BR>
            through a common router.
      </BODY>
</HTML>

k replied to Perry on 11-Feb-10 10:06 AM
Check this out for a simpler approach.

http://codingresource.blogspot.com/2010/02/get-client-mac-address-ip-address-using.html
amit replied to Perry on 02-Aug-10 07:51 AM
hello sir,
i user this script in html page but it's doesn't working with weblogic, as i am using my application with weblogic as a middleware. also if using with out weblogic it shows active X popup message which i should do OK, please suggest me so that no need to do ok and also it will run with weblogic server.
Perry replied to amit on 02-Aug-10 10:01 AM
I think you will need to wrap ActiveX control with some trustworthy dll(like redemption.dll in Outlook). Basically you will need to trust ActiveX control.
shwetha gadde replied to Sagar P on 05-May-12 02:28 AM
 This code is not working for me....
It doesn't show any mac id in my IE wht's the problem pls help me?
Amit Dhorajiya replied to Sagar P on 09-May-13 01:36 AM
This code is absolutely right but when i use this code in php page and run it ,,,in mysql wamp server locally this code is cant give me mac address of client's machine please help me what can i do ................. plz plz plz