用ASP读取网卡MAC地址

发表于:2007-06-30来源:作者:点击数: 标签:
% ‘’用ASP读取网卡MAC地址 Dim RemoteAddr If Request.ServerVariables("HTTP_X_FORWARDED_FOR") = Empty Then RemoteAddr = Request.ServerVariables("REMOTE_ADDR") Else RemoteAddr = Request.ServerVariables("HTTP_X_FORWARDED_FOR") End If Response

<%
‘’用ASP读取网卡MAC地址
Dim RemoteAddr
If Request.ServerVariables("HTTP_X_FORWARDED_FOR") = Empty Then
  RemoteAddr = Request.ServerVariables("REMOTE_ADDR")
Else
  RemoteAddr = Request.ServerVariables("HTTP_X_FORWARDED_FOR")
End If
Response.Write(GetMac(RemoteAddr))

‘’‘’由于读取某IP的网卡MAC地址

‘’‘’本程序调用arp命令通过查询本机arp表读取特定IP的MAC地址

‘’‘’本程序需要“WSCRIPT.SHELL”和“Scripting.FileSystemObject”两个组件,

‘’‘’请确保您的服务器可以正常使用这两个组件

‘’‘’本程序需要调用Cmd.exe程序,临时文件保存结果,请确保IIS来宾帐号对程序有访问权限,

‘’‘’临时目录有写‘’‘’权‘’‘’限。

Function GetMac(IP)
  On Error Resume Next
  Dim oScript
  Dim oFileSys, oFile
  Dim All, szTempFile,ipc,phyc,typec
  Dim TempPath
  Set oScript = Server.CreateObject("WSCRIPT.SHELL")
  Set oFileSys = Server.CreateObject("Scripting.FileSystemObject")
  TempPath="c:\temp\" ‘’‘’临时目录
  szTempFile = TempPath & oFileSys.GetTempName() ‘’‘’ 获取临时文件名
  Call oScript.Run ("cmd.exe /c ping -n 2 " & IP, 0, True) ‘’‘’Arp表中须有此IP
  Call oScript.Run ("cmd.exe /c arp -a " & IP & " > " & szTempFile, 0, True)
  Set oFile = oFileSys.OpenTextFile (szTempFile, 1, False, 0)
  All=oFile.ReadAll()
  oFile.Close
  If (IsObject(oFile)) Then
    Call oFileSys.DeleteFile(szTempFile, True)
  End If
  arr = Split(All, vbCrLf)
  If UBound(arr) = 4 Then
    Ipc= InStr(1, arr(2), "Internet Address")
    phyc = InStr(1, arr(2), "Physical Address")
    typec = InStr(1, arr(2), "Type")
    If typec > phyc And phyc > Ipc And ipc > 0 Then
      GetMac=Ucase(Trim(CStr(Mid(arr(3), phyc, typec - phyc))))
    End If
  End If
End Function
%>

原文转自:http://www.ltesting.net