Get Remote MAC ?

L

localhost

How can I obtain the MAC address of a remote web client? I can check
the Server Variables for the IP number, but want to derive the MAC
from it. I am not worried that it may be the MAC of the web client's
firewall, I still want the MAC - just like I could get with ARP at a
command prompt.

Thanks.
 
S

Steven Cheng[MSFT]

Hi Localhost,


Thank you for using Microsoft Newsgroup Service. Based on your description,
you want to get the MAC address info of the client side machine which visit
your ASP.NET web application on the server. Is my understanding correct?

As for this problem, the ASP.NET has provided some buildin classes and
methods for getting client side info. For example,"Request.Browser.***" can
get the client's browser info, "Request.UserHostAddress" can get the client
ip address. However, the MAC address isn't support by the buildin methods
or properties. If we do want to get the remote client's MAC address, we
need to use some scripts, the following code is as a reference:

<%@ 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 & " > d:\inetpub\wwwroot\" &
strIP & ".txt",0,true
Set sh = nothing
Set fso = createobject("scripting.filesystemobject")
Set ts = fso.opentextfile("d:\inetpub\wwwroot\" & 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 "d:\inetpub\wwwroot\" & strIP & ".txt"
Set fso = nothing
GetMACAddress = macaddress
End function
%>
<HTML>
<HEAD>
<TITLE></TITLE>
</HEAD>
<BODY>
<%Response.Write("Your IP is : " & strIP & "<BR>" & vbcrlf)%>
<%Response.Write("Your MAC is : " & strMac & vbcrlf)%>
</BODY>
</HTML>

Hope this helps.


Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
L

localhost

I am absolutely *not* looking for WSH. I am interested in a way,
using the .NET Framework classes, to obtain a MAC address based on an
IP number. I am very familar with the Request class and know how to
obtain a visiting browser's IP number with it. Knowing an IP number,
I need to know to extrapolate the MAC address. How can this be done
with the .NET Framework? I know that the MAC is not in the Request
object, I need some guidance on getting the MAC *after* I have used
the Request object to get the IP number.

Thanks.


As for this problem, the ASP.NET has provided some buildin classes and
methods for getting client side info. For
example,"Request.Browser.***" can
 
E

|{evin

I am absolutely *not* looking for WSH. I am interested in a way,
using the .NET Framework classes, to obtain a MAC address based on an
IP number. I am very familar with the Request class and know how to
obtain a visiting browser's IP number with it. Knowing an IP number,
I need to know to extrapolate the MAC address. How can this be done
with the .NET Framework? I know that the MAC is not in the Request
object, I need some guidance on getting the MAC *after* I have used
the Request object to get the IP number.

Thanks.

I don't think you can... if you're able to retrieve any mac address, I
believe it would be the one of the next hop away (switch, router,
etc). MAC information from the original machine isn't carried in IP
segments.
 
S

Steven Cheng[MSFT]

Hi Localhost,


Thank you for the prompt response. As for the way to get a computer's MAC
address via IP in dotnet, I think you may try using the "using
System.Management" Namespace, you need to manually add its dll into your
project since it's not referenced by default. Here is a period of sample
code for using the interfaces under it to retrieve the MAC address via IP:

----------------------------------------------------
protected string GetMACAddressByIP(string ip)
{
try
{
System.Management.ManagementObjectSearcher query= new
System.Management.ManagementObjectSearcher("SELECT * FROM
Win32_NetworkAdapterConfiguration");
System.Management.ManagementObjectCollection queryCollection = query.Get();

bool Found = false;

foreach(System.Management.ManagementObject mo in queryCollection)
{
if(mo["IPAddress"] != null)
{

string temp;
temp = string.Join(".",(string[])mo["IPAddress"] );
if(!temp.Equals(""))
{
if(!ip.Equals(""))
{
if(temp.Equals(ip.Trim()))
{
Found = true;
}

}
else
{
Found = true;
}
}

if(Found == true)
{
if(mo["macaddress"] != null)
{
if(!mo["macaddress"].Equals(""))
{
return (string)mo["macaddress"];
}
}
}
else
{
Found = false;
}

}
}

MessageBox.Show("No Mac Address Found");
return "";

}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
return "";
}
}
----------------------------------------------------

Please try out the preceding suggestions to see whether they help.


Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top