segunda-feira, 16 de abril de 2001

Identificando o endereço MAC da placa de rede

O script abaixo, mediante uma chamada ao Window Script Host, retorna o endereço MAC da placa de rede instalada no client. Note que este script não pode ser executado da máquina com o servidor web.


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


'**************************************
' Name: MAC address
' Description:Get the clients MAC(Media Access Control)
'  address, a hardware address that uniquely
'  identifies Each node of a network. Works great on
'  LAN's. Firewalls and Proxy's will be an issue
'  depending what side of them you're coding for.
' By: Jerry Aguilar
'
'
' Inputs:None
'
' Returns:Returns the client IP and MAC
'     address.
'
'Assumes:You can't navigate to it running
' PWS on the same pc but If you are running PWS, you can navigate
' To it from another pc on the same lan (it does
' Not like 127.0.0.1)
'
'Side Effects:None
'
'Warranty:
'code provided by Planet Source Code(tm)
'     (
http://www.Planet-Source-Code.com) 'as
'     is', without warranties as to performanc
'     e, fitness, merchantability,and any othe
'     r warranty (whether expressed or implied
'     ).
'Terms of Agreement:
'By using this source code, you agree to
'     the following terms...
' 1) You may use this source code in per
'     sonal projects and may compile it into a
'     n .exe/.dll/.ocx and distribute it in bi
'     nary format freely and with no charge.
' 2) You MAY NOT redistribute this sourc
'     e code (for example to a web site) witho
'     ut written permission from the original
'     author.Failure to do so is a violation o
'     f copyright laws.
' 3) You may link to this code from anot
'     her website, provided it is not wrapped
'     in a frame.
' 4) The author of this code may have re
'     tained certain additional copyright righ
'     ts.If so, this is indicated in the autho
'     r's description.
'**************************************


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>



Esta matéria foi postada originalmente no ASP4Developers por Rubens N. Farias (site), que na época era "pós-graduado em análise de sistemas orientados a objetos, MCP, MCSD, MCAD, MCSD.NET e consultor em TI, além de idealizador do projeto ASP4Developers. Desenvolve sistemas sob medida, focados na satisfação do usuário, com qualidade e custo realista.". Hoje, vai saber...

0 comentários: