Link to home
Start Free TrialLog in
Avatar of jagu98
jagu98

asked on

Get client MAC Address

I need to get the MAC address of the client machine when it navigates to a specific page on my WinNT Server running IIS.  If anyone knows a script(ASP or Javascript preferred) or has another suggestion, please let me know.
I've tried creating an ActiveX dll in vb but no good.
Avatar of Ginger_Ed
Ginger_Ed

Have a look at this, there are some limitations in that it only works in IE.  Also it comes up with a message saying: "Do you want to run this ActiveX Control", but if they have security settings set to high, it just won't do anything except give errors.

It does the joba nd gets all the mac address of the machine, but try it out:

<HTML>
<HEAD>
<SCRIPT LANGUAGE=JScript>

var address = new Array()
var i = 0
var wsh = new ActiveXObject("WScript.Shell")
var fso = new ActiveXObject("Scripting.FileSystemObject")

wsh.Run("cmd /C ipconfig /all > c:\\temp.txt", 0)


function getMacAddress() {

var file = fso.OpenTextFile("c:\\temp.txt", 1)
var currentLine
var colon

      while(file.AtEndOfStream != true)
            {
               currentLine = file.ReadLine()               
               if(currentLine.indexOf("Physical Address") >= 0)
                  {
                    colon = currentLine.indexOf(":")
                    address[i] = currentLine.substr(colon + 1)      
                    i++       
                  }      

            }

file.Close()
f = fso.GetFile("c:\\temp.txt");
f.Delete();

}

function writeAddress() {

      for(j = 0; j < i; j++)
            {
              mac.innerHTML = mac.innerHTML + "  <BR>Mac Address: " + address[j]
            }

}
</SCRIPT>



<TITLE></TITLE>
</HEAD>
<BODY>
<P id=mac>
</P>
<Input type="Button" Value="Retrieve Mac Addresses" onClick="getMacAddress()">
<Input type="Button" Value="Display Addresses" onClick="writeAddress()">

</BODY>
</HTML>



Also, I had to put the code that runs ipconfig "wsh.run" at the start because when I had that line then tried to read it straight away I was getting a problem in that it was trying to read the file before it was created so it gave errors.

Also you will need to detect the o/s and if it is 95/98 and change the line.  Wsh.Run("cmd" to wsh.Run("Command"  and change the switch.

It is a start that needs some work, but there isn't any point if you need it to work on nn.

Ed
another thought, is that I am sure with IIS you can get the ip address of the user, (I think it is Server.RequetsVariables("REMOTE_ADDR"))

you could use something like wsh again but on the server side, but if there is a command line tool with changes an ip to a mac address, I don't know of one off teh top of my head but there must be something.

Ed
Avatar of jagu98

ASKER

I thank you Ginger Ed for your comments, but unfortunatly I need to code for Netscape or IE.  One of the versions of the ActiveX dll I made actually did a shell out to an EXE I wrote using nbtstat -A "ip address" > at the command prompt and created an htm file with the information I need.  It works great on the pc but when I call it from ASP... CRASH!  Major mem leaks and it didn't create the htm file.
instead of creating a file use http://www.serverobjects.com/comp/Aspexec.zip to run something like arp to get the mac based on REMOTE_ADDR
ASKER CERTIFIED SOLUTION
Avatar of Ginger_Ed
Ginger_Ed

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jagu98

ASKER

Ginger Ed - I was successful earlier today and it just so happens that I did it the way you suggested on your last comment prior to viewing it.  You pointed me in the right direction.  I left the code at work but I'll make sure to post it here tomorrow along with giving you the credit of the points.  Thanks again for pointing in the direction.
Avatar of jagu98

ASKER

A word of caution, if you test this on your own pc running PWS it will hang. NBTSTAT does not like 127.0.0.1.  You can let it run on PWS and navigate to it from another pc and it will work fine.

<%@ LANGUAGE="VBSCRIPT"%>
<%

      strIP = Request.ServerVariables("REMOTE_ADDR")
      strMac = GetMACAddress(strIP)
      strHost = Request.ServerVariables("REMOTE_HOST")

 Function GetMACAddress(strIP)
   set net = Server.CreateObject("wscript.network")
   set sh = Server.CreateObject("wscript.shell")
   sh.run "%comspec% /c nbtstat -A " & strIP & " > c:\" & strIP & ".txt",0,true
   set sh = nothing
   set fso = createobject("scripting.filesystemobject")
   set ts = fso.opentextfile("c:\" & strIP & ".txt")
   macaddress = null
   do while not ts.AtEndOfStream
     data = ucase(trim(ts.readline))
     if instr(data,"MAC ADDRESS") then
       macaddress = trim(split(data,"=")(1))
       exit do
     end if
   loop
   ts.close
   set ts = nothing
   fso.deletefile "c:\" & strIP & ".txt"
   set fso = nothing
   GetMACAddress = macaddress
 End Function      

%>
<html>
<HEAD>
<TITLE>Say Hello To the MAC MAN</TITLE>
</HEAD>
<body>
<%Response.Write("Your IP is : " & strIP & "<br>" & vbcrlf)%>
<%Response.Write("Your MAC is : " & strMac & vbcrlf)%>
</body>
</html>
Avatar of jagu98

ASKER

Thanks again Ginger Ed
cool,

I just wanted to say:

"Hello Mac Man"


Ed