java通过ip获取mac地址

  
  
  1. package com.dragonsoft.common.sys.util;
  2. import java.io.IOException;
  3. import java.net.DatagramPacket;
  4. import java.net.DatagramSocket;
  5. import java.net.InetAddress;
  6. public class MacAddressUtils {
  7. private String sRemoteAddr;
  8. private int iRemotePort = 137;
  9. private byte[] buffer = new byte[1024];
  10. private DatagramSocket ds = null;
  11. /**
  12. * 通過IP獲取MAC地址
  13. * @param ip
  14. * @return
  15. */
  16. public static String getMacAddrByIp(String ip){
  17. String macAddr = "";
  18. try {
  19. MacAddressUtils utils = new MacAddressUtils(ip);
  20. macAddr = utils.GetRemoteMacAddr();
  21. } catch (Exception e) {
  22. e.printStackTrace();
  23. }
  24. return macAddr;
  25. }
  26. public MacAddressUtils(String strAddr) throws Exception {
  27. sRemoteAddr = strAddr;
  28. ds = new DatagramSocket();
  29. }
  30. protected final DatagramPacket send(final byte[] bytes) throws IOException {
  31. DatagramPacket dp = new DatagramPacket(bytes, bytes.length, InetAddress.getByName(sRemoteAddr), iRemotePort);
  32. ds.send(dp);
  33. return dp;
  34. }
  35. protected final DatagramPacket receive() throws Exception {
  36. DatagramPacket dp = new DatagramPacket(buffer, buffer.length);
  37. ds.receive(dp);
  38. return dp;
  39. }
  40. protected byte[] GetQueryCmd() throws Exception {
  41. byte[] t_ns = new byte[50];
  42. t_ns[0] = 0x00;
  43. t_ns[1] = 0x00;
  44. t_ns[2] = 0x00;
  45. t_ns[3] = 0x10;
  46. t_ns[4] = 0x00;
  47. t_ns[5] = 0x01;
  48. t_ns[6] = 0x00;
  49. t_ns[7] = 0x00;
  50. t_ns[8] = 0x00;
  51. t_ns[9] = 0x00;
  52. t_ns[10] = 0x00;
  53. t_ns[11] = 0x00;
  54. t_ns[12] = 0x20;
  55. t_ns[13] = 0x43;
  56. t_ns[14] = 0x4B;
  57. for (int i = 15; i < 45; i++) {
  58. t_ns[i] = 0x41;
  59. }
  60. t_ns[45] = 0x00;
  61. t_ns[46] = 0x00;
  62. t_ns[47] = 0x21;
  63. t_ns[48] = 0x00;
  64. t_ns[49] = 0x01;
  65. return t_ns;
  66. }
  67. protected final String GetMacAddr(byte[] brevdata) throws Exception {
  68. // 获取计算机名
  69. int i = brevdata[56] * 18 + 56;
  70. String sAddr = "";
  71. StringBuffer sb = new StringBuffer(17);
  72. // 先从第56字节位置,读出Number Of Names(NetBIOS名字的个数,其中每个NetBIOS Names
  73. // Info部分占18个字节)
  74. // 然后可计算出“Unit ID”字段的位置=56+Number Of
  75. // Names×18,最后从该位置起连续读取6个字节,就是目的主机的MAC地址。
  76. for (int j = 1; j < 7; j++)
  77. {
  78. sAddr = Integer.toHexString(0xFF & brevdata[i + j]);
  79. if (sAddr.length() < 2)
  80. {
  81. sb.append(0);
  82. }
  83. sb.append(sAddr.toUpperCase());
  84. if (j < 6)
  85. sb.append(':');
  86. }
  87. return sb.toString();
  88. }
  89. public final void close() {
  90. try
  91. {
  92. ds.close();
  93. }
  94. catch (Exception ex) {
  95. ex.printStackTrace();
  96. }
  97. }
  98. public final String GetRemoteMacAddr() throws Exception {
  99. byte[] bqcmd = GetQueryCmd();
  100. send(bqcmd);
  101. DatagramPacket dp = receive();
  102. String smac = GetMacAddr(dp.getData());
  103. close();
  104. return smac;
  105. }
  106. }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值