index.ashx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <%@ WebHandler Language="C#" Class="getIP" %>
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Web;
  6. using System.Web.UI;
  7. using System.Web.UI.WebControls;
  8. using System.Runtime.InteropServices;
  9. using System.Diagnostics;
  10. using System.Text.RegularExpressions;
  11. public class getIP : IHttpHandler
  12. {
  13. public void ProcessRequest(HttpContext context)
  14. {
  15. context.Response.ContentType = "text/html";
  16. context.Response.Write("HostAddress:"+context.Request.UserHostAddress +"<br>");
  17. context.Response.Write("HostName:"+context.Request.UserHostName +"<br>");
  18. context.Response.Write("Agent:"+context.Request.UserAgent +"<br>");
  19. context.Response.Write("REMOTE_ADDR:"+context.Request.ServerVariables["REMOTE_ADDR"] +"<br>");
  20. context.Response.Write("HTTP_X_FORWARDED_FOR:"+context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] +"<br>");
  21. string ip = context.Request.ServerVariables["REMOTE_ADDR"];
  22. context.Response.Write("MAC:"+GetClientMac(ip) +"<br>");
  23. context.Response.Write("MAC2:"+GetClientMac(ip) +"<br>");
  24. }
  25. [DllImport("Iphlpapi.dll")]
  26. private static extern int SendARP(Int32 dest, Int32 host, ref Int64 mac, ref Int32 length);
  27. [DllImport("Ws2_32.dll")]
  28. private static extern Int32 inet_addr(string ip);
  29. public static string GetClientMac(string IP)
  30. {
  31. Int32 ldest = inet_addr(IP);
  32. Int64 macinfo = new Int64();
  33. Int32 len = 6;
  34. int res = SendARP(ldest, 0, ref macinfo, ref len);
  35. string mac_src = macinfo.ToString("X");
  36. while (mac_src.Length < 12)
  37. {
  38. mac_src = mac_src.Insert(0, "0");
  39. }
  40. string mac_dest = "";
  41. for (int i = 0; i < 11; i++)
  42. {
  43. if (0 == (i % 2))
  44. {
  45. if (i == 10)
  46. {
  47. mac_dest = mac_dest.Insert(0, mac_src.Substring(i, 2));
  48. }
  49. else
  50. {
  51. mac_dest = "-" + mac_dest.Insert(0, mac_src.Substring(i, 2));
  52. }
  53. }
  54. }
  55. return mac_dest;
  56. }
  57. //跨网段取Mac
  58. public string GetMac(string IP)
  59. {
  60. string dirResults = "";
  61. ProcessStartInfo psi = new ProcessStartInfo();
  62. Process proc = new Process();
  63. psi.FileName = "nbtstat";
  64. psi.RedirectStandardInput = false;
  65. psi.RedirectStandardOutput = true;
  66. psi.Arguments = "-A " + IP;
  67. psi.UseShellExecute = false;
  68. proc = Process.Start(psi);
  69. dirResults = proc.StandardOutput.ReadToEnd();
  70. proc.WaitForExit();
  71. dirResults = dirResults.Replace("\r", "").Replace("\n", "").Replace("\t", "");
  72. Regex reg = new Regex("Mac[ ]{0,}Address[ ]{0,}=[ ]{0,}(?<key>((.)*?))__MAC", RegexOptions.IgnoreCase | RegexOptions.Compiled);
  73. Match mc = reg.Match(dirResults + "__MAC");
  74. if (mc.Success)
  75. { return mc.Groups["key"].Value; }
  76. else
  77. {
  78. reg = new Regex("Host not found", RegexOptions.IgnoreCase | RegexOptions.Compiled);
  79. mc = reg.Match(dirResults);
  80. if (mc.Success)
  81. {
  82. return "Host not found!";
  83. }
  84. else
  85. { return ""; }
  86. }
  87. }
  88. public bool IsReusable
  89. {
  90. get
  91. {
  92. return false;
  93. }
  94. }
  95. }